Sql 通过特定列从表中查找不同值的方法
下面是一个由如下数据组成的表格:Sql 通过特定列从表中查找不同值的方法,sql,sql-server,database,Sql,Sql Server,Database,下面是一个由如下数据组成的表格: StandardName|Username |RType ------------|------------|-------- Department |Department | Position Division |Division | Entity Division |Division | Position Plant |Plant | Entity Section |Section
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
我希望所有的StandardName和UserName后跟Rtype='Entity',如果Rtype='Position',它将只获得与Rtype'Entity'不关联的StandardName
为此,我做了这样的查询
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
然后输出为
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
在这里,如果是“细分”,它会显示两次,而需要使用“实体”RType显示一次
预期输出应为--
SQLFIDLE这是根据您的需求翻译的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
您没有任何重复的
“实体”
-条目-因此不需要任何不同的条目 这是根据您的需求翻译的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
您没有任何重复的
“实体”
-条目-因此不需要任何不同的条目 对于每个StandardName,您希望使用“实体”行,但如果不存在,则使用“位置”行。这是一个简单的行号任务:
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
;
请参见,了解您希望使用“实体”行的每个标准名称,但如果不存在,则使用“位置”行。这是一个简单的行号任务:
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
;
请参见试一试:
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
试试这个:
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
这是一个优先级查询。帕特里克的方法是一种很好的方法,特别是当只有两种类型时 另一种方法使用
行编号()
:
这是一个优先级查询。帕特里克的方法是一种很好的方法,特别是当只有两种类型时 另一种方法使用
行编号()
:
用户名
Sub-Division
和Subdivision
应该相等,不是吗?用户名Sub-Division
和Subdivision
应该相等,不是吗。。?