Solved

sql join

Posted on 2011-09-15
3
272 Views
Last Modified: 2012-05-12
Table A has following fields and data
CID, Addkey, Cplan, changeDt
1,1a, planA, 10/10/2010
2, 1b, planD, 12/10/2010
3, 2a planE, 07/15/2010
4, 1a, planA, 06/22/2010
5, 1b, planD, 10/23/2010
6 1c, planA, 12/10/2010
7, 1a, planA, 06/06/2011

I want to find all CIDs where Addkey, Cplan are same and set them as master and child CID.
MasterCID, ChildCID. The CID with most recent changeDt should be the masterCID and the other one will be the childCID. ex

MasterCID, ChildCID
1,4
1,7
5,2

How do I do that using sql (MS Sql Server 2008)?

Thanks.

0
Comment
Question by:patd1
  • 2
3 Comments
 
LVL 25

Accepted Solution

by:
jogos earned 167 total points
ID: 36543348
First identify the most recent date for each combination (subquery) and then find the matching records
select x.cid,y.cid
from (select cid,addkey,cplan, max(changedt)
from a group by addkey,cplan
) a as x 
inner join a as y on ( x.addkey = y.addkey 
                          and x.cplan = y.cplan
                          and x.changedt > y.changedt)

Open in new window

0
 
LVL 18

Assisted Solution

by:deighton
deighton earned 333 total points
ID: 36543372
SELECT SUBQ.CID AS MASTERCID, T1.CID FROM TABLEA T1 JOIN
(select *, row_number() OVER(PARTITION BY ADDKEY,CPLAN ORDER BY ChangeDT,cid) AS rown FROM tablea) SUBQ
ON SUBQ.rown=1 AND SUBQ.ADDKEY = T1.ADDKEY AND SUBQ.CPLAN = T1.CPLAN 
	AND SUBQ.CID <> T1.CID

Open in new window

0
 
LVL 18

Assisted Solution

by:deighton
deighton earned 333 total points
ID: 36543416
or perhaps

SELECT SUBQ.CID AS MASTERCID, T1.CID FROM TABLEA T1 JOIN
(select *, row_number() OVER(PARTITION BY ADDKEY,CPLAN ORDER BY ChangeDT DESC,cid) AS rown FROM tablea) SUBQ
ON SUBQ.rown=1 AND SUBQ.ADDKEY = T1.ADDKEY AND SUBQ.CPLAN = T1.CPLAN
      AND SUBQ.CID <> T1.CID
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now