SQL获取单个表中两个字段之间的差异

时间:2012-11-24 09:57:08

标签: sql sql-server-2005

我的观点显示:

d_date      d_shop  d_qty  r_date     r_shop  r_qty
12-oct-12   ABC     20     12-oct-12  XYZ     20
12-oct-12   ABC     15     12-oct-12  XYZ     0
12-oct-12   XYZ     10     12-oct-12  ABC     10
12-oct-12   ABC     25     12-oct-12  XYZ     10
12-oct-12   XYZ     10     12-oct-12  ABC     0

我想检查receive_shop

的receive_qty和dispatch_qty之间的区别

即。正确答案是:

r_date     r_shop  r_qty
12-oct-12  XYZ     10

我正在使用SQL Server 2005

1 个答案:

答案 0 :(得分:1)

我对这个问题并不十分清楚,但也许你正在寻找这样的东西:

select r_date, d_shop, sum(d_qty)-sum(r_qty)
from asdf
group by r_date, d_shop

SQL Fiddle