简体   繁体   English

T-SQL查找唯一组合的首次出现

[英]T-SQL Find first occurrence of unique combinations

I have a table that looks like this 我有一张看起来像这样的桌子

Log_ID  User_ID  Line   Attribute
1       A        1      **** 
1       B        2      ****
1       B        3      ****
2       C        1      ****
2       C        2      ****
2       A        3      ****
2       B        4      ****

For each Log_ID, there are multiple values in User_ID and Line. 对于每个Log_ID,User_ID和Line中都有多个值。 (Log_ID, Line) will always be unique, but (Log_ID, User_ID) will not. (Log_ID,Line)始终是唯一的,但(Log_ID,User_ID)不会唯一。

I'm trying to return the unique (Log_ID, User_ID) pairs where the lowest Line value is the tiebreaker. 我试图返回唯一的(Log_ID,User_ID)对,其中最低的Line值是平局。 The result set would look like this: 结果集如下所示:

Log_ID  User_ID  Line   Attribute
1       A        1      ****
1       B        2      ****
2       C        1      **** 
2       A        3      ****
2       B        4      ****

Nothing I've tried has worked. 我尝试过的一切都没有奏效。 I keep either getting unique (Log_ID, User_ID, Line) triplets or only getting rows where Line=1. 我一直在获取唯一的(Log_ID,User_ID,Line)三元组,或者仅获取Line = 1的行。

I need additional attributes from the table besides Log_ID, User_ID, and Line, so I can't just use SELECT DISTINCT 除了Log_ID,User_ID和Line,我还需要表中的其他属性,所以我不能只使用SELECT DISTINCT

Any ideas? 有任何想法吗? The solutions I've found generally assume that I'm trying to join to the table and that I want to join on the lowest match. 我找到的解决方案通常假定我正在尝试加入该表,并且希望加入最低匹配项。 But this is my primary table. 但这是我的主要表格。

Thanks! 谢谢!

This type of prioritization can make good use of row_number() . 这种优先级排序可以很好地利用row_number()

select t.*
from (select t.*,
             row_number() over (partition by log_id, user_id
                                order by line) as seqnum
      from t
     ) t
where seqnum = 1;

EDIT: 编辑:

You can also do this by joining on the lowest match or using a correlated subquery. 也可以通过加入最低匹配项或使用相关子查询来实现。 For example: 例如:

select t.*
from t
where t.line = (select min(t2.line)
                from t t2
                where t2.log_id = t.log_id and t2.user_id = t.user_id
               );

row_number() is usually faster. row_number()通常更快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM