MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 5th, 2003, 04:51 PM
lblock0110 lblock0110 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 3 lblock0110 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Unhappy Joining Tables With Twist

PLS, help ,

I am trying to JOIN two tables together with the LEFT OUTER JOIN statement, but
with a twist :

1. table 1 id and table 2 projectid are the join field
2. Table 1 has de detail info for several ocurrences in table 2. For each id
in table 1 (project detail), there are many entries of projectid on table 2
(update log on project)
3. I need a query that gets each project detail (table 1) with ONLY the LAST
(most recent) update date and comment (table 2) for that project.

The result report will look something like this:

Repeat_____________________________________________
PROJECT NAME(table 1) PROJECT DATE(table 1) PROJECT OWNER(table 1)
LAST UPDATE(table 2) LAST UPDATE COMMENT(table 2)
Repeat______________________________________________

I have used the SELECT TOP 1 function, the DISTINCT function, I am out of ideas .

Thanx

Reply With Quote
  #2  
Old November 6th, 2003, 02:46 AM
swampBoogie swampBoogie is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jan 2003
Location: Paris Uppland
Posts: 2,349 swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 4 Days 7 h 10 m 28 sec
Reputation Power: 390
Code:
select * from table1 t1 join table2 t2
on t1.id = t2.projectid
where updateDate = (select max(updateDate)
from table2
where projectid = t2.projectid)

Reply With Quote
  #3  
Old November 6th, 2003, 06:06 PM
lblock0110 lblock0110 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 3 lblock0110 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Great Thanx!,One more thing...how about if I want to display also T1 date for T2 that do not have any updateDate but I need then to be displayed as 'NA', Can this be accomplished?
Thanx in advance

Reply With Quote
  #4  
Old November 7th, 2003, 04:09 AM
swampBoogie swampBoogie is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jan 2003
Location: Paris Uppland
Posts: 2,349 swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 4 Days 7 h 10 m 28 sec
Reputation Power: 390
Using union is one possibility

Code:
select t1.*,t2.* from table1 t1 join table2 t2
on t1.id = t2.projectid
where updateDate = (select max(updateDate)
from table2
where projectid = t2.projectid)
union
select t1.*, '',... 'n/a', ...
from table1 t1
where id not in (
select projectid 
from table2)


The number of columns in each part of the union must be the same so you need to add as many constants as there are columns in table2 to the second select. These must be the same type as the columns in table2.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > Joining Tables With Twist

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap