Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

sql join

Posted on 2011-09-15
3
Medium Priority
?
282 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 668 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 1332 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 1332 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

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

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

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

670 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