2

我的代码看起来像这样:

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吗?

4

2 回答 2

1

你问:问题出在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

编辑到期的操作评论

好的,这里有一些数据可以理解您的问题

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) |

现在,写下加急结果。

于 2012-09-29T12:00:52.403 回答
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

在 Mac OS X 10.7.5 上针对 IBM Informix Dynamic Server 11.70.FC2 运行时。

如果这是 Teradata 针对相同数据给出的答案,那么查询计划执行重复消除这一事实就不是问题了;答案是正确的。如果这不是 Teradata 对相同数据给出的答案,那么 Teradata 中可能存在一个错误(IMNSHO,尽管我必须小心地对其他人的 DBMS 进行诽谤,因为我在 IBM 的 Informix 上工作)。

如果我误解了这个问题,那么请提供示例表模式和值以及实际和预期的输出,以便我们可以更清楚地查看正在发生的事情。您可能还想提供解释输出。


请注意,您可以将查询重写为:

SELECT t1.colId, t1.col1, t1.col2, t1.col3, t2.colId_count
  FROM table1 t1
  JOIN (SELECT t3.colID, COUNT(*) AS colId_count
          FROM (SELECT DISTINCT colID FROM table1) AS t3
          LEFT JOIN db1.tb90 AS t4 ON t3.colId = t4.colId
         GROUP BY t3.colID
       ) t2
    ON t1.colId = t2.colId;

你可以看到table1在这个重新表述中有一个 DISTINCT 操作;可能是 Teradata 自动为您进行转换。

于 2012-10-02T07:06:07.877 回答