Double LEFT JOIN on Same Table for Transpose
Double LEFT JOIN on Same Table for Transpose
IBM iNavigator(或generic/agnosticSQL):
我正在使用一个 IBM 系统,该系统将评论保存在 25 个字符的文本块中。我正在尝试为每个已保存的评论重新组合前三段文本块。评论table有评论header编号(COMM_NO)、行序号(LINE_SQ)和实际文本(TXT)。我正在查看三个示例评论。评论 #1140 说 "Customer product return after warranty expired. Needs quote issued." 评论 #1408 是两段,"Tried to call customer after update." 评论 #2884 很短,只说 "RMA #467"。 Table 数据看起来像这样 [编辑:更正插入语句]:
CREATE TABLE mycomments
([COMM_NO] int, [LINE_SQ] int, [TXT] varchar(25))
;
INSERT INTO mycomments
([COMM_NO], [LINE_SQ], [TXT])
VALUES
(1140, 1, 'Customer product return a'),
(1140, 2, 'fter warranty expired. Ne'),
(1140, 3, 'eds quote issued.'),
(1408, 1, 'Tried to call customer af'),
(1408, 2, 'ter update.'),
(2884, 1, 'RMA #467');
而我需要的是:
COMM_NO TXT1 TXT2 TXT3
------- ------------------------- ------------------------- -------------------------
1140 Customer product return a fter warranty expired. Ne eds quote issued.
1408 Tried to call customer af ter update. {null}
2884 RMA #467 {null} {null}
我多次尝试使用别名将 table 自身左连接两次。我还没有完全做到,但这是我目前为止最接近的尝试:
SELECT
comm1.COMM_NO
, comm1.TXT as TXT1
, comm2.TXT as TXT2
, comm3.TXT as TXT3
FROM mycomments comm1
LEFT JOIN mycomments AS comm2 ON comm2.COMM_NO = comm1.COMM_NO AND comm1.LINE_SQ=1 AND comm2.LINE_SQ=2
LEFT JOIN mycomments AS comm3 ON comm3.COMM_NO = comm1.COMM_NO AND comm1.LINE_SQ=1 AND comm2.LINE_SQ=2 AND comm3.LINE_SQ=3
其实我真的很想把三个段连接在一起,即使 TXT3 或 TXT2/TXT3 上面是这样的空值:
COMM_NO BIGTXT1
------- ---------------------------------------------------------------------------
1140 Customer product return after warranty expired. Needs quote issued.
1408 Tried to call customer after update.
2884 RMA #467
感谢任何帮助。谢谢!
首先,在提供 DDL 和 INSERT 语句方面做得很好。您遇到的主要问题是您对 JOIN 定义的 table 顺序。 OUTER
加入的顺序很重要。由于您要针对 comm1
发出 LEFT
连接,所以它始终需要在您的 ON
语句中排在第一位。换句话说,你说的是 "always give me data from comm1, and give me whatever is in my joined table if there is data there." 所以我重写了它:
SELECT
comm1.COMM_NO
, comm1.TXT as TXT1
, comm2.TXT as TXT2
, comm3.TXT as TXT3
FROM mycomments comm1
LEFT JOIN mycomments AS comm2 ON comm1.COMM_NO = comm2.COMM_NO AND comm2.LINE_SQ=2
LEFT JOIN mycomments AS comm3 ON comm1.COMM_NO = comm3.COMM_NO AND comm3.LINE_SQ=3
WHERE comm1.LINE_SQ = 1
注意 - 我还将 comm1.LINE_SQ = 1
条件放在 WHERE
子句中,因此您不必在 JOIN 上重复它。我使用您提供的 DDL 进行了测试,它按预期工作。
如果你想在一个大专栏中,你可以为你的 SELECT
语句做这样的事情:
comm1.TXT + CASE WHEN comm2.TXT IS NOT NULL THEN comm2.TXT ELSE '' END + CASE WHEN comm3.TXT IS NOT NULL THEN comm3.TXT ELSE '' END BIGTXT1
IBM iNavigator(或generic/agnosticSQL):
我正在使用一个 IBM 系统,该系统将评论保存在 25 个字符的文本块中。我正在尝试为每个已保存的评论重新组合前三段文本块。评论table有评论header编号(COMM_NO)、行序号(LINE_SQ)和实际文本(TXT)。我正在查看三个示例评论。评论 #1140 说 "Customer product return after warranty expired. Needs quote issued." 评论 #1408 是两段,"Tried to call customer after update." 评论 #2884 很短,只说 "RMA #467"。 Table 数据看起来像这样 [编辑:更正插入语句]:
CREATE TABLE mycomments
([COMM_NO] int, [LINE_SQ] int, [TXT] varchar(25))
;
INSERT INTO mycomments
([COMM_NO], [LINE_SQ], [TXT])
VALUES
(1140, 1, 'Customer product return a'),
(1140, 2, 'fter warranty expired. Ne'),
(1140, 3, 'eds quote issued.'),
(1408, 1, 'Tried to call customer af'),
(1408, 2, 'ter update.'),
(2884, 1, 'RMA #467');
而我需要的是:
COMM_NO TXT1 TXT2 TXT3
------- ------------------------- ------------------------- -------------------------
1140 Customer product return a fter warranty expired. Ne eds quote issued.
1408 Tried to call customer af ter update. {null}
2884 RMA #467 {null} {null}
我多次尝试使用别名将 table 自身左连接两次。我还没有完全做到,但这是我目前为止最接近的尝试:
SELECT
comm1.COMM_NO
, comm1.TXT as TXT1
, comm2.TXT as TXT2
, comm3.TXT as TXT3
FROM mycomments comm1
LEFT JOIN mycomments AS comm2 ON comm2.COMM_NO = comm1.COMM_NO AND comm1.LINE_SQ=1 AND comm2.LINE_SQ=2
LEFT JOIN mycomments AS comm3 ON comm3.COMM_NO = comm1.COMM_NO AND comm1.LINE_SQ=1 AND comm2.LINE_SQ=2 AND comm3.LINE_SQ=3
其实我真的很想把三个段连接在一起,即使 TXT3 或 TXT2/TXT3 上面是这样的空值:
COMM_NO BIGTXT1
------- ---------------------------------------------------------------------------
1140 Customer product return after warranty expired. Needs quote issued.
1408 Tried to call customer after update.
2884 RMA #467
感谢任何帮助。谢谢!
首先,在提供 DDL 和 INSERT 语句方面做得很好。您遇到的主要问题是您对 JOIN 定义的 table 顺序。 OUTER
加入的顺序很重要。由于您要针对 comm1
发出 LEFT
连接,所以它始终需要在您的 ON
语句中排在第一位。换句话说,你说的是 "always give me data from comm1, and give me whatever is in my joined table if there is data there." 所以我重写了它:
SELECT
comm1.COMM_NO
, comm1.TXT as TXT1
, comm2.TXT as TXT2
, comm3.TXT as TXT3
FROM mycomments comm1
LEFT JOIN mycomments AS comm2 ON comm1.COMM_NO = comm2.COMM_NO AND comm2.LINE_SQ=2
LEFT JOIN mycomments AS comm3 ON comm1.COMM_NO = comm3.COMM_NO AND comm3.LINE_SQ=3
WHERE comm1.LINE_SQ = 1
注意 - 我还将 comm1.LINE_SQ = 1
条件放在 WHERE
子句中,因此您不必在 JOIN 上重复它。我使用您提供的 DDL 进行了测试,它按预期工作。
如果你想在一个大专栏中,你可以为你的 SELECT
语句做这样的事情:
comm1.TXT + CASE WHEN comm2.TXT IS NOT NULL THEN comm2.TXT ELSE '' END + CASE WHEN comm3.TXT IS NOT NULL THEN comm3.TXT ELSE '' END BIGTXT1