sql >> データベース >  >> RDS >> Sqlserver

列SQLを動的に作成する

    PIVOTを使用することをお勧めします このための機能。既知の列数がある場合は、値をハードコーディングできます。

    select name, [Bronze], [Silver], [Gold], [Platinum], [AnotherOne]
    from
    (
      select c.name,
        cr.description,
        r.typeid
      from customers c
      left join rewards r
        on c.id = r.customerid
      left join customerrewards cr
        on r.typeid = cr.typeid
    ) x
    pivot
    (
      count(typeid)
      for description in ([Bronze], [Silver], [Gold], [Platinum], [AnotherOne])
    ) p;
    

    SQL FiddlewithDemoを参照してください。

    これで、列の数が不明な場合は、動的SQLを使用してPIVOTを実行できます。 :

    DECLARE @cols AS NVARCHAR(MAX),
        @query  AS NVARCHAR(MAX)
    
    select @cols = STUFF((SELECT ',' + QUOTENAME(description) 
                        from customerrewards
                        group by description, typeid
                        order by typeid
                FOR XML PATH(''), TYPE
                ).value('.', 'NVARCHAR(MAX)') 
            ,1,1,'')
    
    set @query = 'SELECT name,' + @cols + ' from 
                 (
                    select c.name,
                      cr.description,
                      r.typeid
                    from customers c
                    left join rewards r
                      on c.id = r.customerid
                    left join customerrewards cr
                      on r.typeid = cr.typeid
                ) x
                pivot 
                (
                    count(typeid)
                    for description in (' + @cols + ')
                ) p '
    
    execute(@query)
    

    SQL Fiddle With Demo

    を参照してください

    Totalを含める必要がある場合 列の場合、ROLLUPを使用できます (静的バージョンのデモ):

    select name, sum([Bronze]) Bronze, sum([Silver]) Silver, 
      sum([Gold]) Gold, sum([Platinum]) Platinum, sum([AnotherOne]) AnotherOne
    from 
    (
      select name, [Bronze], [Silver], [Gold], [Platinum], [AnotherOne]
      from
      (
        select c.name,
          cr.description,
          r.typeid
        from customers c
        left join rewards r
          on c.id = r.customerid
        left join customerrewards cr
          on r.typeid = cr.typeid
      ) x
      pivot
      (
        count(typeid)
        for description in ([Bronze], [Silver], [Gold], [Platinum], [AnotherOne])
      ) p
    ) x
    group by name with rollup
    

    動的バージョン(デモ):

    DECLARE @cols AS NVARCHAR(MAX),
        @colsRollup AS NVARCHAR(MAX),
        @query  AS NVARCHAR(MAX)
    
    select @cols = STUFF((SELECT ',' + QUOTENAME(description) 
                        from customerrewards
                        group by description, typeid
                        order by typeid
                FOR XML PATH(''), TYPE
                ).value('.', 'NVARCHAR(MAX)') 
            ,1,1,'')
    
    select @colsRollup 
          = STUFF((SELECT ', Sum(' + QUOTENAME(description) + ') as '+ QUOTENAME(description)
                        from customerrewards
                        group by description, typeid
                        order by typeid
                FOR XML PATH(''), TYPE
                ).value('.', 'NVARCHAR(MAX)') 
            ,1,1,'')
    
    
    set @query 
              = 'SELECT name, '+ @colsRollup + '
                 FROM
                 (
                    SELECT name,' + @cols + ' from 
                     (
                        select c.name,
                          cr.description,
                          r.typeid
                        from customers c
                        left join rewards r
                          on c.id = r.customerid
                        left join customerrewards cr
                          on r.typeid = cr.typeid
                    ) x
                    pivot 
                    (
                        count(typeid)
                        for description in (' + @cols + ')
                    ) p 
                  ) x1
                  GROUP BY name with ROLLUP'
    
    execute(@query)
    



    1. Oracleで月の最終日を取得する方法

    2. TNSPINGはOKですが、sqlplusはORA-12154を提供しますか?

    3. MySQLテーブル列に名前を付けるために番号を使用できますか?

    4. SQLServerで月末を見つける方法