在另一位社区成员的帮助下,我成功地构建了一个运行良好的选择。
但是,现在我需要执行 JOIN 以从另一个表中获取信息。
SQL 初学者
谢谢
SELECT
t.TICKETID -- Here is the information from the other table
,c.OWNERID
,c.CREATEDATE
FROM
(
SELECT
*
, ROW_NUMBER () OVER (PARTITION BY c.OWNERID ORDER BY c.CREATEDATE) AS RN_
FROM MAXIMO.COMMLOG c
left join maximo.TICKET t on t.TICKETUID = c.OWNERID -- here is my join
WHERE c.UNIQUEID = '468'
AND c.OWNERID = '4355070'
) c
WHERE RN_ = 1
ORDER BY c.CREATEDATE DESC
**没有 JOIN 的结果**
所有者ID | 创建日期 |
---|---|
16031985 | 22022-08-11-08.02.30.351000 |
**加入的预期结果**
门票ID | 所有者ID | 创建日期 |
---|---|---|
123456 | 16031985 | 2022-08-11-08.02.30.351000 |
JOIN 错误“Erro SQL [42601]:在“
”之后发现意外标记“.”c"。预期的标记可能包括:",".. SQLCODE=-104, SQLSTATE=42601, DRIVER=4.31.10"
子选择的相关名称
t
和 c
在外部 SELECT 中不可见。c
,这就是为什么您可以使用此相关名称来限定那里的列。SELECT
c.TICKETID -- Here is the information from the other table
,c.OWNERID
,c.CREATEDATE
FROM
(
SELECT
c.*
, t.TICKETID
, ROW_NUMBER () OVER (PARTITION BY c.OWNERID ORDER BY c.CREATEDATE) AS RN_
FROM MAXIMO.COMMLOG c
left join maximo.TICKET t on t.TICKETUID = c.OWNERID -- here is my join
WHERE c.UNIQUEID = '468'
AND c.OWNERID = '4355070'
) c
WHERE RN_ = 1
ORDER BY c.CREATEDATE DESC