2012-09-29 58 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可能会自动为您进行转换。