SQL Server:SELECT DISTINCT Group By with Count

问题描述 投票:1回答:5

我有一张桌子如下。

我可以按上述方式输出吗?

谢谢

sql-server tsql select count distinct
5个回答
0
投票

这是执行此操作的MYSQL查询

SELECT id,date,msg,COUNT(id)AS count FROM table_name GROUP BY id


0
投票

请尝试以下查询:

SELECT t1.id, t1.date, t1.msg, t2.msg_count 
FROM table_name t1 
JOIN (SELECT max(msg_id) as mid, count(*) as msg_count FROM table_name GROUP BY id) as t2 
ON(t1.msg_id=t2.mid)

0
投票

好像你想在结果集中找到最新日期,所以查询看起来像是什么,SELECT id,max(date),msg,COUNT(id)AS count FROM table_name GROUP BY id


0
投票

对于sql server,试试这个:

SELECT id, max(date) as date, msg, count(id) AS count 
FROM table_name 
GROUP BY id, msg

0
投票
SELECT T.id, Q.max_date, count(T.msg)
FROM Table1 T, (select id, MAX(dateD) as max_date from Table1 group by id) Q
where Q.id=T.id
group by T.id, Q.max_date
;
© www.soinside.com 2019 - 2024. All rights reserved.