【问题标题】:SQL using PIVOT rightSQL 使用 PIVOT 权限
【发布时间】:2015-12-21 21:38:37
【问题描述】:

我有一个问题,我试图在我的查询中使用 PIVOT,但没有任何结果。现在我有一个看起来像这样的表:

Category     Month    Value
A            August     10
B            August     19
C            August     15
A            September  20
B            September  23
C            September  25
A            October    24
B            October    87
C            October    44

我想让这样看:

Category      August       September    October
      A       10             20          24
      B       19             23          87
      C       15             25          47

在我的选择中是这样的:

Select cat_name, CAST(month AS VARCHAR(20)), value from dbo.table1.

_

      select * from (
        select ft.categoryData as [category], CAST(fft.date AS VARCHAR(20)) as [month], tt.value as [value] from firstt ft
                join secondt st on ft.id = st.id
                join thirdt tt on ft.id = tt.type_id
                join fourtht fft on ft.id = fft.category_id
            where ft.date between '2015-07-01' and '2015-09-01' and ft.country = 'EUR'  
group by fft.date, ft.categoryData, tt.value
                ) as t

        PIVOT (

    max(value)
    for [date] in ([jul], [aug], [sept])


      ) as pvt

【问题讨论】:

    标签: sql sql-server pivot


    【解决方案1】:

    通过使用pivot,我们可以在下面编写查询,语法如下链接 如前所述

    https://www.simple-talk.com/sql/t-sql-programming/questions-about-pivoting-data-in-sql-server-you-were-too-shy-to-ask/

    SELECT *
    FROM TABLE_NAME
    PIVOT(MAX(VALUE) FOR MONTH IN (
                [AUGUST]
                ,[SEPTEMBER]
                ,[OCTOBER]
                )) AS
    PIVOT_SALES
    

    输出是

    >

    【讨论】:

    • 你能告诉我负分的正当理由吗
    【解决方案2】:

    您可以使用以下代码

    SELECT * from (SELECT Category, Month, Value 
    FROM table1 t) pivot (sum (Value) for Month in ('August', 'September', October') ) order by Category;
    

    【讨论】:

      【解决方案3】:

      试试Conditional Aggregate

      select Category,
             max(case when Month = 'August' then Value END) as August,
             max(case when Month = 'September' then Value END) as September,
             max(case when Month = 'October' then Value END) as October
      from Yourtable
      Group by Category
      

      或使用Pivot

      select * 
      from Yourtable
      pivot (max(Value) For Month in ([August],[September],[October]))pv
      

      当月份列中的值未知时使用动态sql

       declare @sql nvarchar(max)= '',
               @col_list varchar(max)=''
      
       set @col_list =(select distinct quotename(Month)+',' from yourtable for xml path (''))
       set @col_list  = left(@col_list,len(@col_list)-1)
       set @sql  = '
          select * 
          from yourtable
          pivot (max(Value) For Month in ('+@col_list+'))pv'
      
          exec sp_executesql @sql
      

      【讨论】:

      • 亲爱的 VR46,我更新了我的问题,我就是这样做的,关于你的回答,但值是 NULL
      • 亲爱的 VR46,现在我正在尝试制作您创建的动态版本,但问题是我有像别名 t 这样的表,当我 set @col_list 时,它无法识别表t。怎么解决???
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多