2016-12-27 36 views
-3

我有兩個表如
LEFT JOIN第三表到原來的表,而不是將所得表

  1. 收入(點,日期,INC)
  2. 結果(點,日期,下)

其中日期是主鍵。
我需要找出任何給定日期的總收入inc和結果out。 我的回答

WITH all_date AS 
(SELECT point, date FROM income 
UNION 
SELECT point, date FROM outcome) 
SELECT a.point, a.date, SUM(inc), SUM(out) 
FROM all_date a LEFT JOIN income i ON i.point=a.point AND i.date=d.date 
       LEFT JOIN outcome o ON o.point=a.point AND o.date=a.date 
GROUP BY a.point, a.date 

但是我得到錯誤的結果。因爲all_date LEFT JOIN income試圖做LEFT JOINoutcome重複從income某些行,因此搞亂最後SUM

基本上我想LEFT JOIN第三個表outcomeall_dateall_date LEFT JOIN income沒有。請參閱QUS否http://www.sql-ex.ru/learn_exercises.php 30.進一步澄清

回答

0

我不知道什麼是這裏用表格表達的目的,你可以使用一個得到的結果加入夜:

SELECT i.point, i.date, SUM(inc), SUM(out) 
FROM income i 
LEFT JOIN outcome o 
ON o.point=i.point AND o.date=i.date 
GROUP BY i.point, i.date 
+0

單向外連接將跳過一側的行。 –

+0

@LONG在你的查詢中,你將丟棄右表'結果'中的行,它在左表'收入'中沒有匹配。爲了獲得所有'point-date',我使用了CTE –

+0

我看到了,如果你只想過濾掉那些常見的日期,那就使用'INNER JOIN'。只需在我的答案中將'LEFT JOIN'改爲'INNER JOIN'即可。根據你的評論,我猜你不想錯過任何日期,無論是否存在'inc'和'out',然後嘗試:SELECT i.point,i.date,SUM(ISNULL(i.inc ,0)),SUM(ISNULL(o.out,0)) FROM income i FULL JOIN結果o ON o.point = i.point AND o.date = i.date GROUP BY i.point,i .date – LONG

4

爲什麼不那麼首先加入?

,甚至更好,只是做一個完全外部聯接:

Select coalesce(o.point, i.point) point, 
    coalesce(o.Date, i.date) date 
From income i full join outcome o 
    on o.point=i.point 
    and o.date=i.date 
group by coalesce(o.point, i.point), 
    coalesce(o.Date, i.date) 

和,因爲你實際上並沒有使用任何聚合函數,明顯將工作以及:

Select distinct 
    coalesce(o.point, i.point) point, 
    coalesce(o.Date, i.date) date 
From income i full join outcome o 
    on o.point=i.point 
    and o.date=i.date 

我道歉,我不是MySQL的人,並沒有意識到MySQL沒有完全連接語法。但有一個解決方法。看看下面的鏈接:

Full Join in MYSQL

基本上你需要一個聯盟,(接近你在做什麼,實際上)

select point, date 
from income i 
left join outcome o 
    on o.point=i.point 
     and o.date=i.date 
    UNION     -- <-- leave out the ALL to eliminate duplicates 
select point, date 
from outcome o 
right join income i 
    on i.point=o.point 
     and i.date=o.date 
+0

剛剛實現的MySQL不支持FULL JOIN。任何解決方案? –

+0

我的歉意,我不是MySQL的人,並沒有意識到這一點。但檢查了這一點...正如我的回答中提到的鏈接 –

+0

@BikashBehera不會混淆人們 - 你用MS SQL SERVER dbms標記了遊覽問題。 –

0

MS-SQL SP您的問題:

CREATE PROCEDURE dototals 
@date DATETIME 
AS 
BEGIN 

SELECT point,SUM(ISNULL(inc,0)) as inc into #tmp_i FROM Income WHERE date [email protected] GROUP BY point 

SELECT point,SUM(ISNULL(out,0)) as out into #tmp_d FROM Outcome WHERE date [email protected] GROUP BY point 

SELECT DISTINCT point into #tmp_p FROM 
(SELECT point FROM #tmp_i 
UNION ALL 
SELECT point FROM #tmp_o) 

SELECT @date as date, t1.point as point, t2.out as outcome,t3.inc as income 
FROM #tmp_p as t1 
LEFT JOIN #tmp_o as t2 on t1.point=t2.point 
LEFT JOIN #tmp_i as t3 on t1.point=t3.point 
END 
0

這是我在MSSQL中的最終解決方案。但我想用COALESCEFULL JOIN的作品效果最好。

WITH all_date AS 
    (SELECT point, date FROM income 
    UNION 
    SELECT point, date FROM outcome) 

    SELECT a.point, a.date, 
    (SELECT SUM(o.out) FROM outcome o WHERE o.point=a.point AND o.date=a.date) AS outcome, 
    (SELECT SUM(i.inc) FROM income i WHERE i.point=a.point AND i.date=a.date) AS income 

    FROM all_date a