我正在尝试在SQL中返回一组项目的第二个最近日期,但是我收到错误'当EXISTS没有引入子查询时,只能在选择列表中指定一个表达式。 这是我的代码:
SELECT ProjectId, max(CreatedDateTime)
FROM NTNote.dbo.nProjectReference P
INNER JOIN NTNote.dbo.nJournal J
ON J.JournalID = P.JournalID
Where CreatedDateTime < (SELECT max(CreatedDateTime), projectid
FROM NTNote.dbo.nProjectReference P
INNER JOIN NTNote.dbo.nJournal J
ON J.JournalID = P.JournalID
Group by ProjectId)
Group by ProjectId
非常感谢任何帮助!
答案 0 :(得分:3)
使用以下代码进行所需的输出(sql server)。
with cte_grp
as
(SELECT ProjectId,CreatedDateTime,ROW_NUMBER () OVER(PARTITION BY ProjectId ORDER BY CreatedDateTime desc) RNo
FROM NTNote.dbo.nProjectReference P
INNER JOIN NTNote.dbo.nJournal J
ON J.JournalID = P.JournalID)
SELECT *
FROM cte_grp
WHERE RNO=2
答案 1 :(得分:0)
您可以使用
Row_Number() over (partition by ProjectId order by CreatedDateTime desc) as rownum
并使用2作为谓词来获取输出。