【问题标题】:TSQL - Iterate through list of dates, group data, and insert into tableTSQL - 遍历日期列表、分组数据并插入表中
【发布时间】:2017-03-31 13:00:24
【问题描述】:

所以我将数据导入到以下格式的表中。对于缺少的任何月份,假设 MM、App_Amt、ClaimCnt 和 UnqPtCnt 列为 0:

location|health_plan| plan_type | pcp |  MM  |App_Amt|ClaimCnt|UnqPtCnt|Date
SoJ     |BLUE SHIELD|    COM    | 2384|  18  | 0.00  |   0    |   0    |2014-01-01
SoJ     |BLUE SHIELD|    COM    | 2384|  20  |1430.08|   1    |   1    |2014-02-01
SoJ     |BLUE SHIELD|    COM    | 2384|  19  | 468.69|   4    |   3    |2014-03-01
SoJ     |BLUE SHIELD|    COM    | 2384|  19  | 184.12|   2    |   2    |2014-04-01
SoJ     |BLUE SHIELD|    COM    | 2384|  19  | 184.12|   2    |   2    |2014-05-01
SoJ     |BLUE SHIELD|    COM    | 2384|  15  |2321.79|   3    |   3    |2014-06-01
SoJ     |BLUE SHIELD|    COM    | 2384|  18  | 258.18|   2    |   2    |2014-07-01
SoJ     |BLUE SHIELD|    COM    | 2384|  22  | 258.18|   2    |   2    |2014-08-01
SoJ     |BLUE SHIELD|    COM    | 2384|  83  | 395.29|   3    |   2    |2014-09-01
SoJ     |BLUE SHIELD|    COM    | 2384|  28  | 258.18|   2    |   2    |2014-10-01
SoJ     |BLUE SHIELD|    COM    | 2384|  32  | 571.02|   4    |   4    |2014-11-01
SoJ     |BLUE SHIELD|    COM    | 2384|  14  | 258.18|   2    |   2    |2014-12-01    

我需要根据此数据集计算滚动/追踪 12 个月。我将取 MM 的平均值、App_Amt 的总和、ClaimCnt 的总和和 UnqPtCt 的总和,并按位置、health_plan、plan_type 和 PCP 分组,并且我有一个要迭代的日期范围列表:

我从源表本身获取此日期列表。它目前是过去 3 年的每月 1 日。下个月,数据将超过一个月。例如,现在它的最短日期为 2014 年 1 月 1 日,最长日期为 2016 年 11 月 1 日,但下个月的 4 月,最短日期为 02/01/2014,最长日期为 12/01/2016 .

declare @end_dt date
set @end_dt = dateadd(month,-4,getdate())

select distinct [date] as start_dt,DATEADD(month,11,[date]) end_dt from [dbo].[OON_Summary]

select * from @Rolling where end_dt <= @end_dt


 start_dt | end_dt
2014-01-01|2014-12-01
2014-02-01|2015-01-01
2014-03-01|2015-02-01
2014-04-01|2015-03-01
2014-05-01|2015-04-01
2014-06-01|2015-05-01
2014-07-01|2015-06-01
2014-08-01|2015-07-01
2014-09-01|2015-08-01
2014-10-01|2015-09-01
2014-11-01|2015-10-01
2014-12-01|2015-11-01
2015-01-01|2015-12-01
2015-02-01|2016-01-01
2015-03-01|2016-02-01
2015-04-01|2016-03-01
2015-05-01|2016-04-01
2015-06-01|2016-05-01
2015-07-01|2016-06-01
2015-08-01|2016-07-01
2015-09-01|2016-08-01
2015-10-01|2016-09-01
2015-11-01|2016-10-01
2015-12-01|2016-11-01

到目前为止,我有这个简单的查询,但它过于手动,我知道必须有更好的方法来自动化它。

declare @start_dt date 
declare @end_dt date 
set @start_dt = '2014-01-01'
set @end_dt = '2014-12-01'

insert into [dbo].[OON_Rolling12]
  SELECT 
  ,[location]
  ,[health plan]
  ,[plan_type]
  ,[pcp]
  ,AVG([MM]) [MM]
  ,SUM([App_Amt]) [App_Amt]
  ,SUM([ClaimCnt]) [ClaimCnt]
  ,SUM([UnqPtCnt]) UnqPtCnt
  ,@end_dt as AsOfDate
FROM [dbo].[OON_Summary]
WHERE [Date] >= @start_dt AND [Date] <= @end_dt
GROUP BY 
   [location]
  ,[health plan]
  ,[plan_type]
  ,[pcp]

运行一次查询将在下面的输出中生成第一行。本质上,我将 2014 年 1 月 1 日至 2014 年 12 月 1 日的所有数据分组,并将其称为 2014 年 12 月数据。如何遍历该日期列表并自动生成其余行?

location|health_plan| plan_type | pcp |  MM  |App_Amt|ClaimCnt|UnqPtCnt |AsOfDate
SoJ     |BLUE SHIELD|    COM    | 2384|  25  |6565.83|   27   |   25    |2014-12-01
SoJ     |BLUE SHIELD|    COM    | 2384|  24  |6565.83|   27   |   25    |2015-01-01
SoJ     |BLUE SHIELD|    COM    | 2384|  22  |5135.75|   26   |   24    |2015-02-01
SoJ     |BLUE SHIELD|    COM    | 2384|  20  |4689.06|   22   |   21    |2015-03-01
SoJ     |BLUE SHIELD|    COM    | 2384|  19  |4504.94|   20   |   19    |2015-04-01
SoJ     |BLUE SHIELD|    COM    | 2384|  17  |4320.82|   18   |   17    |2015-05-01
SoJ     |BLUE SHIELD|    COM    | 2384|  16  |1999.03|   15   |   14    |2015-06-01
SoJ     |BLUE SHIELD|    COM    | 2384|  14  |1740.85|   13   |   12    |2015-07-01
SoJ     |BLUE SHIELD|    COM    | 2384|  13  |1482.67|   11   |   10    |2015-08-01
SoJ     |BLUE SHIELD|    COM    | 2384|  6   |1087.38|    8   |   8     |2015-09-01
SoJ     |BLUE SHIELD|    COM    | 2384|  3   |829.20 |    6   |   6     |2015-10-01
SoJ     |BLUE SHIELD|    COM    | 2384|  0   |258.18 |    2   |   2     |2015-11-01    
SoJ     |BLUE SHIELD|    COM    | 2384|  0   | 0.00  |    0   |   0     |2015-12-01    

【问题讨论】:

  • 你使用的是哪个版本的sql server?开始日期和结束日期是否在单独的表格中?
  • 你能在小提琴中创建你的架构吗?
  • 我使用的是 2008 R2,日期来自源表。目前,源表列 [日期] 包含过去 3 年的每月 1 日。

标签: sql-server tsql


【解决方案1】:

您可以使用OUTER APPLY 获取过去 12 个月的汇总值。

SELECT 
   o1.[location]
  ,o1.[health plan]
  ,o1.[plan_type]
  ,o1.[pcp]
  ,o2.[MM]
  ,o2.[App_Amt]
  ,o2.[ClaimCnt]
  ,o2.[UnqPtCnt]
FROM [dbo].[OON_Summary] o1
OUTER APPLY (SELECT AVG([MM]) as [MM]
                   ,SUM([App_Amt]) as [App_Amt]
                   ,SUM([ClaimCnt]) as [ClaimCnt]
                   ,SUM([UnqPtCnt]) as UnqPtCnt
             FROM [dbo].[OON_Summary] o2
             WHERE o1.[location]=o2.[location] AND o1.[health plan]=o2.[health plan] 
             AND o1.[plan_type]=o2.[plan_type] AND o1.[pcp]=o2.[pcp]
             AND o2.[date] BETWEEN DATEADD(month,-11,o1.[date]) AND o1.[date]
             ) o2

这在带有窗口函数的 sql server 2012+ 版本中稍微容易一些。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-21
    • 2012-08-15
    • 2014-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多