Join with union returns 我错了结果
Join with union returns me wrong result
我需要使用 union all 两次。然后用相同的table加入他们。第一个 union 将包含一个 where 语句,而第二个则不包含。
问题是,当我使用第二个 table 时,我的结果发生了变化。
select sum(x.quantity*x.Price)
from CustomerTrans t1
inner join (
select *
from InventoryTrans
union all
select *
from InventoryTransTemp
) x on t1.TrnDocumentID = x.TrnDocumentID
group by t1.TrnDocumentID
这是此结果的输出
First Result
然后我添加第二个联合,其中包含 where 语句
select sum(x.quantity*x.Price), sum(x2.quantity*x2.Price)
from CustomerTrans t1
left join (
select *
from InventoryTrans
union all
select *
from InventoryTransTemp
) x on t1.TrnDocumentID = x.TrnDocumentID
left join (
select *
from InventoryTrans
where printed = 2 or InvoicePaymentID = 2
union all
select *
from InventoryTransTemp
where printed = 2 or InvoicePaymentID = 2
) x2 on t1.TrnDocumentID = x2.TrnDocumentID
group by t1.TrnDocumentID
这是第二个结果
Second Result
第二个结果应该是 3.80 而不是 7.60
看起来它是我的价格 *2 而不是 *1 的倍数。
这里发生的是您加入了您不想加入的行。假设您的第一个子查询 returns
+----------+-------+
| quantity | price |
+----------+-------+
| 10 | 100 |
| 10 | 200 |
+----------+-------+
针对特定文档 ID。而你的第二个子查询 returns only
+----------+-------+
| quantity | price |
+----------+-------+
| 10 | 200 |
+----------+-------+
合并结果为:
+------------+---------+-------------+----------+
| x.quantity | x.price | x2.quantity | x2.price |
+------------+---------+-------------+----------+
| 10 | 100 | 10 | 200 |
| 10 | 200 | 10 | 200 |
+------------+---------+-------------+----------+
之后的聚合结果为:
+----------+-----------+
| x_result | x2_result |
+----------+-----------+
| 3000 | 4000 |
+----------+-----------+
而不是
+----------+-----------+
| x_result | x2_result |
+----------+-----------+
| 3000 | 2000 |
+----------+-----------+
您不想连接单行,而是连接聚合结果(每个文档的总数):
select
ct.*,
coalesce(u1.total, 0) as u1_total,
coalesce(u2.total, 0) as u2_total
from customertrans ct
left join
(
select trndocumentid, sum(quantity * price) as total
from
(
select * from inventorytrans
union all
select * from inventorytranstemp
) union1
group by trndocumentid
) u1 on u1.trndocumentid = ct.trndocumentid
left join
(
select trndocumentid, sum(quantity * price) as total
from
(
select * from inventorytrans where printed = 2 or invoicepaymentid = 2
union all
select * from inventorytranstemp where printed = 2 or invoicepaymentid = 2
) union2
group by trndocumentid
) u2 on u2.trndocumentid = ct.trndocumentid
group by ct.trndocumentid
order by ct.trndocumentid;
我需要使用 union all 两次。然后用相同的table加入他们。第一个 union 将包含一个 where 语句,而第二个则不包含。 问题是,当我使用第二个 table 时,我的结果发生了变化。
select sum(x.quantity*x.Price)
from CustomerTrans t1
inner join (
select *
from InventoryTrans
union all
select *
from InventoryTransTemp
) x on t1.TrnDocumentID = x.TrnDocumentID
group by t1.TrnDocumentID
这是此结果的输出
First Result
然后我添加第二个联合,其中包含 where 语句
select sum(x.quantity*x.Price), sum(x2.quantity*x2.Price)
from CustomerTrans t1
left join (
select *
from InventoryTrans
union all
select *
from InventoryTransTemp
) x on t1.TrnDocumentID = x.TrnDocumentID
left join (
select *
from InventoryTrans
where printed = 2 or InvoicePaymentID = 2
union all
select *
from InventoryTransTemp
where printed = 2 or InvoicePaymentID = 2
) x2 on t1.TrnDocumentID = x2.TrnDocumentID
group by t1.TrnDocumentID
这是第二个结果
Second Result
第二个结果应该是 3.80 而不是 7.60
看起来它是我的价格 *2 而不是 *1 的倍数。
这里发生的是您加入了您不想加入的行。假设您的第一个子查询 returns
+----------+-------+ | quantity | price | +----------+-------+ | 10 | 100 | | 10 | 200 | +----------+-------+
针对特定文档 ID。而你的第二个子查询 returns only
+----------+-------+ | quantity | price | +----------+-------+ | 10 | 200 | +----------+-------+
合并结果为:
+------------+---------+-------------+----------+ | x.quantity | x.price | x2.quantity | x2.price | +------------+---------+-------------+----------+ | 10 | 100 | 10 | 200 | | 10 | 200 | 10 | 200 | +------------+---------+-------------+----------+
之后的聚合结果为:
+----------+-----------+ | x_result | x2_result | +----------+-----------+ | 3000 | 4000 | +----------+-----------+
而不是
+----------+-----------+ | x_result | x2_result | +----------+-----------+ | 3000 | 2000 | +----------+-----------+
您不想连接单行,而是连接聚合结果(每个文档的总数):
select
ct.*,
coalesce(u1.total, 0) as u1_total,
coalesce(u2.total, 0) as u2_total
from customertrans ct
left join
(
select trndocumentid, sum(quantity * price) as total
from
(
select * from inventorytrans
union all
select * from inventorytranstemp
) union1
group by trndocumentid
) u1 on u1.trndocumentid = ct.trndocumentid
left join
(
select trndocumentid, sum(quantity * price) as total
from
(
select * from inventorytrans where printed = 2 or invoicepaymentid = 2
union all
select * from inventorytranstemp where printed = 2 or invoicepaymentid = 2
) union2
group by trndocumentid
) u2 on u2.trndocumentid = ct.trndocumentid
group by ct.trndocumentid
order by ct.trndocumentid;