【问题标题】:Need T-SQL pivot with dynamic date range counting occurrence需要具有动态日期范围计数发生的 T-SQL 数据透视
【发布时间】:2018-10-16 22:07:55
【问题描述】:

我需要关于 T-SQL 中枢轴的帮助,但我无法完成。

这些是要求:

  1. 需要将 Startdate 和 EndDate 定义为“yyyy-MM”
  2. 按月汇总
  3. 每个月只能计算一个“ClientID”(参见 2018-05 中的“A”)

我有这张带有样本数据的表格:

ClientID    Model   LastSyncTime_DW
---------------------------------------------
1           A       2018-09-26 00:00:00.000
2           A       2018-09-05 00:00:00.000
1           A       2018-08-19 00:00:00.000
1           A       2018-07-25 00:00:00.000
3           B       2018-07-03 00:00:00.000
1           A       2018-06-10 00:00:00.000
3           B       2018-06-07 00:00:00.000
8           A       2018-06-01 00:00:00.000
1           A       2018-05-31 00:00:00.000 (duplicate ClientID for this month - count only one)
3           B       2018-05-29 00:00:00.000
4           C       2018-05-26 00:00:00.000
5           D       2018-05-25 00:00:00.000
6           C       2018-05-24 00:00:00.000
1           A       2018-05-19 00:00:00.000 (duplicate ClientID for this month - count only one)
7           D       2018-05-12 00:00:00.000
8           A       2018-05-09 00:00:00.000
9           A       2018-05-05 00:00:00.000

结果应该是这样的:

Model   2018-09 2018-08 2018-07 2018-06 2018-5
-----------------------------------------------
A       2       1       1       2       3
B                       1       1       
C                                       2
D                                       2

请帮忙!

【问题讨论】:

    标签: sql-server tsql pivot


    【解决方案1】:

    这是你想要的吗?

    select model,
           count(distinct case when LastSyncTime_DW >= '2018-09-01' and LastSyncTime_DW < '2018-10-01' then ClientID end) as cnt_201809,
           count(distinct case when LastSyncTime_DW >= '2018-08-01' and LastSyncTime_DW < '2018-10-01' then ClientID end) as cnt_201808,
           . . .
    from t
    group by model;
    

    与您的结果的主要区别在于它返回 0s 而不是 NULLs。

    【讨论】:

    • 感谢您的快速回复。到目前为止它可以工作,但我需要动态定义 startDate 和 endDate。需要此查询以使用 SQL Report Server 生成报告,其中 startDate 和 endDate 始终不同。所以“计数”部分必须是动态的。有什么想法吗?
    • @Fridolin 。 . .这需要动态 SQL。
    【解决方案2】:
        Create table #Pivot 
        (ClientID     int,Model  varchar(2),LastSyncTime_DW datetime)
    
        Insert into #Pivot Values   (1, 'A',    '2018-09-26'),
                            (2, 'A',    '2018-09-05'),
                            (1, 'A',    '2018-08-19'),
                            (1, 'A',    '2018-07-25'),
                            (3, 'B',    '2018-07-03'),
                            (1, 'A',    '2018-06-10'),
                            (3, 'B',    '2018-06-07'),
                            (8, 'A',    '2018-06-01'),
                            (1, 'A',    '2018-05-31'),
                            (3, 'B',    '2018-05-29'),
                            (4, 'C',    '2018-05-26'),
                            (5, 'D',    '2018-05-25'),
                            (6, 'C',    '2018-05-24'),
                            (1, 'A',    '2018-05-19'),
                            (7, 'D',    '2018-05-12'),
                            (8, 'A',    '2018-05-09'),
                            (9, 'A',    '2018-05-05')
    
    Select * from #Pivot
    
      select model,
       count(distinct case when LastSyncTime_DW >= '2018-09-01' and LastSyncTime_DW < '2018-10-01' then ClientID end) as cnt_201809,
       count(distinct case when LastSyncTime_DW >= '2018-08-01' and LastSyncTime_DW < '2018-09-01' then ClientID end) as cnt_201808,
       count(distinct case when LastSyncTime_DW >= '2018-07-01' and LastSyncTime_DW < '2018-08-01' then ClientID end) as cnt_201807,
       count(distinct case when LastSyncTime_DW >= '2018-06-01' and LastSyncTime_DW < '2018-07-01' then ClientID end) as cnt_201806,
       count(distinct case when LastSyncTime_DW >= '2018-05-01' and LastSyncTime_DW < '2018-06-01' then ClientID end) as cnt_201805
     from #Pivot
     group by model;
    

    你会得到以下结果

    【讨论】:

    • 感谢您的快速回复。到目前为止它可以工作,但我需要动态定义 startDate 和 endDate。需要此查询以使用 SQL Report Server 生成报告,其中 startDate 和 endDate 始终不同。所以“计数”部分必须是动态的。有什么想法吗?
    猜你喜欢
    • 2021-07-18
    • 2022-12-09
    • 2012-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多