Sql Server 2000 拼图
Sql Server 2000 Puzzle
SELECT Id ,
ActionDate ,
Operation ,
Date ,
[Payroll Number]
FROM
[dbo].[tblx]
1 2018-01-10 00:00:00.000 NEW 2018-01-30 00:00:00 X0154X
2 2018-01-10 00:00:00.000 NEW 2018-01-31 00:00:00 X0154X
3 2018-01-10 00:00:00.000 NEW 2018-02-01 00:00:00 X0154X
4 2018-01-10 00:00:00.000 DELETE 2018-02-01 00:00:00 X0154X
5 2018-01-10 00:00:00.000 NEW 2018-02-02 00:00:00 X0154X
6 2018-01-10 00:00:00.000 NEW 2018-02-11 00:00:00 X0154X
我需要 Payroll/Date 组合的基于行动日期的最新记录。所以我在 SQL 服务器 2000 上尝试了这个。但是,问题是 ROW_NUMBER()
直到 SQL 服务器 2008 才被引入。
这给了我 4 条记录而不是 5 条记录。 ID 为 4 的记录未返回,因为与操作日期相关联。
SELECT Id ,
ActionDate ,
Operation ,
Date ,
[Payroll Number]
FROM (SELECT *, (SELECT COUNT(*)
FROM dbo.tblx AS counter
WHERE counter.[Payroll Number] = dbo.tblx.[Payroll Number]
AND counter.[Date] = dbo.tblx.[Date]
AND counter.ActionDate >= dbo.tblx.ActionDate) AS rn
FROM dbo.tblx
WHERE Status IN ( 0, 5 )
AND Category = 'Holiday') AS r1
WHERE r1.rn = 1
Id ActionDate Operation Date Payroll Number
1 2018-01-10 00:00:00.000 NEW 2018-01-30 00:00:00 X0154X
2 2018-01-10 00:00:00.000 NEW 2018-01-31 00:00:00 X0154X
5 2018-01-10 00:00:00.000 NEW 2018-02-02 00:00:00 X0154X
6 2018-01-10 00:00:00.000 NEW 2018-02-11 00:00:00 X0154X
有没有办法在 SQL Server 2000 上 ROW_NUMBER()
获得这个?
在 window 活动前几天,您可以:
SELECT x.*
FROM [dbo].[tblx] x
WHERE x.ActionDate = (SELECT MAX(x2.ActionDate)
FROM [dbo].[tblx] x2
WHERE x2.[Payroll Number] = x.[Payroll Number] AND
x2.Date = x.Date
);
此语法用于 MySQL、SQLite 和 MS Access 等不支持 window 函数的数据库。
如果您可以通过 id
定义 "latest",则在子查询中使用它代替日期:
SELECT x.*
FROM [dbo].[tblx] x
WHERE x.id = (SELECT MAX(x2.id)
FROM [dbo].[tblx] x2
WHERE x2.[Payroll Number] = x.[Payroll Number] AND
x2.Date = x.Date
);
这有效但不是很好的代码或 sql。
SELECT
a.ActionDate ,
a.Date ,
a.[Payroll Number]
, MAXC( a.id) AS ID_Required_To_Porcess
FROM
[dbo].[tblx] a
INNER JOIN (
SELECT
x.ActionDate ,
x.Date ,
x.[Payroll Number]
FROM [dbo].[tblx] x
WHERE x.ActionDate = (SELECT MAX(x2.ActionDate)
FROM [dbo].[tblx] x2
WHERE x2.[Payroll Number] = x.[Payroll Number] AND
x2.Date = x.Date
)
GROUP BY x.ActionDate ,
x.Date ,
x.[Payroll Number]
) b ON a.ActionDate=b.actionDate AND a.date=b.date AND a.[Payroll Number]=b.[Payroll Number]
GROUP BY a.ActionDate ,
a.Date ,
a.[Payroll Number]
SELECT Id ,
ActionDate ,
Operation ,
Date ,
[Payroll Number]
FROM
[dbo].[tblx]
1 2018-01-10 00:00:00.000 NEW 2018-01-30 00:00:00 X0154X
2 2018-01-10 00:00:00.000 NEW 2018-01-31 00:00:00 X0154X
3 2018-01-10 00:00:00.000 NEW 2018-02-01 00:00:00 X0154X
4 2018-01-10 00:00:00.000 DELETE 2018-02-01 00:00:00 X0154X
5 2018-01-10 00:00:00.000 NEW 2018-02-02 00:00:00 X0154X
6 2018-01-10 00:00:00.000 NEW 2018-02-11 00:00:00 X0154X
我需要 Payroll/Date 组合的基于行动日期的最新记录。所以我在 SQL 服务器 2000 上尝试了这个。但是,问题是 ROW_NUMBER()
直到 SQL 服务器 2008 才被引入。
这给了我 4 条记录而不是 5 条记录。 ID 为 4 的记录未返回,因为与操作日期相关联。
SELECT Id ,
ActionDate ,
Operation ,
Date ,
[Payroll Number]
FROM (SELECT *, (SELECT COUNT(*)
FROM dbo.tblx AS counter
WHERE counter.[Payroll Number] = dbo.tblx.[Payroll Number]
AND counter.[Date] = dbo.tblx.[Date]
AND counter.ActionDate >= dbo.tblx.ActionDate) AS rn
FROM dbo.tblx
WHERE Status IN ( 0, 5 )
AND Category = 'Holiday') AS r1
WHERE r1.rn = 1
Id ActionDate Operation Date Payroll Number
1 2018-01-10 00:00:00.000 NEW 2018-01-30 00:00:00 X0154X
2 2018-01-10 00:00:00.000 NEW 2018-01-31 00:00:00 X0154X
5 2018-01-10 00:00:00.000 NEW 2018-02-02 00:00:00 X0154X
6 2018-01-10 00:00:00.000 NEW 2018-02-11 00:00:00 X0154X
有没有办法在 SQL Server 2000 上 ROW_NUMBER()
获得这个?
在 window 活动前几天,您可以:
SELECT x.*
FROM [dbo].[tblx] x
WHERE x.ActionDate = (SELECT MAX(x2.ActionDate)
FROM [dbo].[tblx] x2
WHERE x2.[Payroll Number] = x.[Payroll Number] AND
x2.Date = x.Date
);
此语法用于 MySQL、SQLite 和 MS Access 等不支持 window 函数的数据库。
如果您可以通过 id
定义 "latest",则在子查询中使用它代替日期:
SELECT x.*
FROM [dbo].[tblx] x
WHERE x.id = (SELECT MAX(x2.id)
FROM [dbo].[tblx] x2
WHERE x2.[Payroll Number] = x.[Payroll Number] AND
x2.Date = x.Date
);
这有效但不是很好的代码或 sql。
SELECT
a.ActionDate ,
a.Date ,
a.[Payroll Number]
, MAXC( a.id) AS ID_Required_To_Porcess
FROM
[dbo].[tblx] a
INNER JOIN (
SELECT
x.ActionDate ,
x.Date ,
x.[Payroll Number]
FROM [dbo].[tblx] x
WHERE x.ActionDate = (SELECT MAX(x2.ActionDate)
FROM [dbo].[tblx] x2
WHERE x2.[Payroll Number] = x.[Payroll Number] AND
x2.Date = x.Date
)
GROUP BY x.ActionDate ,
x.Date ,
x.[Payroll Number]
) b ON a.ActionDate=b.actionDate AND a.date=b.date AND a.[Payroll Number]=b.[Payroll Number]
GROUP BY a.ActionDate ,
a.Date ,
a.[Payroll Number]