Solved

sql join

Posted on 2011-09-15
3
276 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

Microsoft Certification Exam 74-409

VeeamĀ® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

756 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