我有一个问题,我有一个表是我保留传入的,另一个是我保留传出的记录,我需要做的是按 partNumber 对这些记录进行分组并有一个输出。
InTable
PartNo Qty Date
A 1 1/1/13
A 5 1/1/13
B 2 1/1/13
OutTable
PartNo Qty Date
A 2 1/1/13
B 1 1/1/13
C 3 1/1/13
Result Needed
Date 1/1/13
PartNo In Out Total
A 6 2 4
B 2 1 1
C 0 3 -3
我已经尝试过这样的事情,但由于总和(数量),它会导致总数,但它不能以任何其他方式工作。
select a.PartNo, sum(b.inQty) as inQty,sum(c.outQty) as outQty, sum(b.inQty)-sum(c.outQty) as total from
(Select PartNo FROM InTable
where date= '01-01-2013'
group by PartNo
union
Select PartNo FROM OutTable
where date= '01-01-2013'
group by PartNo) A
cross join
(
SELECT PartNo,SUM(Qty) inQty FROM InTable
where date= '01-01-2013'
group by PartNo
)B
cross join
(
SELECT PartNo,SUM(Qty) outQty FROM OutTable
where date= '01-01-2013'
group by PartNo
)c
group by a.PartNo
在那里我尝试加入三个查询,每个查询单独产生有用的结果,但问题是当我尝试加入它们时,查询将导致类似
PartNo inQty outQty total
A 8 6 2
B 8 6 2
C 8 6 2
有什么建议吗,谢谢。