SQL Recipes
A FREE cookbook for SQL queries and examples
Register FAQ Search Today's Posts Mark Forums Read

SQL - Questions and Answers Have a SQL question? Post it here. First do a search to see if someone hasn't already answered it.

Go Back   SQL Recipes a FREE cookbook of SQL queries and examples > SQL queries and examples > SQL - Questions and Answers

Reply
 
LinkBack Thread Tools Search this Thread
  #1 (permalink)  
Old 07-07-2008, 05:06 PM
Unregistered
 
Posts: n/a
Default ANY dialect question:

select values from the 2 different table


Sample tables (company_id RK companies.id):
Code:
Companies
id    company
1    …
2    …
3    …

    Locations
id    company_id    location    revision
1    1    …    1
2    2    …    1
3    1    …    2
4    1    …    3
5    2    …    2
6    1    …    4
How to write an SQL query to select values from the following columns (companies.company, locations.location) where locations.revision value will be the highest for each company

I try
Select distinct companies.company, locations.location from Companies, Locations where companies.id=Locations.company_id ...

How? thanks.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

ANSWER(S):

  #2 (permalink)  
Old 07-09-2008, 04:32 AM
Unregistered
 
Posts: n/a
Default ANY answer. Re: select values from the 2 different table

SELECT Companies.id, Locations.location, revision
FROM Companies INNER JOIN Locations ON Companies.id=Locations.id
INNER JOIN
(SELECT C1.id, max(C2.revision) AS maxrevision
FROM Companies C1 INNER JOIN Locations C2 ON C1.id=C2.id GROUP BY C1.id) T2
ON Companies.id=T2.id AND revision=maxrevision

I do this...it works, but if i just want to output the first 2 column, how?
Companies.id, Locations.location only..i only can do if revision is output together with it,.

Please help. thanks.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 07-09-2008, 08:48 PM
Dimitar
 
Posts: n/a
Default ANY answer. Re: select values from the 2 different table

SELECT
 t1.company,t0.location
FROM
 Companies t1
,
(SELECT company_id,location FROM Locations WHERE revision IN
(SELECT max(revision) AS maxrevision GROUP BY company_id)
) t0
WHERE
 t0.company_id=t1.id
;


Totally not tested.
Yet, I hope it is of any help.

Dimitar
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 10:41 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0
Copyright (c) 2006-2008 SQL Recipes

1 2 3 4 5 6 7 8