数据表记录:      
id    type    money      
1     out     1000.00      
2     in      2000.00      
3     out     3000.00      
4     in      4000.00      
5     in      6000.00      
   
要求得出如下结果:      
type    money      
in      8000.00      
 
即最终money是所有的in之和减去所有的out之和,请问用一条sql语句怎么写啊?

解决方案 »

  1.   

    如果type只有 'in' ,'out' 值可以用下面的语句:select min(totle) from  ( select type,sum(money) as totle from test group by type ) as temptable
      

  2.   

    try:
    select sum(case when type='in' then money else -money end) from table1
      

  3.   

    二楼的方法看了就感觉不行啊,按照type分组,并将查询结果作为一个临时表,然后再取临时表中的min(total),这样的话,取出来要么是type='in'的total,取出来要么是type='out'的total,肯定不对的。
    三楼的方法确实管用,多谢!
      

  4.   

    Select Sum(Case When Type='In' Then Money Else -1*Money End) From Table1