Sample Data
Query
;WITH CTE AS (
SELECT
olnID
,cpID
,cpIDInstance
,cpValue
FROM
(
VALUES
(1, 68, 1, N'New Bern')
,(1, 5016, 1, N'MT')
,(1, 5066, 1, N'OAK')
,(2, 68, 1, N'New Bern')
,(2, 5016, 1, N'MT')
,(2, 5066, 1, N'CHY')
,(2, 5066, 2, N'MPL')
,(2, 5066, 3, N'OAK')
) cpv (olnID,cpID,cpIDInstance,cpValue)
)
SELECT DISTINCT
a.olnID
, cpID = CASE WHEN a.cpID = b.cpID THEN a.cpID ELSE b.cpID END
, cpIDInstance = CASE WHEN a.cpID = b.cpID THEN a.cpIDInstance ELSE b.cpIDInstance END
, N = a.cpIDInstance
, cpValue = CASE WHEN a.cpID = b.cpID THEN a.cpValue ELSE b.cpValue END
FROM CTE a
CROSS JOIN CTE b
WHERE a.olnID = b.olnID
AND a.cpID >= b.cpID
ORDER BY 1, 4
|
Desired Output
Reference
http://social.msdn.microsoft.com/Forums/sqlserver/en-US/214fe2b6-b826-4057-8a09-5a760bf0df9a/help-needed-in-duplicating-data-subset#c46f989c-581d-4bc4-8214-6405bb87297f
No comments:
Post a Comment