【发布时间】:2019-01-04 10:01:21
【问题描述】:
我有以下 sql 代码(where 子句只是为了限制当前的行数)
select
month,
monthname,
year,
count(distinct case when a.dim_service_type_id_desc like '%Direct Payment%' then a.DIM_PERSON_ID else null end) as No_dp,
count(distinct a.DIM_PERSON_ID) as no_ppl
from
SERVICE_PROVISIONS a
inner join date_tbl d on CONVERT(VARCHAR(35),a.start_dttm,112) = d.dim_date_id
where
a.dim_person_id >0
and year = 2018
group by
month,
monthname,
year
我的输出是这样的
month monthname year No_dp no_ppl
1 January 2018 142 1604
2 February 2018 111 1526
3 March 2018 133 1636
4 April 2018 1107 3829
5 May 2018 140 1575
6 June 2018 131 1389
7 July 2018 200 893
8 August 2018 2 73
9 September 2018 1 32
10 October 2018 2 21
11 November 2018 2 21
12 December 2018 2 19
所以我的问题是 - 客户想查看在过去 12 个月内有多少服务处于打开状态(使用开始日期和结束日期)(不是已启动的服务数量,而是当前未结束的服务数量)。这在使用当前月份时很好,但是他们也希望将前 12 个月的数据显示为滚动动态数据。
因此,例如本月 7 月,他们想查看过去 12 个月内有多少服务处于开放状态。上个月 6 月,他们想查看在 6 月之前的 12 个月中有多少服务是开放的,以此类推之前的 12 个月。
表格需要包含过去 12 个月的月份名称,并在列中显示该月旁边的过去 12 个月中打开的服务数量。
我希望这是有道理的,如果没有,对不起,请随时提出问题,我会尽力澄清。
输出需要看起来像当前的输出表,但它目前只显示该月内启动了多少服务,这不是我们想要的。
日期表是一个参考表,具有不同的日期格式等。如果需要,可以使用或添加。
【问题讨论】:
-
请务必在问题中包含预期的输出
标签: sql sql-server sql-server-2016