我需要帮助从两个表中提取确切的项目匹配。我想从详细信息表中查看Temp Detail和Master和Temp Master itemQty中存在的所有顺序是相同的。
示例:在这种情况下,我需要master.Amount的总和,对于ItemQty为3的所有订单,详细信息表具有完全相同的temp Detail值。
主
OrderId| Amount | ItemQty |Status
------------------------------------------
125 | 36.75 | 3 | A |
------------------------------------------
128 | 11.95 | 5 | A |
------------------------------------------
详细信息包含大量订单明细
------------------------------------------
OrderId| ItemId | Qty | Price |
------------------------------------------
125 | 12345 | 1 | 11.00 |
------------------------------------------
125 | 23456 | 1 | 12.75 |
------------------------------------------
125 | 34567 | 1 | 13.00 |
------------------------------------------
Temp Master
------------------------------------------
|OrderId| Amount | ItemQty |Status
------------------------------------------
|9999 | 36.75 | 3 | A |
------------------------------------------
临时详情
------------------------------------------
|OrderId | ItemId | Qty | Price |
------------------------------------------
| 9999 | 12345 | 1 | 11.00 |
------------------------------------------
| 9999 | 23456 | 1 | 12.75 |
------------------------------------------
| 9999 | 34567 | 1 | 13.00 |
------------------------------------------
提前谢谢
答案 0 :(得分:0)
你可以试试这样的东西
select m.OrderId, sum(m.amount)
from master m
join detail d
on m.OrderId = d.OrderId
join tempDetail td
on m.OrderId = td.OrderId
where m.ItemQty = 3
group by m.OrderId
having sum(d.Qty * d.Price) = sum(td.Qty * td.Price)
答案 1 :(得分:0)
我最好的猜测:
select Masterr.OrderId,
sum(Masterr.Amount)
from Masterr
where Masterr.ItemQty = 3
and exists(select *
from Detail
join TempMaster on TempMaster.ItemQty = Masterr.ItemQty
join TempDetail on TempDetail.OrderId = TempMaster.OrderId
where Detail.OrderId = Masterr.OrderId
and TempDetail.ItemId = Detail.ItemId
and TempDetail.Qty = Detail.Qty
and TempDetail.Price = Detail.Price)
group by Masterr.OrderId;