【问题标题】:How can I get values of the Rating column together with their definition for every specific month by using Pivot with Month in MSSQL如何通过在 MSSQL 中使用 Pivot with Month 来获取每个特定月份的 Rating 列的值及其定义
【发布时间】:2020-02-17 10:01:44
【问题描述】:

例子:

数据:

GraphDetails
|----------------------------------------------------------|
| Id  GoalId  Definition                Rating  DateCrated |
| -------------------------------------------------------- |
| 1   2       Zero Infra Back Log       100     2020-02-05 |
| 2   2       Happy Customers           95      2020-02-05 |
| 3   2       Complete All Projects     100     2020-02-05 |
| 4   1       Zero Infra Back Log       100     2020-02-05 |
| 5   1       Happy Customers           98      2020-02-05 |
| 6   1       Complete All Projects     100     2020-02-05 |
|----------------------------------------------------------|

查询:

SELECT
 [1] AS Jan,
 [2] AS Feb,
 [3] AS Mar,
 [4] AS Apr,
 [5] AS May,
 [6] AS Jun,
 [7] AS Jul,
 [8] AS Aug,
 [9] AS Sep,
 [10] AS Oct,
 [11] AS Nov,
 [12] AS [Dec]
FROM
(Select Id, MONTH(DateRecorded) as TMonth
  FROM GraphDetails
    WHERE YEAR(DateRecorded) = 2020 AND GoalId = 1
    ) source
PIVOT
( Definition, Rating
    FOR TMonth
    IN ( [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12] ) 
) AS pvtMonth

我收到此错误:

',' 附近的语法不正确。

符合“定义、评级”

结果应如下所示:

|-------------------------------------------------------------------------------------------------|
| Definition              | Jan | Feb | Mar | Apr | May | Jun | Jul | Aug | Sep | Oct | Nov | Dec |
|-------------------------------------------------------------------------------------------------|
| Zero Infra Back Log     | 0   | 100 | 0   | 0   | 0   | 0   | 0   | 0   | 0   | 0   | 0   | 0   |
| Happy Customers         | 0   | 98  | 0   | 0   | 0   | 0   | 0   | 0   | 0   | 0   | 0   | 0   |
| Complete All Projects   | 0   | 100 | 0   | 0   | 0   | 0   | 0   | 0   | 0   | 0   | 0   | 0   |
|-------------------------------------------------------------------------------------------------|

这可能吗? 任何帮助,请!

谢谢!

*以上代码基于PIVOT with MONTH()

【问题讨论】:

  • 您正在尝试PIVOT 2 列:PIVOT ( Definition, Rating...PIVOT 运算符不支持超过 1 列。看起来你会更好Cross-Tab

标签: sql sql-server tsql group-by pivot


【解决方案1】:

你可以只使用条件聚合:

select 
    definition,
    sum(case 
        when dateRecorded >= datefromparts(2020, 1, 1) and dateRecorded < datefromparts(2020, 2, 1)
        then rating
        else 0
    end) Jan,
    sum(case 
        when dateRecorded >= datefromparts(2020, 2, 1) and dateRecorded < datefromparts(2020, 3, 1)
        then rating
        else 0
    end) Feb,
    ...
    sum(case 
        when dateRecorded >= datefromparts(2020, 12, 1) and dateRecorded < datefromparts(2021, 1, 1)
        then rating
        else 0
    end) Dec
from graphDetails
where dateRecorded >= datefromparts(2020, 1, 1) and dateRecorded < datefromparts(2021, 1, 1)
group by definition
order by definition

这种语法在某种程度上比特定的PIVOT 运算符更灵活(而且,值得一提的是,它适用于不同的数据库产品)。

请注意,我修改了日期过滤器,因此它使用半开间隔,并且没有对列 dateRecorded 应用日期函数:这应该允许数据库利用日期列上的索引(并且还会顺利处理dateCreated的时间分量,如果有的话)。

【讨论】:

  • 非常感谢!但为什么是唯一的Jan |二月 |出现了当月的 12 月。是否可以显示从一月到十二月(一月 | 二月 | 三月 | 四月 | 五月 | 六月 | 七月 | 八月 | 九月 | 十月 | 十一月 | 十二月)?谢谢!
  • @TonithAlbiso:当然。您只需要使用更多条件表达式扩展select 子句。为了简洁起见,我只是演示了 1 月、2 月和 12 月的解决方案(中间有 ... 供您填写)。
  • 完美!我得到了它。有用!我感谢您的帮助。我花了两个星期才解决这个问题,现在我只用了几分钟就搞定了。哇!。非常感谢! @GMB
  • @GMB 。 . .我很好奇你为什么要使用datefromparts(2020, 1, 1) 而不是'2020-01-01''20200101'(前者是ISO 标准,适用于大多数数据库,后者保证适用于SQL Server)。
【解决方案2】:

查询:

SELECT [Definition], isnull([1],0) AS Jan,
         isnull([2],0) AS Feb,
         isnull([3],0) AS Mar,
         isnull([4],0) AS Apr,
         isnull([5],0) AS May,
         isnull([6],0) AS Jun,
         isnull([7],0) AS Jul,
         isnull([8],0) AS Aug,
         isnull([9],0) AS Sep,
         isnull([10],0) AS Oct,
         isnull([11],0) AS Nov,
         isnull([12],0) AS [Dec]
        FROM
       (Select Id, MONTH(DateCrated) as TMonth,[Definition],Rating
          FROM GraphDetails
       WHERE YEAR(DateCrated) = 2020 AND GoalId = 1
        ) [source]
    PIVOT
    (  
    sum(Rating)
        FOR TMonth 
        IN ( [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12] ) 
    ) AS pvtMonth

Result :

【讨论】:

    猜你喜欢
    • 2020-03-24
    • 2012-03-22
    • 1970-01-01
    • 1970-01-01
    • 2018-12-23
    • 2023-03-16
    • 2017-12-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多