Select 编号列
Select numbered column
我有一个动态 select 查询,它应该获取列作为 col1
、col2
......col9
、col10
但是它错误地提取为 col1
、col10
、col11
、col12
、col2
... 不确定如何将它们 select 作为查询是动态的。
请帮忙。
这是我一直在用的动态select。
-- CREATE THE COLUMNS REQUIRED
SET @DYColumns = STUFF(( SELECT DISTINCT
',' + N'sourceID'
+ CAST(ROW_NUMBER()
OVER (PARTITION BY prgmg_product_id ORDER BY prgmg_product_id, source_id_other)
AS NVARCHAR(10))
FROM #Prgmg FOR XML PATH('') ), 1, 1, '');
如果您使用 COALESCE 或 STUFF 来构建列,请确保有一个 ORDER by
此外,你可以有col01,col02,..col10来确保顺序
Drop Table #Prgmg
CREATE TABLE #Prgmg (
prgmg_product_id INT
,source_id_other INT
);
INSERT #Prgmg (
prgmg_product_id
,source_id_other
)
VALUES (3310,11478)
,(3337,10833)
,(3354,11466)
,(4039,4846)
,(4039,65454)
,(4039,65456)
,(13337,110833) -- Added to force over 10
,(13354,111466) -- Added to force over 10
,(14039,14846) -- Added to force over 10
,(14039,165454); -- Added to force over 10
DECLARE @DYColumns NVARCHAR(1000)
,@DYSqlQuery NVARCHAR(4000);
-- CREATE THE COLUMNS REQUIRED
SET @DYColumns = STUFF((
SELECT DISTINCT ','
+ N'sourceID'
+ right('00'+CAST(ROW_NUMBER() OVER (ORDER BY prgmg_product_id, source_id_other) AS NVARCHAR(10)),2)
FROM #Prgmg
FOR XML PATH('')
), 1, 1, '');
-- CREATE THE DYNAMIC SQL AND ADD IN THE CREATED COLUMNS
SET @DYSqlQuery = '
SELECT prgmg_product_id,'
+ @DYColumns
+ ' FROM (
SELECT prgmg_product_id
,CAST(N''sourceID'' + CAST(ROW_NUMBER() OVER (
PARTITION BY prgmg_product_id ORDER BY prgmg_product_id, source_id_other
) AS NVARCHAR(10)) AS NVARCHAR(100)) AS Col
,source_id_other
FROM #Prgmg S1
) X
PIVOT(MIN(source_id_other) FOR Col IN (' + @DYColumns + ')) P'
Print @DYSqlQuery
--EXECUTE sp_executesql @DYSqlQuery;
Returns -- 注意列是有序的。这是通过 Row_Number()
零填充完成的
SELECT prgmg_product_id,sourceID01,sourceID02,sourceID03,sourceID04,sourceID05,sourceID06,sourceID07,sourceID08,sourceID09,sourceID10 FROM (
SELECT prgmg_product_id
,CAST(N'sourceID' + CAST(ROW_NUMBER() OVER (
PARTITION BY prgmg_product_id ORDER BY prgmg_product_id, source_id_other
) AS NVARCHAR(10)) AS NVARCHAR(100)) AS Col
,source_id_other
FROM #Prgmg S1
) X
PIVOT(MIN(source_id_other) FOR Col IN (sourceID01,sourceID02,sourceID03,sourceID04,sourceID05,sourceID06,sourceID07,sourceID08,sourceID09,sourceID10)) P
我有一个动态 select 查询,它应该获取列作为 col1
、col2
......col9
、col10
但是它错误地提取为 col1
、col10
、col11
、col12
、col2
... 不确定如何将它们 select 作为查询是动态的。
请帮忙。
这是我一直在用的动态select。
-- CREATE THE COLUMNS REQUIRED
SET @DYColumns = STUFF(( SELECT DISTINCT
',' + N'sourceID'
+ CAST(ROW_NUMBER()
OVER (PARTITION BY prgmg_product_id ORDER BY prgmg_product_id, source_id_other)
AS NVARCHAR(10))
FROM #Prgmg FOR XML PATH('') ), 1, 1, '');
如果您使用 COALESCE 或 STUFF 来构建列,请确保有一个 ORDER by
此外,你可以有col01,col02,..col10来确保顺序
Drop Table #Prgmg
CREATE TABLE #Prgmg (
prgmg_product_id INT
,source_id_other INT
);
INSERT #Prgmg (
prgmg_product_id
,source_id_other
)
VALUES (3310,11478)
,(3337,10833)
,(3354,11466)
,(4039,4846)
,(4039,65454)
,(4039,65456)
,(13337,110833) -- Added to force over 10
,(13354,111466) -- Added to force over 10
,(14039,14846) -- Added to force over 10
,(14039,165454); -- Added to force over 10
DECLARE @DYColumns NVARCHAR(1000)
,@DYSqlQuery NVARCHAR(4000);
-- CREATE THE COLUMNS REQUIRED
SET @DYColumns = STUFF((
SELECT DISTINCT ','
+ N'sourceID'
+ right('00'+CAST(ROW_NUMBER() OVER (ORDER BY prgmg_product_id, source_id_other) AS NVARCHAR(10)),2)
FROM #Prgmg
FOR XML PATH('')
), 1, 1, '');
-- CREATE THE DYNAMIC SQL AND ADD IN THE CREATED COLUMNS
SET @DYSqlQuery = '
SELECT prgmg_product_id,'
+ @DYColumns
+ ' FROM (
SELECT prgmg_product_id
,CAST(N''sourceID'' + CAST(ROW_NUMBER() OVER (
PARTITION BY prgmg_product_id ORDER BY prgmg_product_id, source_id_other
) AS NVARCHAR(10)) AS NVARCHAR(100)) AS Col
,source_id_other
FROM #Prgmg S1
) X
PIVOT(MIN(source_id_other) FOR Col IN (' + @DYColumns + ')) P'
Print @DYSqlQuery
--EXECUTE sp_executesql @DYSqlQuery;
Returns -- 注意列是有序的。这是通过 Row_Number()
零填充完成的SELECT prgmg_product_id,sourceID01,sourceID02,sourceID03,sourceID04,sourceID05,sourceID06,sourceID07,sourceID08,sourceID09,sourceID10 FROM (
SELECT prgmg_product_id
,CAST(N'sourceID' + CAST(ROW_NUMBER() OVER (
PARTITION BY prgmg_product_id ORDER BY prgmg_product_id, source_id_other
) AS NVARCHAR(10)) AS NVARCHAR(100)) AS Col
,source_id_other
FROM #Prgmg S1
) X
PIVOT(MIN(source_id_other) FOR Col IN (sourceID01,sourceID02,sourceID03,sourceID04,sourceID05,sourceID06,sourceID07,sourceID08,sourceID09,sourceID10)) P