如下所示:How to limit an SQL query to return at most one of something?
我有以下表格:
平台
产品
唱片集
版本表存储特定平台的软件产品的每个版本。我想编写一个查询,以返回Releases表中每个平台的每个产品的最新版本。 I.E.如果我在平台A,B和C上发布产品X版本2.0,则Releases表中将有3条记录:
因此,给定产品ID,我如何获取此信息?我尝试使用GROUP BY像我使用的LINQ解决方案,但显然我无法将LINQ转换为SQL以及.NET。
答案 0 :(得分:2)
迂腐:
Select
PR.Name,
PL.Name,
REL.Version,
REL.ReleaseDate
From
Releases As REL
Inner Join Products As PR
On REL.ProductID = PR.ProductID
Inner Join Platforms As PL
On REL.PlatformID = PL.PlatformID
Where
REL.ID = (Select Top 1 Latest.ID
From Releases As Latest
Where Latest.PlatformID = REL.PlatformID
And Latest.ProductID = REL.ProductID
Order By Latest.ReleaseDate Desc)
也应该有效(假设你从不减少版本):
Select
PR.Name,
PL.Name,
Max(REL.Version),
Max(REL.ReleaseDate)
From
Releases As REL
Inner Join Products As PR
On REL.ProductID = PR.ProductID
Inner Join Platforms As PL
On REL.PlatformID = PL.PlatformID
Group By
PR.Name,
PL.Name
答案 1 :(得分:2)
您知道LINQ的DataContext类有Log property吗?
既然你已经有了LINQ查询,为什么不偷看幕后呢? : - )