使用 join 仅对第一个 table 的值求和一次
Sum value from first table only once with join
我有两个table
Table 1 - “B2B - 配额(直接)”Q -
每天单排
| Date (dd/mm/yyyy) | Georgina Target| Date Lookup (unix timestamp) |
| :---------------- | :--------------| :-------------------------------|
| 1/1/22 | | unix_timestamp(date) |
| 2/1/22 | | unix_timestamp(date) |
| 3/1/22 | | unix_timestamp(date) |
Table 2 - “B2B - 账户到发票”A -
每天多行
| Date (dd/mm/yyyy) | Total Directs | Date Lookup (unix timestamp) |
| :---------------- | :--------------| :-------------------------------|
| 1/1/22 | | unix_timestamp(date) |
| 1/1/22 | | unix_timestamp(date) |
| 1/1/22 | | unix_timestamp(date) |
| 3/1/22 | | unix_timestamp(date) |
| 3/1/22 | | unix_timestamp(date) |
| 3/1/22 | | unix_timestamp(date) |
我想要return的是:
| Date (dd/mm/yyyy) | Total Directs| Georgina Target |
| :---------------- | :----------- | :--------------- |
| 1/1/22 | | |
| 2/1/22 | [=13=] | |
| 3/1/22 | | |
我目前正在使用这个 SQL:
SELECT
Q."Date" AS "Date",
sum_if(A."Account Type" REGEXP 'direct', A."Invoice Amount") AS "Total Directs",
sum(Q."Georgina Target") AS "Georgina Target"
FROM "B2B - Quotas (DIRECT)" Q
LEFT OUTER JOIN "B2B - Accounts to Invoices" A ON A."Date Lookup" = Q."Date Lookup"
GROUP BY "Date"
问题是 'Georgina Target' 多次求和(基于 table - “B2B - Accounts to Invoices” A 那个日期的行数)。
注意:“Total Directs”可能没有每个日期的一行,但我需要显示每个日期(基于 table“B2B - 配额(DIRECT)”)
SUM()
total_directs
的子查询。 LEFT JOIN
和 COALESCE()
用于您的注释。假设 QUOTAS
table 一个日期不能有多个目标。
SELECT
q.date
,q.target --No need to aggregate since Target has a single value per day
,COALESCE(a.total_directs,0) -- DBMS dependent
FROM b2b_quotas AS q
LEFT JOIN (
SELECT
SUM(total_directs) as total_directs
,date
FROM invoices
GROUP BY date
) AS a ON q.date = a.date
我有两个table
Table 1 - “B2B - 配额(直接)”Q - 每天单排
| Date (dd/mm/yyyy) | Georgina Target| Date Lookup (unix timestamp) |
| :---------------- | :--------------| :-------------------------------|
| 1/1/22 | | unix_timestamp(date) |
| 2/1/22 | | unix_timestamp(date) |
| 3/1/22 | | unix_timestamp(date) |
Table 2 - “B2B - 账户到发票”A - 每天多行
| Date (dd/mm/yyyy) | Total Directs | Date Lookup (unix timestamp) |
| :---------------- | :--------------| :-------------------------------|
| 1/1/22 | | unix_timestamp(date) |
| 1/1/22 | | unix_timestamp(date) |
| 1/1/22 | | unix_timestamp(date) |
| 3/1/22 | | unix_timestamp(date) |
| 3/1/22 | | unix_timestamp(date) |
| 3/1/22 | | unix_timestamp(date) |
我想要return的是:
| Date (dd/mm/yyyy) | Total Directs| Georgina Target |
| :---------------- | :----------- | :--------------- |
| 1/1/22 | | |
| 2/1/22 | [=13=] | |
| 3/1/22 | | |
我目前正在使用这个 SQL:
SELECT
Q."Date" AS "Date",
sum_if(A."Account Type" REGEXP 'direct', A."Invoice Amount") AS "Total Directs",
sum(Q."Georgina Target") AS "Georgina Target"
FROM "B2B - Quotas (DIRECT)" Q
LEFT OUTER JOIN "B2B - Accounts to Invoices" A ON A."Date Lookup" = Q."Date Lookup"
GROUP BY "Date"
问题是 'Georgina Target' 多次求和(基于 table - “B2B - Accounts to Invoices” A 那个日期的行数)。
注意:“Total Directs”可能没有每个日期的一行,但我需要显示每个日期(基于 table“B2B - 配额(DIRECT)”)
SUM()
total_directs
的子查询。 LEFT JOIN
和 COALESCE()
用于您的注释。假设 QUOTAS
table 一个日期不能有多个目标。
SELECT
q.date
,q.target --No need to aggregate since Target has a single value per day
,COALESCE(a.total_directs,0) -- DBMS dependent
FROM b2b_quotas AS q
LEFT JOIN (
SELECT
SUM(total_directs) as total_directs
,date
FROM invoices
GROUP BY date
) AS a ON q.date = a.date