多年的数据。

时间:2016-09-08 15:21:43

标签: sql sql-server tsql grouping rollup

我需要在多年内同时显示以下结果。因此,我不想在2014年,2015年以及2016年运行此报告,而是希望在三年内运行一次(@startDate& @endDate)。不确定汇总是否有效?还是分组?用户希望比较多年的数据。谢谢!

{{1}}

2 个答案:

答案 0 :(得分:0)

根据需要在year(srv.srv_date) as srv_yearselectgroup by添加order by列:

select year(srv.srv_date) as srv_year, ct.srv_ct_label as service, count (distinct cln.cln_urn) as total
 from cw_domain dmn
inner join cw_service srv on srv.srv_dmn_fk=dmn.dmn_pk
inner join cw_client cln on cln.cln_pk=srv.srv_cln_fk
inner join cw_subservice sbs on sbs.sbs_pk=srv.srv_sbs_fk
inner join cw_service_category ct on ct.srv_ct_rpk=sbs.sbs_srv_ct_rfk
where
srv_ct_rpk in (
'001',
'002',
'004',
'025',
'040',
'003',
'026',
'017',
'016',
'034',
'041',
'042',
'018',
'029'
)
and srv.srv_date >=@startDate and srv.srv_date <=endDate
group by year(srv.srv_date), ct.srv_ct_label, srv_ct_rpk, srv.srv_date
order by year(srv.srv_date),
case srv_ct_rpk
when '001' then 1
when'040' then 2
when '003' then 3
when '025' then 4
when '002' then 5
when '004' then 6
when '041' then 7
when '026' then 8
when '017' then 9
when '034' then 10
when '042' then 11
when '018' then 12
when '029' then 13
end

答案 1 :(得分:0)

您似乎希望以横向方式比较和显示它们。 因此,我建议使用selfjoin,如下所示。

select * from
(select ... 
from tableA join tableB join ...
on ...
where srv.srv_date = 2014) as a
JOIN
(select ... 
from tableA join tableB join ...
on ...
where srv.srv_date = 2015) as b ON ...
JOIN
(select ... 
from tableA join tableB join ...
on ...
where srv.srv_date = 2016) as c ON ...