SQL Join Sum表不同于单一查询
SQL Join Sum tables different from single queries
我的问题是:我有 3 tables Invoiced
、Expired
、Payed
,每个 table 都有这些列:
- customer_code
- 类型
- 数量
在 Oracle 中,我正在尝试这样做:
SELECT
C.customer_code,
C.type,
SUM(C.AMOUNT) AS AMOUNT_EXPIRED,
SUM(F.AMOUNT) AS AMOUNT_INVOICED,
SUM(R.AMOUNT) AS AMOUNT_PAYED
FROM Expired C
LEFT JOIN (SELECT customer_code, ACCOUNT, SUM(AMOUNT) AS AMOUNT
FROM Invoiced
GROUP BY customer_code, type) F
ON C.customer_code = F.customer_code
AND C.type = F.type
LEFT JOIN (SELECT customer_code, type, SUM(AMOUNT) AS AMOUNT
FROM Payed
GROUP BY customer_code, type) R
ON C.customer_code = R.customer_code
AND C.type = R.type
GROUP BY C.customer_code, C.type,
R.customer_code, R.type,
F.customer_code, F.type
我得到一个 table 列:
------------------------------------------------------------------------
customer_code | type | amount_expired | amount invoiced | amount payed
------------------------------------------------------------------------
但金额与单个查询不同:
SELECT customer_code, type, SUM(AMOUNT) AS AMOUNT FROM expired GROUP BY customer_code, type
SELECT customer_code, type, SUM(AMOUNT) AS AMOUNT FROM invoiced GROUP BY customer_code, type
SELECT customer_code, type, SUM(AMOUNT) AS AMOUNT FROM payed GROUP BY customer_code, type
谁能帮帮我?
使用 union
而不是 join
。但是你应该有一个 table 来存储所有的客户代码和类型。当涉及聚合函数时,你永远不应该使用 join
。它非常混乱,通常会产生不正确的结果。
with CUSTOMER as (
select CUSTOMER_CODE, TYPE from EXPIRED group by CUSTOMER_CODE, TYPE
union
select CUSTOMER_CODE, TYPE from INVOICED group by CUSTOMER_CODE, TYPE
union
select CUSTOMER_CODE, TYPE from PAYED group by CUSTOMER_CODE, TYPE
)
select
c.CUSTOMER_CODE, c.TYPE
,(
select sum(AMOUNT)
from EXPIRED
where CUSTOMER_CODE = c.CUSTOMER_CODE and TYPE = c.TYPE
) as AMOUNT_EXPIRED
,(
select sum(AMOUNT)
from INVOICED
where CUSTOMER_CODE = c.CUSTOMER_CODE and TYPE = c.TYPE
) as AMOUNT_INVOICED
,(
select sum(AMOUNT)
from PAYED
where CUSTOMER_CODE = c.CUSTOMER_CODE and TYPE = c.TYPE
) as AMOUNT_EXPIRED
from CUSTOMER c
order by c.CUSTOMER_CODE, c.TYPE
可能是你的第二个子查询的原因,这可能是你所期望的;
SELECT C.customer_code, C.type,
SUM(C.AMOUNT) AS AMOUNT_EXPIRED,
SUM(F.AMOUNT) AS AMOUNT_INVOICED,
SUM(R.AMOUNT) AS AMOUNT_PAYED
FROM Expired C
LEFT JOIN invoiced F
ON C.customer_code= F.customer_code
AND C.type= F.type
LEFT JOIN payed R
ON C.customer_code= R.customer_code
AND C.type= R.type
GROUP BY C.customer_code, C.type
我的问题是:我有 3 tables Invoiced
、Expired
、Payed
,每个 table 都有这些列:
- customer_code
- 类型
- 数量
在 Oracle 中,我正在尝试这样做:
SELECT
C.customer_code,
C.type,
SUM(C.AMOUNT) AS AMOUNT_EXPIRED,
SUM(F.AMOUNT) AS AMOUNT_INVOICED,
SUM(R.AMOUNT) AS AMOUNT_PAYED
FROM Expired C
LEFT JOIN (SELECT customer_code, ACCOUNT, SUM(AMOUNT) AS AMOUNT
FROM Invoiced
GROUP BY customer_code, type) F
ON C.customer_code = F.customer_code
AND C.type = F.type
LEFT JOIN (SELECT customer_code, type, SUM(AMOUNT) AS AMOUNT
FROM Payed
GROUP BY customer_code, type) R
ON C.customer_code = R.customer_code
AND C.type = R.type
GROUP BY C.customer_code, C.type,
R.customer_code, R.type,
F.customer_code, F.type
我得到一个 table 列:
------------------------------------------------------------------------
customer_code | type | amount_expired | amount invoiced | amount payed
------------------------------------------------------------------------
但金额与单个查询不同:
SELECT customer_code, type, SUM(AMOUNT) AS AMOUNT FROM expired GROUP BY customer_code, type
SELECT customer_code, type, SUM(AMOUNT) AS AMOUNT FROM invoiced GROUP BY customer_code, type
SELECT customer_code, type, SUM(AMOUNT) AS AMOUNT FROM payed GROUP BY customer_code, type
谁能帮帮我?
使用 union
而不是 join
。但是你应该有一个 table 来存储所有的客户代码和类型。当涉及聚合函数时,你永远不应该使用 join
。它非常混乱,通常会产生不正确的结果。
with CUSTOMER as (
select CUSTOMER_CODE, TYPE from EXPIRED group by CUSTOMER_CODE, TYPE
union
select CUSTOMER_CODE, TYPE from INVOICED group by CUSTOMER_CODE, TYPE
union
select CUSTOMER_CODE, TYPE from PAYED group by CUSTOMER_CODE, TYPE
)
select
c.CUSTOMER_CODE, c.TYPE
,(
select sum(AMOUNT)
from EXPIRED
where CUSTOMER_CODE = c.CUSTOMER_CODE and TYPE = c.TYPE
) as AMOUNT_EXPIRED
,(
select sum(AMOUNT)
from INVOICED
where CUSTOMER_CODE = c.CUSTOMER_CODE and TYPE = c.TYPE
) as AMOUNT_INVOICED
,(
select sum(AMOUNT)
from PAYED
where CUSTOMER_CODE = c.CUSTOMER_CODE and TYPE = c.TYPE
) as AMOUNT_EXPIRED
from CUSTOMER c
order by c.CUSTOMER_CODE, c.TYPE
可能是你的第二个子查询的原因,这可能是你所期望的;
SELECT C.customer_code, C.type,
SUM(C.AMOUNT) AS AMOUNT_EXPIRED,
SUM(F.AMOUNT) AS AMOUNT_INVOICED,
SUM(R.AMOUNT) AS AMOUNT_PAYED
FROM Expired C
LEFT JOIN invoiced F
ON C.customer_code= F.customer_code
AND C.type= F.type
LEFT JOIN payed R
ON C.customer_code= R.customer_code
AND C.type= R.type
GROUP BY C.customer_code, C.type