2

列を行に変換する動的クエリの出力を並べ替えて、値のない行の前に値のある列のみが生成されるようにする必要があります。最初の列が特定のフィールドの値を含む列になり、特定のフィールドの値を持たない列が行の最後に生成されるように、列の生成時に順序を強制するにはどうすればよいですか?

             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 を生成するとします。

4

1 に答える 1

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 
于 2012-10-26T02:43:07.393 に答える