以下查询结果记录如下。
如何仅获取记录的最大行数?我已经对该查询进行了20,000条以上的记录,以识别重复项并获取最新日期及其处理的数量(RowNumber)。
select [FileName], ProcessDate,
ROW_NUMBER() over (partition by [FileName] order by [FileName] desc) RowNumber
from StagingTable
where filename = 'abc.txt'
答案 0 :(得分:1)
您要寻找过滤器吗?
select s.*
from (select [FileName], ProcessDate,
ROW_NUMBER() over (partition by [FileName] order by ProcessDate desc) as RowNumber
from StagingTable
where filename = 'abc.txt'
) s
where RowNumber = 1;
请注意,ORDER BY
已更改。
如果您还需要总数,则将其添加为附加列:
select s.*
from (select [FileName], ProcessDate,
count(*) over (partition by filename) as cnt,
ROW_NUMBER() over (partition by [FileName] order by ProcessDate desc) as RowNumber
from StagingTable
where filename = 'abc.txt'
) s
where RowNumber = 1;
答案 1 :(得分:1)
根据您的逻辑,似乎没有最大行数。还是行号绑定到ProcessDate
以外的其他字段?
据我了解您的问题,我将使用以下查询:
SELECT [FileName], MAX([ProcessDate]) AS [ProcessDate], COUNT(*) AS [Count]
FROM [StagingTable]
GROUP BY [FileName]
答案 2 :(得分:0)
您的查询有一个错误:您在OrderBy
中写了FileName,但是我想您使用的是日期。
您可以在ROW_NUMBER()列上加倍:
WITH VersionSorting AS (
select
[FileName],
ProcessDate,
ROW_NUMBER() over (partition by [FileName] order by [ProcessDate] ASC) FileVersion,
ROW_NUMBER() over (partition by [FileName] order by [ProcessDate] DESC) FileVersionInv
from StagingTable
)
SELECT FileName, ProcessDate, FileVersion
FROM VersionSorting
WHERE FileVersionInv = 1
-- AND FileName = 'abc.txt'