当有重复的ID时,您如何寻找唯一值?

时间:2018-08-27 00:33:40

标签: sql sql-server left-join primary-key id

我有一个表,该表具有重复的ID。我想要一个具有所有三个值(TWIX,HERSHYS和M&MS)的ID,这些值可以在不同的行上,但是我不知道如何使用逐行而不是按组的语句来查找ID。

我已经尝试过(在Microsoft SQL Server中)

select *, 
case when 'Hershys' in (Col1, Ccol2) then 1 else 0 as case1,
case when 'TWIX' in (Col1, Col2) then 1 else 0 as case2,
case when 'M&M' in (Col1, Col2) then 1 else 0 as case3
into #candyflags_t1
from table 


   select * from #candyflags_t1
   where case1 > 0 and case2> 0 and case3 > 0

由于sql逐行执行,因此无法使用。我应该如何创建标志或查询此标志,以便找到具有所有三个值的id组?

 `+--------------------+----------+
    |  ID     |    Col1    | Col2    |
    +---------------------+----------+
    | L123    |   TWIX     |         |       
     +--------------------+----------+
    | L123    |  Hershys   |         |
    +--------------------+-----------+           
    | L123    |            |   m&ms  |
    +--------------------+----------+
    | F143    |            |   m&ms  |
    +--------------------+----------+
    | F143    |   Snickers | gummies |
    +--------------------+----------+

在此表中,我想找到L123而不是F143。感谢您的帮助,谢谢!

1 个答案:

答案 0 :(得分:2)

您可以使用SegNeighlibrary(changepoint) p.cpt <- changepoint::cpt.mean(p$Rate,method="method") cpts(p.cpt) 。如果您想要具有所有三个品牌的ID:

group by

having是取消数据透视的一种便捷方法,因此您可以处理一列而不是两列。