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ステートメントがdistinctを実行するため、重複を含めるのt1.colIdではなく、distinctの数を取得することです。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、ただし、私は IBM の Informix で作業しているので、他の人の DBMS でアスペルションをキャストすることに注意する必要があります)。

問題を誤解している場合は、何が起こっているのかをより明確に確認できるように、サンプルのテーブル スキーマと値を実際の出力と予想される出力とともに提供してください。Explain 出力も提供したい場合があります。


クエリを次のように書き直すことができることに注意してください。

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 に答える