我想联接多个表,以便从所有表中获得不同列的总和。我的表结构示例如下所示。
客户为A
+---+-------+------------+--------+----------+
|id | name | profile_id | grp | end_date |
+---+-------+------------+--------+----------+
| 1 | cust1 | 7 | ae | (null) |
+---+-------+------------+--------+----------+
| 2 | cust2 | 2 | bz | (null) |
+---+-------+------------+--------+----------+
| 3 | cust2 | 2 | cc | not_null |
+---+-------+------------+--------+----------+
个人资料AS B
+---+-------------------+
|id | profile_name |
+---+-------------------+
| 2 | pro_cust1 |
+---+-------------------+
| 7 | pro_cust2 |
+---+-------------------+
发票AS C
+---+------------------+----------------+----------------+
|id | reference | scandate | customer_id |
+---+------------------+----------------+----------------+
| 1 | test_SOMETHING1 | (today) | 2 |
+---+------------------+----------------+----------------+
| 2 | test_2 | (today) | 2 |
+---+------------------+----------------+----------------+
| 3 | test_SOMETHING2 | (not_today) | 2 |
+---+------------------+----------------+----------------+
| 4 | test_SOMETHING3 | (today) | 1 |
+---+------------------+----------------+----------------+
| 5 | test_3 | (today) | 2 |
+---+------------------+----------------+----------------+
| 6 | test_SOMETHING4 | (not_today) | 1 |
+---+------------------+----------------+----------------+
Invoice_s_errors AS D
+---+------------------+----------------+----------------+
|id | reference | scandate | customer_id |
+---+------------------+----------------+----------------+
| 1 | tst_SOMETHING1 | (today) | 1 |
+---+------------------+----------------+----------------+
| 2 | tst_2 | (today) | 2 |
+---+------------------+----------------+----------------+
| 3 | tst_SOMETHING2 | (not_today) | 1 |
+---+------------------+----------------+----------------+
| 4 | tst_SOMETHING3 | (today) | 1 |
+---+------------------+----------------+----------------+
| 5 | tst_3 | (today) | 2 |
+---+------------------+----------------+----------------+
| 6 | tst_SOMETHING4 | (not_today) | 1 |
+---+------------------+----------------+----------------+
Invoice_fail AS E
+---+------------------+----------------+----------------+
|id | reference | scandate | customer_id |
+---+------------------+----------------+----------------+
| 1 | ttt_SOMETHING1 | (today) | 2 |
+---+------------------+----------------+----------------+
| 2 | ttt_2 | (today) | 1 |
+---+------------------+----------------+----------------+
| 3 | ttt_SOMETHING2 | (not_today) | 2 |
+---+------------------+----------------+----------------+
| 4 | ttt_SOMETHING3 | (today) | 2 |
+---+------------------+----------------+----------------+
| 5 | ttt_3 | (today) | 1 |
+---+------------------+----------------+----------------+
| 6 | ttt_SOMETHING4 | (not_today) | 2 |
+---+------------------+----------------+----------------+
输出:
+---------+---------------+--------+--------+
| customer+ profile | group | Total |
+---------+---------------+--------+--------+
| cust1 | pro_cust1 | ae | 2 |
+---------+---------------+--------+--------+
| cust2 | pro_cust2 | bz | 3 |
+---------+---------------+--------+--------+
下面是我的代码,未返回任何数据。我在做什么错了?
SELECT
A.name as customer
,B.profile_name as profile
,A.grp as "Group"
,count(distinct C.reference) + count(distinct D.reference) + count(distinct E.reference) as "Total"
FROM
customers A
INNER JOIN
profiles B
ON
A.profile_id = B.id
INNER JOIN
invoices C
ON
A.id = C.customer_id
INNER JOIN
invoice_s_errors D
ON
A.id = D.customer_id
INNER JOIN
invoice_fail E
ON
A.id = E.customer_id
WHERE
A.end_date IS NULL
AND
(upper(C.reference) NOT LIKE ('%SOMETHING%') AND trunc(C.scandate) = trunc(sysdate))
AND
(upper(D.reference) NOT LIKE ('%SOMETHING%') AND trunc(D.scandate) = trunc(sysdate))
AND
(upper(E.reference) NOT LIKE ('%SOMETHING%') AND trunc(E.scandate) = trunc(sysdate))
GROUP BY A.name, A.grp, B.profile_name ORDER BY A.name ASC
答案 0 :(得分:2)
这是您的数据,并且是因为您INNER JOIN了它们。
与C和D中的表相比,表别名E中具有“引用”但不带有“ SOMETHING”的customerid有所不同
因此更改为LEFT JOIN。
然后在JOIN的WHERE中放置一些现在的条件。
否则,它们仍将充当INNER JOIN的角色。
SELECT
cust.name AS customer
,prof.profile_name AS profile
,cust.grp AS "Group"
,(COUNT(DISTINCT inv.reference) +
COUNT(DISTINCT inverr.reference) +
COUNT(DISTINCT invfail.reference)) AS "Total"
FROM customers cust
JOIN profiles prof ON prof.id = cust.profile_id
LEFT JOIN invoices inv
ON inv.customer_id = cust.id
AND upper(inv.reference) NOT LIKE ('%SOMETHING%')
AND trunc(inv.scandate) = trunc(sysdate)
LEFT JOIN invoice_s_errors inverr
ON inverr.customer_id = cust.id
AND upper(inverr.reference) NOT LIKE ('%SOMETHING%')
AND trunc(inverr.scandate) = trunc(sysdate)
LEFT JOIN invoice_fail invfail
ON invfail.customer_id = cust.id
AND upper(invfail.reference) NOT LIKE ('%SOMETHING%')
AND trunc(invfail.scandate) = trunc(sysdate)
WHERE cust.end_date IS NULL
GROUP BY cust.name, cust.grp, prof.profile_name
ORDER BY cust.name ASC