居民生活 工业用水 事业单位 营业与基建 特种    餐饮业   年度    月份 
160.29 .00 .00 .00 .00 .00 2006 11
.00 18.33 .00 .00 .00 .00 2006 11
.00 .00 53.90 .00 .00 .00 2006 11
.00 .00 .00 63.60 .00 .00 2006 11
.00 .00 .00 .00 59.97 .00 2006 11
128.28 .00 .00 .00 .00 .00 2006 12
.00 11.28 .00 .00 .00 .00 2006 12
.00 .00 33.00 .00 .00 .00 2006 12
.00 .00 .00 34.50 .00 .00 2006 12
.00 .00 .00 .00 59.96 .00 2006 12要达到这样的效果:
160.29   18.33    53.90    63.60    59.97     00       2006     11
128.28   11.28    33.00    34.5     59.96     00       2006     12
第一个结果的查询语句是这样的:
select case sjbm when '001' then sum(sf) else 0 end as '居民生活用水',
       case sjbm when '002' then sum(sf) else 0 end as '工业用水',
       case sjbm when '003' then sum(sf) else 0 end as '事业单位',
       case sjbm when '004' then sum(sf) else 0 end as '营业与基建',
       case sjbm when '005' then sum(sf) else 0 end as '特种用水',
       case sjbm when '006' then sum(sf) else 0 end as '餐饮业',
       nd,yf  from  bill_sf  group by sjbm,nd,yf order by nd,yf
这样的查询要怎么写啊~~???急死人了...不考虑用临时表...临时表不好处理...谢谢了~!!

解决方案 »

  1.   

    select sum(居民生活) as 居民生活,sum(工业用水) as 工业用水,...
    from 表
      

  2.   

    select sum(居民生活) 居民生活,sum(工业用水) 工业用水,sum(事业单位) 事业单位,sum(营业与基建) 营业与基建,sum(特种) 特种,sum(餐饮业) 餐饮业,年度,月份 from t_water group by 年度,月份
      

  3.   

    我要晕了.....第一个的结果是通过查询语句得到的....而不是现成就有那些字段的啊~~各位大佬居民生活 工业用水 事业单位 营业与基建   特种    餐饮业   年度    月份 
    160.29      .00       .00       .00      .00      .00     2006    11
       .00    18.33       .00       .00      .00      .00     2006    11
       .00      .00     53.90       .00      .00      .00     2006    11
       .00      .00       .00     63.60      .00      .00     2006    11
       .00      .00       .00       .00    59.97      .00     2006    11
    128.28      .00       .00       .00      .00      .00     2006    12
       .00    11.28       .00       .00      .00      .00     2006    12 
       .00      .00     33.00       .00      .00      .00     2006    12
       .00      .00       .00     34.50      .00      .00     2006    12 
       .00      .00       .00       .00    59.96      .00     2006    12
    这个结果的查询语句是这样的:
    select case sjbm when '001' then sum(sf) else 0 end as '居民生活用水',
           case sjbm when '002' then sum(sf) else 0 end as '工业用水',
           case sjbm when '003' then sum(sf) else 0 end as '事业单位',
           case sjbm when '004' then sum(sf) else 0 end as '营业与基建',
           case sjbm when '005' then sum(sf) else 0 end as '特种用水',
           case sjbm when '006' then sum(sf) else 0 end as '餐饮业',
           nd,yf  from  bill_sf  group by sjbm,nd,yf order by nd,yf
      

  4.   

    --创建测试数据,楼主可以用你自己的表名取代"测试数据表",
    --同时把创建测试数据表的这部分代码去掉
    create table 测试数据表
    (客户名称 varchar(50), 客户编号 varchar(20), 年度 int, 月份 int,
    用水量 numeric(18,2), 水价编号 varchar(10), 用水类型 varchar(50), 单价 money, 水费 money)insert 测试数据表
    select '阿飞','2010001001',2006,11,25.00,'001','居民生活用水',1.16, 29.00
    union select '阿飞','2010001001',   2006, 11, 15.00,'004','营业与基建',   1.50,  22.50
    union select '李寻欢','1020002001',   2006, 11, 11.00,'001','居民生活用水', 1.16,  12.76
    union select '江枫','1010002001',   2006, 11, 28.00,'001','居民生活用水', 1.16,  32.48
    union select '荆无命','1010003001',   2006, 11, 5.10,'005','特种用水',  1.91,    9.74
    union select '荆无命','1010003001',   2006, 11, 11.90,'001','居民生活用水', 1.16, 13.80
    union select '十一郎','1010001001',   2006, 11, 23.00,'001','居民生活用水', 1.16, 26.68
    union select '江枫','1010002002',   2006, 11, 12.00,'005','特种用水',    1.91, 22.92
    union select '任飘萍','2010002001',   2006, 11, 18.00,'001','居民生活用水', 1.16, 20.88
    union select '任飘萍','2010002002',   2006, 11, 12.00,'004','营业与基建', 1.50, 18.00
    union select '邵破菊','1020003001',   2006, 11,  7.70,'001','居民生活用水',1.16, 8.93
    union select '邵破菊','1020003001',   2006, 11,  3.30,'005','特种用水',   1.91, 6.30
    union select '独孤红','2010003001',   2006, 11, 29.00,'003','事业单位',    1.10,    31.90
    union select '风郎君','1010004001',   2006, 11,  3.30,'001','居民生活用水', 1.16, 3.82
    union select '风郎君','1010004001',   2006, 11,  7.70,'004','营业与基建', 1.50, 11.55
    union select '叶开','1020004001',   2006, 11,  7.00,'001','居民生活用水', 1.16, 8.12
    union select '叶开','1020004001',   2006, 11,  11.00,'005','特种用水',     1.91,     21.01
    union select '李寻欢','1020002002',   2006, 11,  13.00,'002','工业用水',    1.41, 18.33
    union select '游坦之','1010005001',   2006, 11,   .00,'001','居民生活用水', 1.16, .00
    union select '游坦之','1010005001',   2006, 11,  20.00,'003','事业单位',    1.10, 22.00
    union select '傅红雪','1020001001',   2006, 11,   3.30,'001','居民生活用水', 1.16, 3.82
    union select '傅红雪','1020001001',   2006, 11,   7.70,'004','营业与基建', 1.50, 11.55
    union select '楼主','1020002002', 2006, 12, 5.30, '006','餐饮业', 2.36, 25.8
    go
    --以上只是创建测试数据.declare @str varchar(8000)
    set @str = ''select @str = @str + '['+用水类型+']=sum(case when 水价编号='''+水价编号+''' then 水费 else 0 end),'
    from (select distinct 水价编号, 用水类型 from 测试数据表) AA
    --如果楼主有专门的用水类型表,
    --可以用"用水类型表"表名代替 (select distinct 水价编号, 用水类型 from 测试数据表) AA
    --这样可以将全部的用水类型转换为列,即使主表中没有某个用水类型的数据
    exec('
    select '+@str+' 年度, 月份
    from 测试数据表
    group by 年度, 月份
    ')drop table 测试数据表
      

  5.   

    SELECT 
    客户名称,年度,月份,
    (select Sum(用水量) from Tabel1 a where a.客户名称=b.客户名称 and a.年度=b.年度        and a.月份=b.月份 and a.用水类型='居民生活用水') as 居民生活用水,

    from Table1 b
      

  6.   

    你将
    exec('
    select '+@str+' 年度, 月份
    ... ...替换为
    print('
    select '+@str+' 年度, 月份
    ... ...运行后,就应该能明白怎么回事了!
      

  7.   

    select @str = @str + '['+用水类型+']=sum(case when 水价编号='''+水价编号+''' then 水费 else 0 end),'
    from (select distinct 水价编号, 用水类型 from 测试数据表) AA
    select @str ,nd as  年度,yf as 月份 from bill_sf group by nd,yf
    -----------------------------------------------------------------------------
        无列名    
    [居民生活用水]=sum(case sjbm when 001 then sf else 0 end),[工业用水]=sum(case sjbm when 002 then sf else 0 end),[事业单位]=sum(case sjbm when 003 then sf else 0 end),[营业与基建]=sum(case sjbm when 004 then sf else 0 end),[特种用水]=sum(case sjbm when 005 then sf else 0 end) , | 2006    |    12 
    可是怎么查询出来的结果会是这样的呢?