首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

数据库代码如何写?

2012-02-01 
数据库代码怎么写??数据库表如图:IDNameNumPriceSort1苹果32出库1苹果23出库1苹果42入库2香蕉41入库2香蕉4

数据库代码怎么写??
数据库表如图:
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

热点排行