在 SQL 服务器 2008 中使用分组依据和排序依据检索结果时出错
Got Error in Retrieving results using group by and order by in SQL server 2008
我有一个名为 Scripts 的 table,其中包含修改后的过程、函数和 tables 的数据。
CREATE TABLE #Scripts
(
ID NUMERIC (18) IDENTITY NOT NULL,
[Date] DATETIME NULL,
DatabaseName VARCHAR (50) NULL,
Name VARCHAR (100) NULL,
Type VARCHAR (20) NULL,
Action VARCHAR (50) NULL,
Description VARCHAR (500) NULL,
ModifiedBy VARCHAR (50) NULL,
AddedTimestamp DATETIME NULL,
UpdateTimestamp DATETIME NULL,
)
GO
然后我将记录添加到 table 中,如下所示。这些只是示例记录。
INSERT INTO #Scripts ([Date], DatabaseName, Name, Type, Action, Description, ModifiedBy, AddedTimestamp, UpdateTimestamp)
VALUES ('2015-01-07 11:16:41.4', 'Test', 'sp_GetData', 'Stored Procedure', 'Created', 'To Get ActivitySubscriptions for Mobile from tblSubscriptions', 'dinesh.alla', '2015-01-07 11:39:39.703', '2015-01-07 11:39:39.703')
GO
INSERT INTO #Scripts ([Date], DatabaseName, Name, Type, Action, Description, ModifiedBy, AddedTimestamp, UpdateTimestamp)
VALUES ('2015-01-07 11:16:41.4', 'Test', 'sp_GetData', 'Stored Procedure', 'Updated', 'To Get ActivitySubscriptions for Mobile from tblSubscriptions', 'dinesh.alla', '2015-01-07 11:39:39.703', '2015-01-07 11:39:39.703')
GO
INSERT INTO #Scripts ([Date], DatabaseName, Name, Type, Action, Description, ModifiedBy, AddedTimestamp, UpdateTimestamp)
VALUES ('2015-01-07 11:16:41.4', 'Test', 'sp_GetData', 'Stored Procedure', 'Deleted', 'To Get ActivitySubscriptions for Mobile from tblSubscriptions', 'dinesh.alla', '2015-01-07 11:39:39.703', '2015-01-07 11:39:39.703')
GO
INSERT INTO #Scripts ([Date], DatabaseName, Name, Type, Action, Description, ModifiedBy, AddedTimestamp, UpdateTimestamp)
VALUES ('2015-01-07 11:16:41.4', 'Test', 'sp_UpdateData', 'Stored Procedure', 'Created', 'To Get ActivitySubscriptions for Mobile from tblSubscriptions', 'dinesh.alla', '2015-01-07 11:39:39.703', '2015-01-07 11:39:39.703')
GO
INSERT INTO #Scripts ([Date], DatabaseName, Name, Type, Action, Description, ModifiedBy, AddedTimestamp, UpdateTimestamp)
VALUES ('2015-01-07 11:16:41.4', 'Test', 'sp_UpdateData', 'Stored Procedure', 'Updated', 'To Get ActivitySubscriptions for Mobile from tblSubscriptions', 'dinesh.alla', '2015-01-07 11:39:39.703', '2015-01-07 11:39:39.703')
GO
INSERT INTO #Scripts ([Date], DatabaseName, Name, Type, Action, Description, ModifiedBy, AddedTimestamp, UpdateTimestamp)
VALUES ('2015-01-07 11:16:41.4', 'Test', 'sp_AddData', 'Stored Procedure', 'Created', 'To Get ActivitySubscriptions for Mobile from tblSubscriptions', 'dinesh.alla', '2015-01-07 11:39:39.703', '2015-01-07 11:39:39.703')
GO
我尝试得到如下所示的结果
SELECT MAX(ID) AS ID,MAX(Action) AS Action
FROM #Scripts GROUP BY Name ORDER BY ID ASC
输出:
ID Action
3 Updated
5 Updated
6 Created
预期输出:
ID Action
3 Deleted
5 Updated
6 Created
试试这个:
select distinct sc.action,sc.ID from (
SELECT MAX(ID) OVER(PARTITION BY NAME ) rn,* FROM #Scripts
)d join #Scripts sc
on d.rn=sc.ID
order by sc.ID
输出
action ID
Deleted 3
Updated 5
Created 6
假设您将其中两个操作设置为 'a' 和 'z' ..
那么 select max(Actions) 的结果将是 z
如果你想要预期的输出那么你可以尝试这样的事情
select TOP 1 MAX(ID) AS ID ,Action
FROM #Scripts where Name = 'sp_GetData'
GROUP BY Action
ORDER BY MAX(ID) DESC
请仔细阅读 this link 你会明白的
;WITH LatestChanges AS
(
SELECT S.[ID], S.[Date], S.[DatabaseName], S.[Name], S.[Type], S.[Action], S.[Description],
S.[ModifiedBy], S.[AddedTimestamp], S.[UpdateTimestamp],
[RowNum] = ROW_NUMBER() OVER (PARTITION BY S.[Name] ORDER BY S.[ID] DESC)
FROM [#Scripts] S
)
SELECT *
FROM LatestChanges
WHERE [RowNum] = 1
ORDER BY ID
每个 [Name] 的 returns 最后一个 [ID]。
试试这个,
SELECT * FROM
(
SELECT *,
ROW_NUMBER() OVER(PARTITION BY NAME ORDER BY ID DESC )RN
FROM @Scripts
)T4
WHERE RN=1
我有一个名为 Scripts 的 table,其中包含修改后的过程、函数和 tables 的数据。
CREATE TABLE #Scripts
(
ID NUMERIC (18) IDENTITY NOT NULL,
[Date] DATETIME NULL,
DatabaseName VARCHAR (50) NULL,
Name VARCHAR (100) NULL,
Type VARCHAR (20) NULL,
Action VARCHAR (50) NULL,
Description VARCHAR (500) NULL,
ModifiedBy VARCHAR (50) NULL,
AddedTimestamp DATETIME NULL,
UpdateTimestamp DATETIME NULL,
)
GO
然后我将记录添加到 table 中,如下所示。这些只是示例记录。
INSERT INTO #Scripts ([Date], DatabaseName, Name, Type, Action, Description, ModifiedBy, AddedTimestamp, UpdateTimestamp)
VALUES ('2015-01-07 11:16:41.4', 'Test', 'sp_GetData', 'Stored Procedure', 'Created', 'To Get ActivitySubscriptions for Mobile from tblSubscriptions', 'dinesh.alla', '2015-01-07 11:39:39.703', '2015-01-07 11:39:39.703')
GO
INSERT INTO #Scripts ([Date], DatabaseName, Name, Type, Action, Description, ModifiedBy, AddedTimestamp, UpdateTimestamp)
VALUES ('2015-01-07 11:16:41.4', 'Test', 'sp_GetData', 'Stored Procedure', 'Updated', 'To Get ActivitySubscriptions for Mobile from tblSubscriptions', 'dinesh.alla', '2015-01-07 11:39:39.703', '2015-01-07 11:39:39.703')
GO
INSERT INTO #Scripts ([Date], DatabaseName, Name, Type, Action, Description, ModifiedBy, AddedTimestamp, UpdateTimestamp)
VALUES ('2015-01-07 11:16:41.4', 'Test', 'sp_GetData', 'Stored Procedure', 'Deleted', 'To Get ActivitySubscriptions for Mobile from tblSubscriptions', 'dinesh.alla', '2015-01-07 11:39:39.703', '2015-01-07 11:39:39.703')
GO
INSERT INTO #Scripts ([Date], DatabaseName, Name, Type, Action, Description, ModifiedBy, AddedTimestamp, UpdateTimestamp)
VALUES ('2015-01-07 11:16:41.4', 'Test', 'sp_UpdateData', 'Stored Procedure', 'Created', 'To Get ActivitySubscriptions for Mobile from tblSubscriptions', 'dinesh.alla', '2015-01-07 11:39:39.703', '2015-01-07 11:39:39.703')
GO
INSERT INTO #Scripts ([Date], DatabaseName, Name, Type, Action, Description, ModifiedBy, AddedTimestamp, UpdateTimestamp)
VALUES ('2015-01-07 11:16:41.4', 'Test', 'sp_UpdateData', 'Stored Procedure', 'Updated', 'To Get ActivitySubscriptions for Mobile from tblSubscriptions', 'dinesh.alla', '2015-01-07 11:39:39.703', '2015-01-07 11:39:39.703')
GO
INSERT INTO #Scripts ([Date], DatabaseName, Name, Type, Action, Description, ModifiedBy, AddedTimestamp, UpdateTimestamp)
VALUES ('2015-01-07 11:16:41.4', 'Test', 'sp_AddData', 'Stored Procedure', 'Created', 'To Get ActivitySubscriptions for Mobile from tblSubscriptions', 'dinesh.alla', '2015-01-07 11:39:39.703', '2015-01-07 11:39:39.703')
GO
我尝试得到如下所示的结果
SELECT MAX(ID) AS ID,MAX(Action) AS Action
FROM #Scripts GROUP BY Name ORDER BY ID ASC
输出:
ID Action
3 Updated
5 Updated
6 Created
预期输出:
ID Action
3 Deleted
5 Updated
6 Created
试试这个:
select distinct sc.action,sc.ID from (
SELECT MAX(ID) OVER(PARTITION BY NAME ) rn,* FROM #Scripts
)d join #Scripts sc
on d.rn=sc.ID
order by sc.ID
输出
action ID
Deleted 3
Updated 5
Created 6
假设您将其中两个操作设置为 'a' 和 'z' .. 那么 select max(Actions) 的结果将是 z
如果你想要预期的输出那么你可以尝试这样的事情
select TOP 1 MAX(ID) AS ID ,Action
FROM #Scripts where Name = 'sp_GetData'
GROUP BY Action
ORDER BY MAX(ID) DESC
请仔细阅读 this link 你会明白的
;WITH LatestChanges AS
(
SELECT S.[ID], S.[Date], S.[DatabaseName], S.[Name], S.[Type], S.[Action], S.[Description],
S.[ModifiedBy], S.[AddedTimestamp], S.[UpdateTimestamp],
[RowNum] = ROW_NUMBER() OVER (PARTITION BY S.[Name] ORDER BY S.[ID] DESC)
FROM [#Scripts] S
)
SELECT *
FROM LatestChanges
WHERE [RowNum] = 1
ORDER BY ID
每个 [Name] 的 returns 最后一个 [ID]。
试试这个,
SELECT * FROM
(
SELECT *,
ROW_NUMBER() OVER(PARTITION BY NAME ORDER BY ID DESC )RN
FROM @Scripts
)T4
WHERE RN=1