这里有一个表,我想在SQL Server中在其中编写一个SELECT查询,该查询允许我获得以下内容:
- 对于SalesPerson x Country的每个唯一组合,仅获取Upload_DateTime最新的行
然而,我尝试做一个group-by和inner join,但是没有用。我的代码如下:
SELECT t1.[SalesPerson], t1.[Country], MAX(t1.[Upload_DateTime]) as [Upload_DateTime]
FROM [dbo].[CommentTable] AS t1
GROUP BY t1.[SalesPerson], t1.[Country]
INNER JOIN SELECT * FROM [dbo].[CommentTable] as t2 ON t1.[SalesPerson] = t2.[SalesPerson], t1.[Country] = t2.[Country]
看起来GROUP BY需要在INNER JOIN之外完成?这是如何工作的?当我运行查询时,我得到了一个错误,似乎我的SQL不正确。
1条答案
按热度按时间xcitsw881#
基本上,这个子查询将获取人员、国家和最新日期:
这可以用在很多方面(例如,
JOIN
或IN
子句)。主查询会将剩余的列添加到结果中。
因为您尝试了
JOIN
,所以这里是JOIN
选项:试用:db<>fiddle