如何根据 Teradata SQL 中其他两个 table 中现有的某些值,使用二进制值创建 table?
How to create table with binary values based on existing some values in two other tables in Teradata SQL?
我在 Teradata SQL 中有两个 table,如下所示:
表 1:
ID
10
11
12
表 2:
ID
10
13
14
15
基于上面的两个 table,我需要创建一个如下所示的 table。
所以:
- col: tab1 --> 如果 ID 在 table 1 中,则给他们 1,否则给他们 0。
- col: tab2 --> 如果 ID 在 table 2 中,则给它们 1,否则给它们 0。
想要的结果:
ID
tab1
tab2
10
1
1
11
1
0
12
1
0
13
0
1
14
0
1
15
0
1
我如何在 Teradata SQL 中做到这一点?
Teradata seems to support enough of ISO SQL-2003 natively,因此不需要特定于 Teradata 的 SQL 扩展或专有功能(即完全相同的查询将适用于 MSSQL Server、Oracle、MariaDB 等) .
您需要 UNION
的 table1
和 table2
的值,然后 JOIN 回来,这很简单:
WITH distinctIdValues AS (
SELECT id FROM table1
UNION
SELECT id FROM table2
)
SELECT
dv.id,
CASE WHEN t1.id IS NOT NULL THEN 1 ELSE 0 END AS tab1,
CASE WHEN t2.id IS NOT NULL THEN 1 ELSE 0 END AS tab2
FROM
distinctIdValues AS dv
LEFT OUTER JOIN table1 AS t1 ON dv.id = t1.id
LEFT OUTER JOIN table2 AS t2 ON dv.id = t2.id
然后您可以将此查询用作 VIEW
或将其具体化为新的 TABLE
:
CREATE VIEW foobar AS /* same SQL as above */;
SELECT * FROM foobar;
Teradata 的文档不清楚 how/if a CTE 可以与 INSERT
语句一起使用,因此我将改用内部查询:
CREATE TABLE foobar (
id int NOT NULL PRIMARY KEY,
tab1 byteint NOT NULL,
tab2 byteint NOT NULL
);
INSERT INTO foobar ( id, tab1, tab2 )
SELECT
dv.id,
CASE WHEN t1.id IS NOT NULL THEN 1 ELSE 0 END AS tab1,
CASE WHEN t2.id IS NOT NULL THEN 1 ELSE 0 END AS tab2
FROM
(
SELECT id FROM table1
UNION
SELECT id FROM table2
)
AS dv
LEFT OUTER JOIN table1 AS t1 ON dv.id = t1.id
LEFT OUTER JOIN table2 AS t2 ON dv.id = t2.id
ORDER BY
dv.id
;
或者只是这样:
@dnoeth 提醒我可以简化为:
SELECT
COALESCE( t1.id, t2.id ) AS id,
CASE WHEN t1.id IS NULL THEN 0 ELSE 1 END AS tab1,
CASE WHEN t2.id IS NULL THEN 0 ELSE 1 END AS tab2
FROM
table1 AS t1
FULL OUTER JOIN table2 AS t2 ON t1.id = t2.id
ORDER BY
COALESCE( t1.id, t2.id )
您只需要一个完全外部连接:
SELECT
Coalesce(t1.id, t2.id) AS id
,CASE WHEN t1.id IS NULL THEN 0 ELSE 1 END AS tab1
,CASE WHEN t2.id IS NULL THEN 0 ELSE 1 END AS tab2
FROM table1 AS t1
FULL JOIN table2 AS t2
ON t1.id = t1.id
我在 Teradata SQL 中有两个 table,如下所示:
表 1:
ID |
---|
10 |
11 |
12 |
表 2:
ID |
---|
10 |
13 |
14 |
15 |
基于上面的两个 table,我需要创建一个如下所示的 table。 所以:
- col: tab1 --> 如果 ID 在 table 1 中,则给他们 1,否则给他们 0。
- col: tab2 --> 如果 ID 在 table 2 中,则给它们 1,否则给它们 0。
想要的结果:
ID | tab1 | tab2 |
---|---|---|
10 | 1 | 1 |
11 | 1 | 0 |
12 | 1 | 0 |
13 | 0 | 1 |
14 | 0 | 1 |
15 | 0 | 1 |
我如何在 Teradata SQL 中做到这一点?
Teradata seems to support enough of ISO SQL-2003 natively,因此不需要特定于 Teradata 的 SQL 扩展或专有功能(即完全相同的查询将适用于 MSSQL Server、Oracle、MariaDB 等) .
您需要 UNION
的 table1
和 table2
的值,然后 JOIN 回来,这很简单:
WITH distinctIdValues AS (
SELECT id FROM table1
UNION
SELECT id FROM table2
)
SELECT
dv.id,
CASE WHEN t1.id IS NOT NULL THEN 1 ELSE 0 END AS tab1,
CASE WHEN t2.id IS NOT NULL THEN 1 ELSE 0 END AS tab2
FROM
distinctIdValues AS dv
LEFT OUTER JOIN table1 AS t1 ON dv.id = t1.id
LEFT OUTER JOIN table2 AS t2 ON dv.id = t2.id
然后您可以将此查询用作 VIEW
或将其具体化为新的 TABLE
:
CREATE VIEW foobar AS /* same SQL as above */;
SELECT * FROM foobar;
Teradata 的文档不清楚 how/if a CTE 可以与 INSERT
语句一起使用,因此我将改用内部查询:
CREATE TABLE foobar (
id int NOT NULL PRIMARY KEY,
tab1 byteint NOT NULL,
tab2 byteint NOT NULL
);
INSERT INTO foobar ( id, tab1, tab2 )
SELECT
dv.id,
CASE WHEN t1.id IS NOT NULL THEN 1 ELSE 0 END AS tab1,
CASE WHEN t2.id IS NOT NULL THEN 1 ELSE 0 END AS tab2
FROM
(
SELECT id FROM table1
UNION
SELECT id FROM table2
)
AS dv
LEFT OUTER JOIN table1 AS t1 ON dv.id = t1.id
LEFT OUTER JOIN table2 AS t2 ON dv.id = t2.id
ORDER BY
dv.id
;
或者只是这样:
@dnoeth 提醒我可以简化为:
SELECT
COALESCE( t1.id, t2.id ) AS id,
CASE WHEN t1.id IS NULL THEN 0 ELSE 1 END AS tab1,
CASE WHEN t2.id IS NULL THEN 0 ELSE 1 END AS tab2
FROM
table1 AS t1
FULL OUTER JOIN table2 AS t2 ON t1.id = t2.id
ORDER BY
COALESCE( t1.id, t2.id )
您只需要一个完全外部连接:
SELECT
Coalesce(t1.id, t2.id) AS id
,CASE WHEN t1.id IS NULL THEN 0 ELSE 1 END AS tab1
,CASE WHEN t2.id IS NULL THEN 0 ELSE 1 END AS tab2
FROM table1 AS t1
FULL JOIN table2 AS t2
ON t1.id = t1.id