Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

sql join

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

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

885 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