如何在按列分组的另一列中选择第一个值

时间:2018-10-18 15:39:46

标签: sql sql-server tsql

当我分组时,如何选择另一列的列顺序的第一个值?

我的数据:

ID       UserId        HashText          Text          Date
-------------------------------------------------------
1        1000          123               Hi            2018-10-10
2        2000          124               Hello         2018-10-11
3        3000          123               Hi            2018-10-12
4        4000          123               Hi            2018-10-13

我的结果:

UserId        HashText      count
-------------------------------------------------------
1000          123           3
2000          124           1

请注意:我不能使用min(UserId)-sort并不总是UserId

4 个答案:

答案 0 :(得分:0)

您可以将row_number()ties子句一起使用:

select top (1) with ties UserId, HashText, cnt
from table t cross apply
     ( select count(*) as cnt
       from table t1 
       where t1.HashText = t.HashText 
     ) t1
order by row_number() over (partition by HashText order by id);

答案 1 :(得分:0)

这将显示输出。

SELECT CASE WHEN COUNT(userid) >= 1 THEN MIN(userid) ELSE NULL END UserId, b.hashtext, b.count FROM Table t
JOIN (SELECT hashtext, COUNT(userid) COUNT FROM Table t GROUP BY hashtext) b 
ON t.hashtext=b.hashtext 
GROUP BY b.count, b.hashtext 

答案 2 :(得分:0)

使用ROW_NUMBER() OVER(PARTITION BY HashText ORDER BY (UserId) ASC)

查询

CREATE TABLE #temp(ID int,UserId int, HashText  int, Text varchar(5), Datevalue Date)
INSERT INTO #temp(ID,UserId,HashText,Text,Datevalue)
select 1, 1000,123,'Hi'    ,       '2018-10-10' union
select 2, 2000,124,'Hello',        '2018-10-11' union
select 3, 3000,123,'Hi' ,          '2018-10-12' union
select 4, 4000,123,'Hi',           '2018-10-13' 

Select * from #temp 

;with CTE as (
Select UserId,HashText,
ROW_NUMBER() OVER(PARTITION BY HashText ORDER BY (UserId) ASC) sort
FRom #temp
),getCount as 
(SELECT HashText, COUNT(userid) [Count] 
FROM #temp t GROUP BY hashtext)
SElect UserId, C.HashText, [Count] 
FROM CTE C
JOIN getCount T ON
T.HashText = C.HashText
WHERE sort = 1

输出:

enter image description here

答案 3 :(得分:0)

不幸的是,SQL Server不支持first_value()作为聚合函数。但是,如果您不喜欢子查询,可以使用一个技巧:

select distinct first_value(userId) over (partition by hashtext order by id) as userId,
       hashtext
       count(*) over (partition by hashtext) as cnt
from mydata;