SQL统计报表中1-12月的数据

  1. ----------------------------
  2. --Author:旋风
  3. --Date:2008-09-11 
  4. --Version:V1.0
  5. --Memo:SQL统计报表中1-12月的数据
  6. ----------------------------
  7. --> 测试数据: [Accident_Report]
  8. if object_id('[Accident_Report]'is not null drop table [Accident_Report]
  9. create table [Accident_Report] ([unitid] int,[DeathNumber] int,[GrievousNumber] int,[WoundNumber] int,[LoseMoney] int,[LoseDate] int,[Date] datetime)
  10. insert into [Accident_Report]
  11. select 1,3,2,4,5,1,'2008-3-3' union all
  12. select 1,3,2,4,5,1,'2008-3-8' union all
  13. select 1,5,7,4,5,1,'2008-3-5' union all
  14. select 5,3,2,4,5,1,'2008-3-4' union all
  15. select 4,3,2,4,5,1,'2008-8-3' union all
  16. select 2,7,1,6,5,3,'2008-8-5' union all
  17. select 2,7,1,6,5,3,'2008-8-3' union all
  18. select 3,4,2,4,9,5,'2008-9-3'
  19. --> 测试数据: [Systemmanagement_Department]
  20. if object_id('[Systemmanagement_Department]'is not null drop table [Systemmanagement_Department]
  21. create table [Systemmanagement_Department] ([UnitId] int,[UnitName] varchar(5))
  22. insert into [Systemmanagement_Department]
  23. select 1,'unit1' union all
  24. select 2,'unit2' union all
  25. select 3,'unit3'  union all
  26. select 4,'unit4'  union all
  27. select 5,'unit5'  
  28. --select * from [Accident_Report]
  29. --select * from [Systemmanagement_Department]
  30. SELECT 
  31. SUM(isnull(a.DeathNumber,0)) AS M1, 
  32. SUM(isnull(a.GrievousNumber,0)) AS M2,
  33. SUM(isnull(a.WoundNumber,0)) AS M3, 
  34. SUM(isnull(a.LoseMoney,0)) AS M4,
  35. SUM(isnull(a.LoseDate,0))  AS M5,
  36. COUNT(*) AS M9, 
  37. SUM(isnull(DeathNumber,0)) AS qq,
  38. MONTH(a.Date) AS Month, 
  39. STR(MONTH(a.Date)) + '月' AS strMonth, 
  40. YEAR(a.Date) AS Year, 
  41. STR(YEAR(a.Date)) + '年' AS strYear, 
  42. a.UnitId,b.UnitName,
  43. a.tmonth
  44. FROM 
  45. (
  46. select * from [Accident_Report] m right join
  47. (
  48. select '1' as tmonth  union all select '2' union all select '3' union all
  49. select '4' union all select '5' union all select '6' union all
  50. select '7' union all select '8' union all select '9' union all
  51. select '10' union all select '11' union all select '12' 
  52. )  n --月份临时表
  53. on MONTH(m.Date)=n.tmonth
  54. ) a
  55. LEFT JOIN  Systemmanagement_Department b 
  56. ON  a.UnitId = b.UnitId 
  57. GROUP BY a.tmonth,month(a.date), a.UnitId, 
  58.       b.UnitName, YEAR(a.Date)
  59. --删除测试
  60. drop table Accident_Report
  61. drop table Systemmanagement_Department
  62. /*
  63. M1          M2          M3          M4          M5          M9          qq          Month       strMonth     Year        strYear      UnitId      UnitName tmonth
  64. ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ------------ ----------- ------------ ----------- -------- ------
  65. 0           0           0           0           0           1           0           NULL        NULL         NULL        NULL         NULL        NULL     1
  66. 0           0           0           0           0           1           0           NULL        NULL         NULL        NULL         NULL        NULL     10
  67. 0           0           0           0           0           1           0           NULL        NULL         NULL        NULL         NULL        NULL     11
  68. 0           0           0           0           0           1           0           NULL        NULL         NULL        NULL         NULL        NULL     12
  69. 0           0           0           0           0           1           0           NULL        NULL         NULL        NULL         NULL        NULL     2
  70. 11          11          12          15          3           3           11          3                    3月  2008              2008年  1           unit1    3
  71. 3           2           4           5           1           1           3           3                    3月  2008              2008年  5           unit5    3
  72. 0           0           0           0           0           1           0           NULL        NULL         NULL        NULL         NULL        NULL     4
  73. 0           0           0           0           0           1           0           NULL        NULL         NULL        NULL         NULL        NULL     5
  74. 0           0           0           0           0           1           0           NULL        NULL         NULL        NULL         NULL        NULL     6
  75. 0           0           0           0           0           1           0           NULL        NULL         NULL        NULL         NULL        NULL     7
  76. 14          2           12          10          6           2           14          8                    8月  2008              2008年  2           unit2    8
  77. 3           2           4           5           1           1           3           8                    8月  2008              2008年  4           unit4    8
  78. 4           2           4           9           5           1           4           9                    9月  2008              2008年  3           unit3    9
  79. */
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值