Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

help needed in duplicating data subset

$
0
0

Thank you for any ideas, your experience and time.

temp variable contains source data set. cpID is multivalued. Instance is N signifying it

I am looking on ways to turn it into data set below

DECLARE @olnCPV TABLE
(
	olnID int NOT NULL,
	cpID smallint NOT NULL,
	cpIDInstance tinyint NOT NULL,
	cpValue nvarchar(100) NOT NULL,
	PRIMARY KEY CLUSTERED (olnID ASC,cpID ASC,cpIDInstance)WITH (IGNORE_DUP_KEY = OFF)
)
INSERT @olnCPV
--OUTPUT inserted.*
SELECT
     olnID
	,cpID
	,cpIDInstance
	,cpValue
FROM
(
    VALUES
    -- single value
     (1, 68,   1, N'New Bern')
    ,(1, 5016, 1, N'MT')
    ,(1, 5066, 1, N'OAK')
    -- multi-valued
    ,(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)

--
-- Your code here, which will produce result set below. Let us assume cpIDInstance is <= 10

SELECT *
FROM @olnCPV

--


-- Looking for this data set
SELECT *
FROM
(
    VALUES
    -- single value
     (1, 68,   1, 1, N'New Bern')
    ,(1, 5016, 1, 1, N'MT')
    ,(1, 5066, 1, 1, N'OAK')
    -- multi-valued
    ,(2, 68,   1, 1, N'New Bern')
    ,(2, 5016, 1, 1, N'MT')
    ,(2, 5066, 1, 1, N'CHY')

    ,(2, 68,   1, 2, N'New Bern')
    ,(2, 5016, 1, 2, N'MT')
    ,(2, 5066, 2, 2, N'MPL')

    ,(2, 68,   1, 3, N'New Bern')
    ,(2, 5016, 1, 3, N'MT')
    ,(2, 5066, 3, 3, N'OAK')
) cpv (olnID,cpID,cpIDInstance,N, cpValue)



Vladimir Moldovanenko


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>