select sum(a.ye) from table where a.date='20070731'

解决方案 »

  1.   

    谢谢楼上的,可是如果我要查询的语句:
    select a.name,sum(case when a.date = '20070731' then a.ye end)
    ,sum(case when a.date = '20070631' then a.ye end)
    ,sum(case when a.date = '20060731' then a.ye end)
    from people a where a.date in ('20070731' ,'20070631' ,'20060731' )
    现实用的时候people表还关联了很多个相关的表.
    这样如何优化?
      

  2.   

    如果效率低,就写个函数或者存储过程,取三次。比如你上面的语句,
    你写三条select sum(a.ye) from table where a.date='20070731'
    都比你上面那条效率高得多。