我在SQL查询中非常注意并且无法编写它们。
我需要一个可以在参数(Description
)中传递的查询,它将从数据库返回最新的项目。
dbo.Document
DocumentID (int), Description (nvarchar(255)), CreatedDate (datetime)
查询:
SELECT *
FROM dbo.Document
WHERE Description = "HelpDocument",
select * from (RANK() OVER (PARTITION BY DocumentID order by CreatedDate desc)
答案 0 :(得分:2)
SELECT TOP 1 *
FROM dbo.Document
WHERE Description = @yourparameter
ORDER BY CreatedDate desc
答案 1 :(得分:0)
如果您需要最近的项目,如果表格中没有任何PRIMARY id
键,则可以使用LIMIT 1
检索最近添加的项目。
所以我认为您的查询可能如下:
SELECT *
FROM dbo.Document
WHERE Description = "HelpDocument",
select * from (RANK() OVER (PARTITION BY DocumentID order by CreatedDate desc)
LIMIT 1