当前位置:   article > 正文

sql 报表:_sql报表

sql报表

要求在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

最后效果是每个地区对应日期的抄水表完成情况


第一条语句:

  1. select (case
  2. WHEN Meter_Time ='2018-07-01' then '2018-07-01'
  3. WHEN Meter_Time ='2018-07-02' then '2018-07-02'
  4. WHEN Meter_Time ='2018-07-03' then '2018-07-03'
  5. end )as 区间,
  6. Should_Meter, Actual_Meter
  7. from C_Weater
  8. group by Meter_Time, Father_Level, Should_Meter, Actual_Meter

结果:


第二条语句:

  1. select 区间,
  2. case when Father_Level =1 then sum(Should_Meter) else 0 end should_1,
  3. case when Father_Level =1 then sum(Actual_Meter) else 0 end Actual_1,
  4. case when Father_Level =2 then sum(Should_Meter) else 0 end should_2,
  5. case when Father_Level =2 then sum(Actual_Meter) else 0 end Actual_2,
  6. case when Father_Level =3 then sum(Should_Meter) else 0 end should_3,
  7. case when Father_Level =3 then sum(Actual_Meter) else 0 end Actual_3
  8. from
  9. (select (case
  10. WHEN Meter_Time ='2018-07-01' then '2018-07-01'
  11. WHEN Meter_Time ='2018-07-02' then '2018-07-02'
  12. WHEN Meter_Time ='2018-07-03' then '2018-07-03'
  13. end )as 区间,
  14. Should_Meter, Actual_Meter,Father_Level
  15. from
  16. C_Weater
  17. group by Meter_Time, Father_Level, Should_Meter, Actual_Meter) AS T
  18. group by 区间,Father_Level

结果:


第三条:

  1. select 区间,
  2. sum(should_1) as 应抄1,sum(Actual_1)as 实抄1,
  3. sum(should_2) as 应抄2,sum(Actual_2)as 实抄2,
  4. sum(should_3) as 应抄3,sum(Actual_3)as 实抄3
  5. FROM
  6. (
  7. select 区间,
  8. case when Father_Level =1 then sum(Should_Meter) else 0 end should_1,
  9. case when Father_Level =1 then sum(Actual_Meter) else 0 end Actual_1,
  10. case when Father_Level =2 then sum(Should_Meter) else 0 end should_2,
  11. case when Father_Level =2 then sum(Actual_Meter) else 0 end Actual_2,
  12. case when Father_Level =3 then sum(Should_Meter) else 0 end should_3,
  13. case when Father_Level =3 then sum(Actual_Meter) else 0 end Actual_3
  14. from
  15. (select (case
  16. WHEN Meter_Time ='2018-07-01' then '2018-07-01'
  17. WHEN Meter_Time ='2018-07-02' then '2018-07-02'
  18. WHEN Meter_Time ='2018-07-03' then '2018-07-03'
  19. end )as 区间,
  20. Should_Meter, Actual_Meter,Father_Level
  21. from
  22. C_Weater
  23. group by Meter_Time, Father_Level, Should_Meter, Actual_Meter) AS T
  24. group by 区间,Father_Level) as p
  25. group by 区间

结果:




声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/423569
推荐阅读
相关标签
  

闽ICP备14008679号