分组和排序结果以选择 MS SQL 中的最新记录
Grouping and Ordering result to pick the most recent record in MS SQL
我以这样的方式编写了以下查询,它 return 是我需要的数据:
SELECT SLCA.CustomerAccountNumber, SLCD.DefaultEmail, SOR.DocumentDate, SLST.*
FROM SLCustomerAccount AS SLCA
INNER JOIN SOPOrderReturn AS SOR ON SOR.CustomerID = SLCA.SLCustomerAccountID
INNER JOIN SLCustomerContactDefaultsVw AS SLCD ON SLCD.SLCustomerAccountID = SLCA.SLCustomerAccountID
LEFT OUTER JOIN MMS182SLStatus AS SLST ON SLST.Customer = SLCA.SLCustomerAccountID
WHERE SLCD.IsDefaultRole = 1 AND SLCD.DefaultEmail IS NOT NULL AND SLCD.DefaultEmail <> ''
AND SLCD.DefaultEmail = '...major@live.co.uk'
AND SLST.[Status] IS NULL
ORDER BY SOR.DocumentDate DESC
唯一的问题是,我在 where 过滤器中使用的这封电子邮件会产生多个这样的结果(尽管是正确的):
如何使用 group by
删除重复的行,以便我只获得最近 DocumentDate 的一条记录?
我知道我可以做到 TOP 1
但这不是我需要的。因为,我现在正在通过将范围限定到一封具有重复的特定电子邮件来测试查询。稍后,我需要 运行 在没有此行 AND SLCD.DefaultEmail = '...major@live.co.uk'
的所有记录上进行此查询 - 这样您就可以明白为什么我不能使用 TOP 1
.
当我尝试像这样使用 group by
时:
SELECT MAX(SLCA.CustomerAccountNumber), MAX(SLCD.DefaultEmail), MAX(SOR.DocumentDate), MAX(SLST.MMS182SLStatus), MAX(SLST.Customer), MAX(SLST.[Status])
FROM SLCustomerAccount AS SLCA
INNER JOIN SOPOrderReturn AS SOR ON SOR.CustomerID = SLCA.SLCustomerAccountID
INNER JOIN SLCustomerContactDefaultsVw AS SLCD ON SLCD.SLCustomerAccountID = SLCA.SLCustomerAccountID
LEFT OUTER JOIN MMS182SLStatus AS SLST ON SLST.Customer = SLCA.SLCustomerAccountID
WHERE SLCD.IsDefaultRole = 1 AND SLCD.DefaultEmail IS NOT NULL AND SLCD.DefaultEmail <> ''
AND SLCD.DefaultEmail = '...major@live.co.uk'
AND SLST.[Status] IS NULL
GROUP BY SLCD.DefaultEmail
ORDER BY MAX(SOR.DocumentDate) DESC
它 return 是错误的结果:
我期待 return 这条记录:
知道我这里可能做错了什么吗?
使用row_number()
:
with q as (<your query here without the `order by`>)
select q.*
from (select q.*,
row_number() over (partition by DefaultEmail order by documentdate desc) as seqnum
from q
) q
where seqnum = 1;
我以这样的方式编写了以下查询,它 return 是我需要的数据:
SELECT SLCA.CustomerAccountNumber, SLCD.DefaultEmail, SOR.DocumentDate, SLST.*
FROM SLCustomerAccount AS SLCA
INNER JOIN SOPOrderReturn AS SOR ON SOR.CustomerID = SLCA.SLCustomerAccountID
INNER JOIN SLCustomerContactDefaultsVw AS SLCD ON SLCD.SLCustomerAccountID = SLCA.SLCustomerAccountID
LEFT OUTER JOIN MMS182SLStatus AS SLST ON SLST.Customer = SLCA.SLCustomerAccountID
WHERE SLCD.IsDefaultRole = 1 AND SLCD.DefaultEmail IS NOT NULL AND SLCD.DefaultEmail <> ''
AND SLCD.DefaultEmail = '...major@live.co.uk'
AND SLST.[Status] IS NULL
ORDER BY SOR.DocumentDate DESC
唯一的问题是,我在 where 过滤器中使用的这封电子邮件会产生多个这样的结果(尽管是正确的):
如何使用 group by
删除重复的行,以便我只获得最近 DocumentDate 的一条记录?
我知道我可以做到 TOP 1
但这不是我需要的。因为,我现在正在通过将范围限定到一封具有重复的特定电子邮件来测试查询。稍后,我需要 运行 在没有此行 AND SLCD.DefaultEmail = '...major@live.co.uk'
的所有记录上进行此查询 - 这样您就可以明白为什么我不能使用 TOP 1
.
当我尝试像这样使用 group by
时:
SELECT MAX(SLCA.CustomerAccountNumber), MAX(SLCD.DefaultEmail), MAX(SOR.DocumentDate), MAX(SLST.MMS182SLStatus), MAX(SLST.Customer), MAX(SLST.[Status])
FROM SLCustomerAccount AS SLCA
INNER JOIN SOPOrderReturn AS SOR ON SOR.CustomerID = SLCA.SLCustomerAccountID
INNER JOIN SLCustomerContactDefaultsVw AS SLCD ON SLCD.SLCustomerAccountID = SLCA.SLCustomerAccountID
LEFT OUTER JOIN MMS182SLStatus AS SLST ON SLST.Customer = SLCA.SLCustomerAccountID
WHERE SLCD.IsDefaultRole = 1 AND SLCD.DefaultEmail IS NOT NULL AND SLCD.DefaultEmail <> ''
AND SLCD.DefaultEmail = '...major@live.co.uk'
AND SLST.[Status] IS NULL
GROUP BY SLCD.DefaultEmail
ORDER BY MAX(SOR.DocumentDate) DESC
它 return 是错误的结果:
我期待 return 这条记录:
知道我这里可能做错了什么吗?
使用row_number()
:
with q as (<your query here without the `order by`>)
select q.*
from (select q.*,
row_number() over (partition by DefaultEmail order by documentdate desc) as seqnum
from q
) q
where seqnum = 1;