日期:2014-05-17  浏览次数:20965 次

求教进出库的问题
数据表结构
--------------------------------------------
number              type             t_id
--------------------------------------------
54                   1                 12
30                   2                 13
68                   1                 13
---------------------------------------------

type中 1代表 进了多少,2代表出了多少 

现在想要实现的结果如下:
---------------------------------------------
t_id            进                     出           
--------------------------------------------
12              54                 0
13              68                 30
--------------------------------------------------
求解

------解决方案--------------------
SQL> select * from test;

         N          T         ID
---------- ---------- ----------
        54          1         12
        30          2         13
        68          1         13

SQL> select id,sum(n * decode(t,1,1,0)) as "in",
  2          sum(n * decode(t,2,1,0)) as "out"
  3  from test group by id;

        ID         in        out
---------- ---------- ----------
        13         68         30