我需要订购动态查询的输出,将列转换为行,以确保在没有值的行之前只生成具有值的列。如何强制生成列的顺序,以便第一列是包含特定字段值的列,而在行的末尾生成没有特定字段值的列?
insert into #PriceSheet
select ID, ProductID,SheetNumber ,SheetDesc ,MfgPriceCode,PriceZone
FROM ProductPrice
left join UNITS On SUOM = UOMID
select @colsUnpivot = stuff((select ','+quotename(C.name)
from tempdb.sys.columns as C
where C.object_id = object_id('tempdb..#PricingSheet') and
C.name LIKE '%%'
for xml path('')), 1, 1, '')
select @colsPivot = STUFF((SELECT ','
+ quotename(c.name
+ cast(t.rn as varchar(10)))
from
(
select row_number() over(partition by ProductID
order by ProductID) rn
from #PriceSheet
) t
cross apply
tempdb.sys.columns as C
where C.object_id = object_id('tempdb..#PricingSheet')
and C.name Not in ('CreateDate', 'LastModifiedDate')
group by c.name, t.rn
order by t.rn
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set @query = 'select *
from
(
select ProductID, col + cast(rn as varchar(10)) new_col, val
from
(
select
cast(ProductID as varchar(50))ProductID
,cast(SheetNumber as varchar(50))SheetNumber
,cast(SheetDesc as varchar(50))SheetDesc
,cast(MfgPriceCode as varchar(50))MfgPriceCode
,cast(PriceZone as varchar(50))PriceZone
row_number() over(partition by productid order by productid) rn
from #PriceSheet
) x
unpivot
(
val
for col in ('+ @colsunpivot +')
) u
) x1
pivot
(
max(val)
for new_col in
('+ @colspivot +')
) p'
比如说,首先为SheetNumber不等于NULL的行生成列,然后再生成NULLS。
答案 0 :(得分:0)
select *
from
(
select ProductID,
col + cast(rn as varchar(10)) new_col,
val
from
(
select
Cast (ProductID as NVarchar(3072)) ProductID
,Cast (SheetNumber as NVarchar(3072)) SheetNumber
,Cast (SheetDesc as NVarchar(3072)) SheetDesc
,Cast (MfgPriceCode as NVarchar(3072)) MfgPriceCode
,Cast (PriceZone as NVarchar(3072)) PriceZone
,row_number() over(partition by ProductID order by Case When SheetNumber Is Null Then 1 Else 0 End, ProductID) rn
from MainPricesheet
) x
unpivot
(
val
for col in ([SheetNumber],[SheetDesc],[MfgPriceCode],[PriceZone])
) u
) x1
pivot
(
max(val)
for new_col in
([SheetNumber1],[SheetDesc1],[MfgPriceCode1],[PriceZone1],
[SheetNumber2],[SheetDesc2],[MfgPriceCode2],[PriceZone2],
[SheetNumber3],[SheetDesc3],[MfgPriceCode3],[PriceZone3],
[SheetNumber4],[SheetDesc4],[MfgPriceCode4],[PriceZone4],
[SheetNumber5],[SheetDesc5],[MfgPriceCode5],[PriceZone5],
[SheetNumber6],[SheetDesc6],[MfgPriceCode6],[PriceZone6],
[SheetNumber7],[SheetDesc7],[MfgPriceCode7],[PriceZone7],
[SheetNumber8],[SheetDesc8],[MfgPriceCode8],[PriceZone8],
[SheetNumber9],[SheetDesc9],[MfgPriceCode9],[PriceZone9],
[SheetNumber10],[SheetDesc10],[MfgPriceCode10],[PriceZone10],
[SheetNumber11],[SheetDesc11],[MfgPriceCode11],[PriceZone11],
[SheetNumber12],[SheetDesc12],[MfgPriceCode12],[PriceZone12],
[SheetNumber13],[SheetDesc13],[MfgPriceCode13],[PriceZone13],
[SheetNumber14],[SheetDesc14],[MfgPriceCode14],[PriceZone14],
[SheetNumber15],[SheetDesc15],[MfgPriceCode15],[PriceZone15],
[SheetNumber16],[SheetDesc16],[MfgPriceCode16],[PriceZone16],
[SheetNumber17],[SheetDesc17],[MfgPriceCode17],[PriceZone17],
[SheetNumber18],[SheetDesc18],[MfgPriceCode18],[PriceZone18],
[SheetNumber19],[SheetDesc19],[MfgPriceCode19],[PriceZone19],
[SheetNumber20],[SheetDesc20],[MfgPriceCode20],[PriceZone20],
[SheetNumber21],[SheetDesc21],[MfgPriceCode21],[PriceZone21])
) p