dbTalk Databases Forums  

help with a specific query

comp.databases.mysql comp.databases.mysql


Discuss help with a specific query in the comp.databases.mysql forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
elubin
 
Posts: n/a

Default help with a specific query - 06-23-2010 , 09:36 PM






given the following two tables, i am looking for help writing a query
that returns all rows in table the first table along with the data
from the second time with the latest (newest) timestamp. thanks!

id name
1 A
2 B
3 C
4 D

id time text
1 2010-06-23 20:40:00 first
1 2010-06-23 20:41:00 second
1 2010-06-23 20:42:00 third
2 2010-06-23 20:40:00 first
2 2010-06-23 20:41:00 second
2 2010-06-23 20:42:00 third
3 2010-06-23 20:40:00 first
3 2010-06-23 20:41:00 second
3 2010-06-23 20:42:00 third

the result i would like from the query is:
A third
B third
C third
d null

Reply With Quote
  #2  
Old   
Doug Miller
 
Posts: n/a

Default Re: help with a specific query - 06-23-2010 , 09:50 PM






In article <b70acc0e-9ee6-445f-a13e-fa2ca0fb1e0f (AT) 5g2000yqz (DOT) googlegroups.com>, elubin <elubin1234 (AT) gmail (DOT) com> wrote:
Quote:
given the following two tables, i am looking for help writing a query
that returns all rows in table the first table along with the data
from the second time with the latest (newest) timestamp. thanks!
Smells like homework. What have you tried so far, and what happened when you
tried it?
Quote:
id name
1 A
2 B
3 C
4 D

id time text
1 2010-06-23 20:40:00 first
1 2010-06-23 20:41:00 second
1 2010-06-23 20:42:00 third
2 2010-06-23 20:40:00 first
2 2010-06-23 20:41:00 second
2 2010-06-23 20:42:00 third
3 2010-06-23 20:40:00 first
3 2010-06-23 20:41:00 second
3 2010-06-23 20:42:00 third

the result i would like from the query is:
A third
B third
C third
d null

Reply With Quote
  #3  
Old   
Captain Paralytic
 
Posts: n/a

Default Re: help with a specific query - 06-24-2010 , 04:46 AM



On 24 June, 02:36, elubin <elubin1... (AT) gmail (DOT) com> wrote:
Quote:
given the following two tables, i am looking for help writing a query
that returns all rows in table the first table along with the data
from the second time with the latest (newest) timestamp. *thanks!

id name
1 *A
2 *B
3 *C
4 *D

id time * * * * * * * * * * * *text
1 *2010-06-23 20:40:00 first
1 *2010-06-23 20:41:00 second
1 *2010-06-23 20:42:00 third
2 *2010-06-23 20:40:00 first
2 *2010-06-23 20:41:00 second
2 *2010-06-23 20:42:00 third
3 *2010-06-23 20:40:00 first
3 *2010-06-23 20:41:00 second
3 *2010-06-23 20:42:00 third

the result i would like from the query is:
A third
B third
C third
d null
Search this group for the strawberry query

Reply With Quote
  #4  
Old   
elubin
 
Posts: n/a

Default Re: help with a specific query - 06-24-2010 , 01:11 PM



not a homework assignment... rather an app i'm trying to develop...
just don't have strong SQL skills.

strawberry query works great for the first part of my question
(getting the max values from the second table), but still can't figure
out how to successfully LEFT OUTER JOIN so i get all rows from the
first table.

select t1.wait_time,t1.id,t1.insert_time from times t1
where t1.insert_time= (select max(t2.insert_time) from times t2
where t1.id=t2.id)
order by id

Reply With Quote
  #5  
Old   
elubin
 
Posts: n/a

Default Re: help with a specific query - 06-24-2010 , 01:18 PM



Sorry, meant to post the INNER JOIN strawberry type, not the sub-
select...

SELECT t1.text,t1.id,t1.insert_time
FROM times t1
LEFT JOIN times t2 ON t1.id = t2.id AND t1.insert_time <
t2.insert_time
WHERE t2.id IS NULL;

Reply With Quote
  #6  
Old   
elubin
 
Posts: n/a

Default Re: help with a specific query - 06-24-2010 , 01:35 PM



OK, finally got it with a proper second join....

SELECT r.name,t1.wait_time,t1.id,t1.insert_time
FROM tableA r
left outer join tableB t1 on t1.id=r.id
LEFT JOIN tableB t2 ON t1.id = t2.id AND t1.insert_time <
t2.insert_time
WHERE t2.id IS NULL;

Reply With Quote
  #7  
Old   
Captain Paralytic
 
Posts: n/a

Default Re: help with a specific query - 06-24-2010 , 04:12 PM



On Jun 24, 6:18*pm, elubin <elubin1... (AT) gmail (DOT) com> wrote:
Quote:
Sorry, meant to post the INNER JOIN strawberry type, not the sub-
select...

SELECT t1.text,t1.id,t1.insert_time
FROM times t1
LEFT JOIN times t2 ON t1.id = t2.id AND t1.insert_time
t2.insert_time
WHERE t2.id IS NULL;
Glad it helped

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.