比如下表(账目),第一列的“月”有1月,2月,3,4,5,。。,缺少3月的和11月的数据
-------------------------------------
------账目
月 支出科目 金额
1 业务费-------- 100
1 广告费-- 100
2 业务费-- 100
2 广告费-- 100
4 业务费-- 800
4 广告费-- 200
4 业务费-- 800
5 广告费-- 200
5 广告费-- 1000
6 广告费-- 100
7 业务费-- 800
7 广告费-- 200
8 业务费-- 800
9 广告费-- 200
9 广告费-- 1000
10 广告费-- 100
12 业务费-- 800
12 广告费-- 200
---------------------------------
select 月,sum(IIF(支出项目='业务费',金额,0)) as 业务费,sum(IIF(支出项目='广告费',金额,0)) as 广告费 from 账目 group by 月 "
--------------------------------------------
--以上语句只能求出已有的月份的金额汇总,不过我想让第一列能不能显示所的月份呢,就是1到12 个月所有的??,没有的月分数据为0 的那种汇总,如下月 业务费 广告费
1 1720 0
2 1896 0
3 0 0
4 3134 0
5 2120 0
6 14060 65
7 6917 0
8 4658.6 0
9 2551 0
10 5387 32
11 [color=#FF0000]0 0[/color]
12 3301 0
(数据是我乱填的)
看看各位高人有什么办法实现上面的结果,用SQL怎么写给本人指点一下,谢谢了!!!!!!!

解决方案 »

  1.   

    我上面的SQL语句的“支出项目”不对,应该为“支出科目”,还有下面的汇总的11月的汇总数据是0,0(第一次提问请各位见谅)
      

  2.   

    二楼你太聪明了,不过比你笨不了太多的我怎么会想不到这一点呢!
    我就是想不用补齐数据的方法,直接用SQL语句来实现(表不止一个,而且缺的月份不一样),如果其它高人不来分就指定给你了(因为你是第一个)。
      

  3.   

    生成一个1到12月的月份表,然后使用左连接,为空的显示为0, isnull(cnt , 0) 0.
      

  4.   

    select m.月 , isnull(n.业务费,0) 业务费 , isnull(广告费,0) 广告费 from
    (
    select 1 as 月 union all
    select 2 as 月 union all
    select 3 as 月 union all
    select 4 as 月 union all
    select 5 as 月 union all
    select 6 as 月 union all
    select 7 as 月 union all
    select 8 as 月 union all
    select 9 as 月 union all
    select 10 as 月 union all
    select 11 as 月 union all
    select 12 as 月 
    ) m
    left join
    (
    select 月,
      sum(case 支出科目 when '业务费' then 金额 else 0 end) [业务费],
      sum(case 支出科目 when '广告费' then 金额 else 0 end) [广告费]
    from 账目表
    group by 月
    ) n
    on m.月 = n.月
      

  5.   


    create table test(id1 int,id2 varchar(4), id3 int)insert test select 1,'ywf',100 union all
        select 1,'ggf',200 union all
        select 2,'ywf',300 union all
        select 2,'ggf',200 union all
        select 2,'ywf',300 union all
        select 4,'ywf',300 union all
        select 4,'ggf',200 union all
        select 4,'ywf',300 union all
        select 5,'ywf',300 union all
        select 5,'ggf',200 union all
        select 5,'ggf',300 union all
                select 6,'ywf',300 union all
        select 7,'ywf',300 union all
                select 7,'ggf',300 union all
        select 8,'ggf',800 union all
        select 9,'ggf',300 union all
        select 9,'ggf',800 union all
                select 10,'ywf',800 union all
        select 12,'ywf',600 union all
        select 12,'ggf',600 select id1,sum(
    case when id2='ywf' then id3 else 0 end),
            sum(
    case when id2 = 'ggf' then id3 else 0 end)
     from test group by id1
    union all
    select top 1 3,0,0 from test
    union all
    select top 1 11,0,0 from test
    order by id1
      

  6.   

    create table test(id1 int,id2 varchar(4), id3 int)insert test select 1,'ywf',100 union all
        select 1,'ggf',200 union all
        select 2,'ywf',300 union all
        select 2,'ggf',200 union all
        select 2,'ywf',300 union all
        select 4,'ywf',300 union all
        select 4,'ggf',200 union all
        select 4,'ywf',300 union all
        select 5,'ywf',300 union all
        select 5,'ggf',200 union all
        select 5,'ggf',300 union all
                select 6,'ywf',300 union all
        select 7,'ywf',300 union all
                select 7,'ggf',300 union all
        select 8,'ggf',800 union all
        select 9,'ggf',300 union all
        select 9,'ggf',800 union all
                select 10,'ywf',800 union all
        select 12,'ywf',600 union all
        select 12,'ggf',600 select id1,sum(
    case when id2='ywf' then id3 else 0 end) as 'ywf',
            sum(
    case when id2 = 'ggf' then id3 else 0 end) as 'ggf'
     from test group by id1
    union all
    select top 1 3,0,0 from test
    union all
    select top 1 11,0,0 from test
    order by id1
    drop table test
      

  7.   

    id1     ywf     ggf
    1 100 200
    2 600 200
    3 0 0
    4 600 200
    5 300 500
    6 300 0
    7 300 300
    8 0 800
    9 0 1100
    10 800 0
    11 0 0
    12 600 600
      

  8.   

    首先感谢dawugui,谢谢你的精彩的代码,还有谢谢FNO1468(,虽然你的代码不完全是我想要的,我的表所缺的月份不固定),学习了,谢谢大家了,各位辛苦了!!!