在Sql Server中按一列分组,并按未包含在聚合函数或Group by子句中的另一列排序
我有一个表“ApplicationEventSearch”,有两列关键字和EventDate 我希望有一个查询,它以distinct关键字返回结果,但按EventDate排序asc 我试过很多组合,但都不起作用 资料 期望结果在Sql Server中按一列分组,并按未包含在聚合函数或Group by子句中的另一列排序,sql,sql-server,date,sql-order-by,Sql,Sql Server,Date,Sql Order By,我有一个表“ApplicationEventSearch”,有两列关键字和EventDate 我希望有一个查询,它以distinct关键字返回结果,但按EventDate排序asc 我试过很多组合,但都不起作用 资料 期望结果 1-123457fdfdfdfd, 2-123457 到目前为止我都试过了 SELECT [Keyword],EventDate FROM [NavaarDb-Dev].[dbo].[ApplicationEventsSearch] group
1-123457fdfdfdfd,
2-123457
到目前为止我都试过了
SELECT
[Keyword],EventDate
FROM [NavaarDb-Dev].[dbo].[ApplicationEventsSearch]
group by Keyword,EventDate
order by EventDate
SELECT
distinct [Keyword],EventDate
FROM [NavaarDb-Dev].[dbo].[ApplicationEventsSearch]
group by Keyword,EventDate
order by EventDate
您不是在按排序的
中只查找MIN
或MAX
吗
SELECT关键字
从dbo.YourTable
按关键字分组
按最大(事件日期)ASC订购;
您不是在按
排序的MIN
或MAX
之后吗
SELECT关键字
从dbo.YourTable
按关键字分组
按最大(事件日期)ASC订购;
这回答了这个问题
资料
质疑
结果
string
1-123457fdfdfdfd
2-123457
这就回答了问题 资料 质疑 结果
string
1-123457fdfdfdfd
2-123457
;with kw_cte as (select *, row_number() over (partition by Keyword order by EventDate desc) rn from #tTEST)
select
concat_ws('-', row_number() over (order by EventDate desc), Keyword) string
from kw_cte
where rn=1;
string
1-123457fdfdfdfd
2-123457