赞
踩
要求在web上以bootsrap 画出 报表:
光是sql语句我就晕了
类似如下的效果:
有幸看到大神的博客:https://www.2cto.com/database/201712/704724.html
我的数据库: sql server 2012
表准备:以Meter_Time作为纵向条件 ,Father_Leval 作为横向条件
解释一下
: Meter_Time 为抄水表日期
Should_Meter 为当天应该抄多少个水表
Actual_Meter 为当天实际抄的水表数
Father_Level : 当前公司所属地区 : 地区1,地区2,地区3
最后效果是每个地区对应日期的抄水表完成情况
第一条语句:
- select (case
- WHEN Meter_Time ='2018-07-01' then '2018-07-01'
- WHEN Meter_Time ='2018-07-02' then '2018-07-02'
- WHEN Meter_Time ='2018-07-03' then '2018-07-03'
- end )as 区间,
- Should_Meter, Actual_Meter
- from C_Weater
- group by Meter_Time, Father_Level, Should_Meter, Actual_Meter
结果:
第二条语句:
- select 区间,
- case when Father_Level =1 then sum(Should_Meter) else 0 end should_1,
- case when Father_Level =1 then sum(Actual_Meter) else 0 end Actual_1,
- case when Father_Level =2 then sum(Should_Meter) else 0 end should_2,
- case when Father_Level =2 then sum(Actual_Meter) else 0 end Actual_2,
- case when Father_Level =3 then sum(Should_Meter) else 0 end should_3,
-
-
- case when Father_Level =3 then sum(Actual_Meter) else 0 end Actual_3
- from
- (select (case
- WHEN Meter_Time ='2018-07-01' then '2018-07-01'
- WHEN Meter_Time ='2018-07-02' then '2018-07-02'
- WHEN Meter_Time ='2018-07-03' then '2018-07-03'
- end )as 区间,
- Should_Meter, Actual_Meter,Father_Level
- from
- C_Weater
- group by Meter_Time, Father_Level, Should_Meter, Actual_Meter) AS T
- group by 区间,Father_Level
结果:
第三条:
- select 区间,
- sum(should_1) as 应抄1,sum(Actual_1)as 实抄1,
- sum(should_2) as 应抄2,sum(Actual_2)as 实抄2,
- sum(should_3) as 应抄3,sum(Actual_3)as 实抄3
-
- FROM
- (
- select 区间,
- case when Father_Level =1 then sum(Should_Meter) else 0 end should_1,
- case when Father_Level =1 then sum(Actual_Meter) else 0 end Actual_1,
- case when Father_Level =2 then sum(Should_Meter) else 0 end should_2,
- case when Father_Level =2 then sum(Actual_Meter) else 0 end Actual_2,
- case when Father_Level =3 then sum(Should_Meter) else 0 end should_3,
- case when Father_Level =3 then sum(Actual_Meter) else 0 end Actual_3
- from
- (select (case
- WHEN Meter_Time ='2018-07-01' then '2018-07-01'
- WHEN Meter_Time ='2018-07-02' then '2018-07-02'
- WHEN Meter_Time ='2018-07-03' then '2018-07-03'
- end )as 区间,
- Should_Meter, Actual_Meter,Father_Level
- from
- C_Weater
- group by Meter_Time, Father_Level, Should_Meter, Actual_Meter) AS T
- group by 区间,Father_Level) as p
- group by 区间
结果:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。