通过特定列从表中查找不同值的方法

时间:2018-06-03 07:19:53

标签: sql sql-server database

这是一个由以下数据组成的表格:

SELECT family_head_id, ref_name+' '+ref_value reference
  FROM TBL_FACILITY INNER JOIN TBL_REFERENCE 
    ON ( ref_number = SUBSTRING(facility_used,CHARINDEX(',', facility_used)-1,1) )
UNION ALL
SELECT family_head_id, ref_name+' '+ref_value reference
  FROM TBL_FACILITY INNER JOIN TBL_REFERENCE 
    ON ( ref_number = SUBSTRING(facility_used,CHARINDEX(',', facility_used)+1,1) )
 ORDER BY 1, 2;

我希望所有StandardName和UserName后跟Rtype ='Entity',如果Rtype ='Position',它只会获得那些与RType'Entity'无关的StandardName

为此,我做了这样的查询

StandardName|Username    |RType
------------|------------|--------
Department  |Department  |  Position
Division    |Division    |  Entity
Division    |Division    |  Position
Plant       |Plant       |  Entity
Section     |Section     |  Position
SubDivision |Sub-Division|  Entity
SubDivision |Subdivision |  Position
SubSection  |Subsection  |  Position
Unit        |Unit        |  Entity

然后输出

SELECT DISTINCT u.StandardName, u.UserName ,ISNULL(e.RType,'position') AS RType
from (
     SELECT  DISTINCT StandardName,UserName
     from Table1 AS ee where   RType = 'Entity'  
     UNION
     SELECT  DISTINCT StandardName, UserName
     from Table1 AS pp where  RType = 'position' 
) u
LEFT OUTER JOIN (
     select UserName,StandardName,RType 
     from Table1 WHERE RType='Entity' 
) e on e.StandardName = u.StandardName
 ORDER BY Rtype

此处在'SubDivision'的情况下,它出现两次,而它需要与'Entity'RType一起出现一次。

预期输出应为 -

StandardName|Username    |RType
------------|------------|--------
Division    |Division    |  Entity
Plant       |Plant       |  Entity      
SubDivision |Sub-Division|  Entity
SubDivision |Subdivision |  Entity       
Unit        |Unit        |  Entity
Department  |Department  |  Position
Section     |Section     |  Position
SubSection  |Subsection  |  Position

SQLFIDDLE Link

4 个答案:

答案 0 :(得分:2)

这是根据您的要求翻译的SQL语句:

 -- gimme all things that are entity
 select StandardName, UserName, RType 
   from Table1
   where RType = 'Entity'  
 union   
 -- and all things that have no entity-entry
   select StandardName, UserName, RType from Table1 as k
   where rtype = 'Position' and not exists (
       select 1 
       from table1 as t 
       where t.standardname = k.standardname 
         and t.rtype = 'Entity'  
   )

没有你的“distincs”,但你得到的结果是:

StandardName    UserName        RType
----------------------------------------
Division        Division        Entity
Plant           Plant           Entity
SubDivision     Sub-Division    Entity
Unit            Unit            Entity
Department      Department      Position
Section         Section         Position
SubSection      Subsection      Position

您没有任何重复的'Entity' - 条目 - 因此没有明确要求。

答案 1 :(得分:2)

这是优先级查询。帕特里克的方法是一种很好的方法,特别是当只有两种类型时。

另一种方法使用row_number()

select e.StandardName, e.Username, e.RType
from (select e.*,
             row_number() over (partition by StandardName
                                order by (case RType when 'Entity' then 1 when 'Position' then 2 else 3 end)
                               ) as seqnum
      from employees e
     ) e
where seqnum = 1;

答案 2 :(得分:1)

对于每个StandardName,您希望行包含“Entity”,但如果不存在,则为“Position”的行。这是ROW_NUMBER的一个简单任务:

with cte as 
 ( 
   select
      StandardName
     ,Username
     ,RType
     ,row_number() 
      over (partition by StandardName  -- for each StandardName
            order by RType) as rn      -- sort to get Entity first
   from Table1
 )
select
   StandardName
  ,Username
  ,RType
from cte
where rn = 1
order by
   RType
  ,Username
;

请参阅fiddle

答案 3 :(得分:1)

试试这个:

select * from Employees 
where rtype = 'Entity' 
or
standardname in (
select standardname from Employees 
where rtype = 'Position' and 
standardname not in 
(select standardname from employees where rtype = 'Entity'))
order by rtype,standardname