Microsoft SQL Server
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsDatabasesMicrosoft SQL Server

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old March 8th, 2007, 04:37 AM
qbz qbz is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2007
Posts: 1 qbz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 16 m 28 sec
Reputation Power: 0
Picking the record on table2

Hello,

i have two tables named table01 and table02,

the two tables have a common column "CUSTNO" which is set as the primary key

i don't allow duplicate custno on table01, however on table02, it is possible

to have many duplicate customer number

i want to retrieve all records from table01 and get one record that exists on table02

example:

table01

cust_id cust_name
1 John Smith
2 Grace Period
3 Bill Discounted
4 Ryan Ryan

table02

cust_id disposition date
1 Busy 01/01/2007
1 Out-of-Town 01/15/2007
1 Sleeping 01/18/2007
1 Dating 01/15/2007
3 Driving 01/17/2007


the output

cust_id cust_name disposition
1 John Smith Sleeping
2 Grace Period null
3 Bill Discounted Driving
4 Ryan Ryan null

Reply With Quote
  #2  
Old March 27th, 2007, 08:06 AM
eralpery eralpery is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2006
Posts: 16 eralpery User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 19 m 57 sec
Reputation Power: 0
Hi,
If you are using SQL Server 2005, it is easy by using the ROW_NUMBER OVER PARTITION syntax like the script below:

select
table01.cust_id,
table01.cust_name,
t.disposition,
t.date
from table01
left join (
select
row_number() over (partition by cust_id order by cust_id) rownum,
cust_id,
disposition,
date
from table02
) t on t.cust_id = table01.cust_id and t.rownum = 1



You can use the below script for SQL2k



select
table01.cust_id,
table01.cust_name,
table02.disposition,
table02.date
from table01
left join (
select
cust_id,
max(date) date
from table02
group by cust_id
) t on t.cust_id = table01.cust_id
left join table02 on t.cust_id = table02.cust_id and t.date = table02.date



I hope it helps,

Eralper
http://www.kodyaz.com

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsDatabasesMicrosoft SQL Server > Picking the record on table2


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 | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
Stay green...Green IT