2012-09-29 151 views
2

我有一些代碼看起來是這樣的:LEFT OUTER JOIN問題SQL

select t1.colId, t1.col1, t1.col2, t1.col3, count(t2.colId) 
from table1 t1 left outer join 

(select colId from db1.tb90) t2 
on t1.colId = t2.colId 
group by 1,2,3,4 

我想所有行從t1複製,和t2應該加入那裏是一個匹配。

但是,t1.colId可以包含重複項並希望這些重複項存在。

我當前的問題是,select語句在t1.colId上執行不同的選項,所以我得到不同的t1.colId作爲反對包括重複項的數量。

問題on t1.colId = t2.colId

+0

您是否試圖在t1.colId = t2.colID的每一行顯示相同的總計數t2.colId? –

回答

1

YOu問:on t1.colId = t2.colId問題?答案是,不是,問題出在group by。在子查詢嘗試的總量,除以colId:

select t1.colId, t1.col1, t1.col2, t1.col3, n 
from table1 t1 left outer join  
    (select colId, count(*) as n 
    from db1.tb90 group by colId) t2 
on t1.colId = t2.colId 

EDITED DUE OP COMMENT

好吧,這裏some data to understand your question

create table table1 (
    colId int, 
    col1 int, 
    col2 int, 
    col3 int 
); 

create table tb90 (
    colId int 
); 

insert into table1 values 
(1,1,1,1), 
(1,1,1,1), 
(2,2,2,2); 

insert into tb90 values 
(1), 
(1), 
(3); 

查詢結果:

select t1.colId, t1.col1, t1.col2, t1.col3, count(t2.colId) 
from table1 t1 left outer join 
(select colId from tb90) t2 
on t1.colId = t2.colId 
group by 1,2,3,4; 

| COLID | COL1 | COL2 | COL3 | COUNT(T2.COLID) | 
------------------------------------------------ 
|  1 | 1 | 1 | 1 |    4 | 
|  2 | 2 | 2 | 2 |    0 | 

我的查詢結果:

select t1.colId, t1.col1, t1.col2, t1.col3, n 
from table1 t1 left outer join  
    (select colId, count(*) as n 
    from tb90 group by colId) t2 
on t1.colId = t2.colId 

| COLID | COL1 | COL2 | COL3 |  N | 
--------------------------------------- 
|  1 | 1 | 1 | 1 |  2 | 
|  1 | 1 | 1 | 1 |  2 | 
|  2 | 2 | 2 | 2 | (null) | 

現在,寫出迅速的結果。

+0

不幸的是,返回相同的不同號 – user159603

+0

雖然OP會給你一個明確的答案,我期望的目的是得到一個計數爲0(非NULL),當沒有條目給出't1.colID'值在表'tb90'中。 –

+0

@JonathanLeffler,請隨意編輯我的答案,並在合適的地方寫上合併功能。感謝您的評論。 – danihp

0

鑑於以下數據:

CREATE TABLE table1 
(
    colID INTEGER NOT NULL, 
    col1 INTEGER NOT NULL, 
    col2 INTEGER NOT NULL, 
    col3 INTEGER NOT NULL, 
    PRIMARY KEY(colID, col1, col2, col3) 
); 
INSERT INTO table1 VALUES(1, 1, 1, 1); 
INSERT INTO table1 VALUES(1, 2, 1, 1); 
INSERT INTO table1 VALUES(1, 1, 2, 1); 
INSERT INTO table1 VALUES(1, 1, 1, 2); 
INSERT INTO table1 VALUES(2, 2, 1, 1); 
INSERT INTO table1 VALUES(2, 1, 2, 1); 
CREATE TABLE db1.tb90 
(
    colID INTEGER NOT NULL, 
    col4 INTEGER NOT NULL, 
    PRIMARY KEY(ColID, Col4) 
); 
INSERT INTO db1.tb90 VALUES(1, 1); 
INSERT INTO db1.tb90 VALUES(1, 2); 
INSERT INTO db1.tb90 VALUES(1, 3); 
INSERT INTO db1.tb90 VALUES(1, 4); 
INSERT INTO db1.tb90 VALUES(1, 5); 

您的疑問:

SELECT t1.colId, t1.col1, t1.col2, t1.col3, COUNT(t2.colId) 
    FROM table1 t1 
    LEFT OUTER JOIN (SELECT colId FROM db1.tb90) t2 
    ON t1.colId = t2.colId 
GROUP BY 1, 2, 3, 4; 

產生輸出:

colid col1 col2 col3 (count) 
1  1  1  1  5 
1  1  1  2  5 
1  1  2  1  5 
1  2  1  1  5 
2  1  2  1  0 
2  2  1  1  0 

時對IBM Informix Dynamic Server的11.70.FC2在Mac OS上運行X 10.7.5。

如果這是Teradata爲相同數據提供的答案,那麼查詢計劃執行重複消除的事實並非探針;答案是正確的。如果這不是Teradata針對相同數據提供的答案,Teradata可能存在一個錯誤(IMNSHO,儘管自從我爲IBM工作在Informix上之後,我必須小心地將錯誤散佈到其他人的DBMS上)。

如果我誤解了這個問題,那麼請提供示例表格模式和值以及實際和預期輸出,以便我們可以更清楚地回顧發生的情況。您可能也想提供解釋輸出。


需要注意的是,你可以重寫查詢,如:

​​

你可以看到,有在此再形成table1不同的操作; Teradata可能會自動爲您進行轉換。