Solved

sql join

Posted on 2011-09-15
3
277 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
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.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

759 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