如有一数据库 TEST ,数据如下:
 a                b             c
一库             出售           3
一库             未出售         2
一库             出售           4
一库             未出售         6
一库             出售           1
一库             出售           9
一库             出售           3
一库             未出售         2
一库             出售           4
一库             未出售         6
一库             出售           1
一库             出售           9==============================================================现在要用SQL输出以出以下结果:库名           已出售的          未出售的
一库             100(件)           90(件)==================================
就是同时统计出已出售和未出售的数量.

解决方案 »

  1.   

    a b c是不是字段,c中数字是什么意思?
      

  2.   

    什么数据库?sql server:select  a as 库名,
    sum(case when b='出售' then c else 0 end ) as 已出售的,
    sum(case when b='未出售' then c else 0 end ) as 未出售的
    from tablename
    group by a
      

  3.   

    access:select  a as 库名,
    sum(iif(b='出售',c,0)) as 已出售的,
    sum(iif(b='未出售',c,0)) as 未出售的
    from tablename
    group by a
      

  4.   

    C 是数量,数据库是SQL Server我试试先
      

  5.   

    select m.a 库,m.no 出售,n.yes 未出售 from 
    (select a,sum(c) no from test where b='出售') m
    (select a,sum(c) yes from test where b='未出售') n
    where m.a=n.a
      

  6.   

    这样写有问题
    select m.km 库,m.no 出售,n.yes 未出售 from 
    (select km,isnull(sum(num),0) no from table3 where yesno=1 group by km) m,
    (select km,sum(num) yes from table3 where yesno=0 group by km) n
    where m.km=n.km下面这种写法是最好的了( Haiwer(海阔天空) )select  a as 库名,
    sum(case when b='出售' then c else 0 end ) as 已出售的,
    sum(case when b='未出售' then c else 0 end ) as 未出售的
    from tablename
    group by a
      

  7.   

    谢各位了,用 Haiwer(海阔天空) 的方法试通了,分脏了.