在用sum函数  和 group  by    计算时,总是自动排序,问题:
1、如何不让他排序
2、如果条件里有,而表中没有,能否显示结果时直接显示0
比如:colume1     colume2
-----------------
c             1
a             3
b             4
b             2select sum(colume2) from table1 where colume1 in ('c','d','a','b' )  group by colume1
得出的结果居然是:
a    3
b    6
c    1
而我想要的结果是(按照 colume1 in 中的顺序进行,而且0值的也显示):
c        1
d        0
a        3
b        6
如何实现啊~!
情况是这样的,统计2月份的销量,给的execl表中有200个商品,而且没有排序,  而且200其中的商品很可能有个别就没有销售,所以我需要按照excel表中的顺序来查 数据库中的合计销量(数据库中的商品销售表中,有销售才有记录),而且0销售的也要有,所以就会有那个例子

解决方案 »

  1.   

    select a.col1,isnull(b.s,0) as col2
    from 
    (select 'a' as col1 union select 'b' union select 'c' union select 'd') a
    left join
    (select col1,sum(col2) as s from table1 group by col1) b
    on a.col1=b.col2
      

  2.   

    太复杂了吧,我那个只是一个例子,并不是只有4个条件 a   b   c  d  对应的是200多种商品哦
      

  3.   


    select colume1,sum(isnull(colume2,0)) 
    from table1 
    where colume1 in ('c','d','a','b' ) 
    group by colume1
    order by sum(isnull(colume2,0)) 
      

  4.   

    if object_id('dbo.tb') is not null drop table dbo.tbcreate table dbo.tb
    (
    col1 char(1),
    col2 int
    )insert dbo.tb
    select 'a', 2 union all
    select 'b', 3 union all
    select 'c', 3 union all
    select 'b', 4if object_id('dbo.tbcategory') is not null drop table dbo.tbcategorycreate table dbo.tbcategory
    (
    col1 char(1)
    )insert into dbo.tbcategory
    select 'a' union all
    select 'b' union all
    select 'c' union all
    select 'd'
    select c.col1, isnull(sum(col2),0) total from (select col1 from dbo.tbcategory) c left join dbo.tb t on c.col1=t.col1
    group by c.col1 order by total/*
    col1   total
    d 0
    a 2
    c 3
    b 7
    */
      

  5.   

    select c.column1, isnull(c.column2, 0)
    from
    (select a.column1 as column1, b.column2 as column2
    from a
    left join b
    on a.column1 = b.column1) c
    a为一个表,里面200种商品信息都有
      

  6.   

    你给的column1并不代表了所有两百种,把所有商品信息放入新建表中,用外连接引入,像6楼描述的那样就可以了