简易代码之家

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  157 Posts :: 2 Stories :: 57 Comments :: 0 Trackbacks

数据表结构及数据:


要统计的报表格式:


SQL语句:

1.只统计最右边的合计人数:
select t.addr,
      sum( case when(t.type='0') then 1 else 0 end ) as "甲流人数",
      sum( case when(t.type='1') then 1 else 0 end ) as "流感人数",
      sum( case when(t.type='2') then 1 else 0 end ) as "它病人数",
      count(*) as "合计人数"
from test t
group by t.addr;
2.最右边和下边的合计都统计:
(select t.addr as "区域",
      sum( case when(t.type='0') then 1 else 0 end ) as "甲流人数",
      sum( case when(t.type='1') then 1 else 0 end ) as "流感人数",
      sum( case when(t.type='2') then 1 else 0 end ) as "它病户数",
      count(*) as "合计人数"
from test t
group by t.addr)
union
(select null, sum( case when(t.type='0') then 1 else 0 end ),
      sum( case when(t.type='1') then 1 else 0 end ),
      sum( case when(t.type='2') then 1 else 0 end ),
      count(*)
from test t);

 

posted on 2009-11-13 15:12 Jakin.zhou 阅读(458) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: