I am getting wrong data (cartesian product) when using inner join.
Table t1
PurchaseOrder CostID Amount
1 1 4
1 2 3
Table t2
PurchaseOrder OrderType ItemId
1 321 1
1 321 2
1 321 3
1 321 4
2 128 5
2 128 6
3 321 9
Required Output
PurchaseOrder Amount
1 7
My Output
PurchaseOrder Amount
1 28
I am trying to use inner join to get the output but not getting the right data.
Query:
CREATE TEMP TABLE t1
(
PurchaseOrder INT64,
CostID INT64,
Amount INT64
);
INSERT INTO t1
VALUES (1,1,4),(1,2,3);
SELECT *
FROM t1;
CREATE TEMP TABLE t2
(
PurchaseOrder INT64,
OrderType INT64
);
INSERT INTO t2
VALUES (1,321),(1,321),(1,321),(1,321);
SELECT *
FROM t2;
select t1.PurchaseOrder, sum(amount)
from t1 inner join t2 on t1.PurchaseOrder = t2.PurchaseOrder
where t2.OrderType = 321
group by t1.PurchaseOrder;
I think you just want to check if some PurchaseOrder has an OrderType 321, that is what you use Table 2 for, to do this check. But then to sum the amounts, all information is already in Table 1. So I would write the query as follows:
select PurchaseOrder, sum(amount)
from t1
where exists
(select * from t2
where PurchaseOrder=t1.PurchaseOrder
and OrderType=321)
group by PurchaseOrder
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.