日期:2014-05-18  浏览次数:20507 次

数据库代码怎么写??
数据库表如图:
ID       Name     Num   Price     Sort
1         苹果       3         2         出库
1         苹果       2         3         出库
1         苹果       4         2         入库
2         香蕉       4         1         入库
2         香蕉       4         4         出库
3       自行车     1         2         出库
3       自行车     2         4         出库

我想得到如下结果:
产品名称     出库总数量   出库总金额   入库总数量   入库总金额
  苹果               3+2               3*2+2*3                 4                   4*2
  香蕉                 4                     4*4                     4                   4*1
自行车             2+1               1*2+2*4                 0                     0

数据库代码怎么写??


------解决方案--------------------
select name,sum(case sort when '出库 ' then num else 0 end) as 出库总数量, sum(case sort when '出库 ' then num*price else 0 end) as 出库总金额, sum(case sort when '入库 ' then num else 0 end) as 出库总数量, sum(case sort when '入库 ' then num*price else 0 end) as 出库总金额 from 表 group by name,price