Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基于多个筛选器从SQL Server中选择重复行_Sql_Sql Server - Fatal编程技术网

基于多个筛选器从SQL Server中选择重复行

基于多个筛选器从SQL Server中选择重复行,sql,sql-server,Sql,Sql Server,我有这些数据 ID Name State Check 1 Raj UP ok 1 Raj UP Notok 2 bob MP ok 2 bob MP ok 3 joy MH Notok 3 joy MH Notok 我想要以下数据 ID Name State Check flag 1 Raj UP ok y 1 Raj UP Notok n 2 bob MP o

我有这些数据

ID Name State Check
1  Raj  UP     ok
1  Raj  UP     Notok
2  bob  MP     ok
2  bob  MP     ok
3  joy  MH     Notok
3  joy  MH     Notok  
我想要以下数据

ID Name State Check  flag
1  Raj  UP     ok      y  
1  Raj  UP     Notok   n 
2  bob  MP     ok      y
2  bob  MP     ok      n
3  joy  MH     Notok   y
3  joy  MH     Notok   n 

我想确定选择了哪个。

您应该指定规则,但看起来任何
ok
都会使名称
ok

create table #Temp (ID int, Name Varchar(8000), State char(2), [Check] varchar(8000))
Insert #Temp Values (1,  'Raj',  'UP',     'ok')
Insert #Temp Values (1  ,'Raj',  'UP',     'Notok')
Insert #Temp Values (2  ,'bob',  'MP',     'ok')
Insert #Temp Values (2  ,'bob',  'MP',     'ok')
Insert #Temp Values (3  ,'joy',  'MH',     'Notok')
Insert #Temp Values (3  ,'joy',  'MH',     'Notok')


;With cteRowsAssigned AS
(
Select  Row_Number() Over (Partition By Name Order By [Check] Desc) RowNum,
        *
    From #Temp
)
Select ID, Name, State, [Check] From cteRowsAssigned Where RowNum = 1 Order By ID
select id, name, state,
       (case when sum(case when [check] = 'ok' then 1 else 0 end) > 0
             then 'ok'
             else 'Notok'
        end) as [check]
from t
group by id, name, state;
注意:
check
是SQL中的一个关键字,因此它对于列来说是一个坏名称。在您的情况下,您还可以执行以下操作:

select id, name, state, min(lower([check])) as [check]
from t
group by id, name, state;

此版本使用逻辑值的字母顺序。

您应该指定规则,但看起来任何
ok
都会使名称
ok

select id, name, state,
       (case when sum(case when [check] = 'ok' then 1 else 0 end) > 0
             then 'ok'
             else 'Notok'
        end) as [check]
from t
group by id, name, state;
注意:
check
是SQL中的一个关键字,因此它对于列来说是一个坏名称。在您的情况下,您还可以执行以下操作:

select id, name, state, min(lower([check])) as [check]
from t
group by id, name, state;

此版本使用逻辑值的字母顺序。

如果您的意思是
ok
优先于
Notok

select id, name, state, case when max(
       case when [check] = 'ok' then 1 else 0 end) = 1
       then 'ok'
       else 'Notok'
    end as [check]
from table
group by id, name, state;

如果您的意思是
ok
优先于
Notok

select id, name, state, case when max(
       case when [check] = 'ok' then 1 else 0 end) = 1
       then 'ok'
       else 'Notok'
    end as [check]
from table
group by id, name, state;

最简单的形式是:

SELECT id,name,state FROM t where <whatever filter you want to set> GROUP BY id,name,state
从t中选择id、name、state,其中按id、name、state分组

最简单的形式是:

SELECT id,name,state FROM t where <whatever filter you want to set> GROUP BY id,name,state
从t中选择id、name、state,其中按id、name、state分组

但检查值可以是任何形式order@Rahul . . . 我不理解你的评论。我会使用
max(lower([check]))
,以避免大小写问题。我已更新问题可能有助于udenstand my proble我已更新问题可能有助于udenstand my proble,但检查值可以是任意大小写order@Rahul . . . 我不理解你的评论。我会做
max(lower([检查])
,以避免大小写问题。我已更新问题可能有助于udenstand我的问题我已更新问题可能有助于udenstand我的问题我已更新问题可能有助于udenstand我的先证者“选中”表示…上次编辑后的问题完全不同…对已经回答的人有点不尊重。“选中”表示…上次编辑后的问题完全不同…对已经回答的人有点不尊重。