有表  a(item,qty,startdate,enddate),如何查询得如下结果  
  ----------------------------------------------------------------  
  item  totala  totalb  totalc  
  ---------------------------------------------------------- 
sysdate为当前日期 
  其中totala为 startdate <sysdate and enddate <sysdate qty的和  
  其中totalb为 startdate <sysdate and enddate>sysdate qty的和  
  其中totalc为 startdate>sysdate and enddate>sysdate qty的和 
  item 为三个(不同条件)的并集,如果相应的不存在可以在 相应的TOTAL 列填充0

解决方案 »

  1.   

    你用decode或case when都比较好做阿。
      

  2.   

    试下
    select totala + totalb + totalc item, totala, totalb, totalc
      from (select sum(case when(startdate < sysdate) and (enddate < sysdate) then qty else 0) totala,
                   sum(case when(startdate < sysdate) and (enddate < sysdate) then qty else 0) totalb,
                   sum(case when(startdate < sysdate) and (enddate < sysdate) then qty else 0) totalc
              from a)
      

  3.   


    select totala + totalb + totalc item, totala, totalb, totalc
      from (select sum(case when(startdate < sysdate) and (enddate < sysdate) then qty else 0) totala,
                   sum(case when(startdate < sysdate) and (enddate > sysdate) then qty else 0) totalb,
                   sum(case when(startdate > sysdate) and (enddate > sysdate) then qty else 0) totalc
              from a)
      

  4.   

    select item, 
                   sum(case when(startdate < sysdate) and (enddate < sysdate) then qty else 0) totala,
                   sum(case when(startdate < sysdate) and (enddate > sysdate) then qty else 0) totalb,
                   sum(case when(startdate > sysdate) and (enddate > sysdate) then qty else 0) totalc
    from a
    group by item可以了,很感谢大家
      

  5.   


    select item, 
            sum(case when(startdate < sysdate) and (enddate < sysdate) then nvl(qty,0) else 0) totala, 
            sum(case when(startdate < sysdate) and (enddate > sysdate) then nvl(qty,0) else 0) totalb, 
            sum(case when(startdate > sysdate) and (enddate > sysdate) then nvl(qty,0) else 0) totalc 
    from a 
    group by item