stk*_*flw 0 sql google-bigquery
WITH rawData AS (
SELECT
wholesaler_id,
month,
day,
year,
product_volume,
product_gp,
buyer_id,
salesrep_id AS default_salesrep_id,
salesrep_name,
fpl
FROM tables.transactions
WHERE wholesaler_id = 'hos'
AND year IN (2019, 2018)
AND month = 12
),
salesRepsToBuyersAssociation AS (
SELECT DISTINCT
default_salesrep_id AS salesrep_id,
buyer_id
FROM rawData
)
Run Code Online (Sandbox Code Playgroud)
此查询返回 60636 行:
SELECT *
FROM rawData
Run Code Online (Sandbox Code Playgroud)
此查询返回 72039 行:
SELECT
rawData.*,
salesrep_id
FROM rawData
LEFT JOIN salesRepsToBuyersAssociation USING (buyer_id)
Run Code Online (Sandbox Code Playgroud)
LEFT JOIN 假设只将匹配的salesRepsToBuyersAssociation行连接到rawData,对吗?为什么我得到的行比rawData.
因为salesRepsToBuyersAssociation一个或多个buyer_ids中有多行。
你可以检查这个:
select buyer_id, count(*)
from salesRepsToBuyersAssociation
group by buyer_id
having count(*) > 1;
Run Code Online (Sandbox Code Playgroud)