我想要一种更优雅的方式来按 LastUpdatedDateTime 降序选择下表中的顶级类别。基本上我只想按 CategoryID 查看每个 Category 的最新单行。
我可以通过执行以下操作来实现...
SELECT Category,
MAX(LastUpdateDateTime) as LastUpdateDateTime
INTO #t
FROM Settings
WHERE CatalogID = 123
GROUP BY Category
ORDER BY Category
SELECT s.*
FROM Settings s
INNER JOIN #t t
ON s.Category = t.Category
AND s.LastUpdateDateTime = t.LastUpdateDateTime
--> 设置表
CREATE TABLE [dbo].[Settings](
[ID] [int] IDENTITY(1,1) NOT NULL,
[CatalogID] [int] NOT NULL,
[Category] [varchar](50) NOT NULL,
[Facings] [bit] NOT NULL,
[Quantity] [bit] NOT NULL,
[LastUpdateDateTime] [datetime] NOT NULL,
CONSTRAINT [PK_Settings_1] PRIMARY KEY CLUSTERED
(
[ID] ASC,
[CatalogID] ASC,
[Category] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
这很简单,只是找人为我指出正确的方向,以便更有效地做到这一点。我有什么工作。我想重构一下。
感谢您的关注。
-B