我有一个Emptbl
我有EmpType
列。
在EmpType中,我有以下数据:
E0123
M0123
E1245
E4578
M1245
E0478
M4789
E4762
现在我想只获得那些具有相同EmpType
的emp数据,例如以下数据:
E0123
M0123
E1245
M1245
并希望将此数据显示为0123
和1245
那么如何获得以上数据?我使用UNION
但它没有获得有效的数据。
由于
答案 0 :(得分:1)
试试这个:
select substring(emptype, 2, len(emptype))
from emptbl
group by substring(emptype, 2, len(emptype))
having count(*) > 1
硬编码2基于您的样本数据。相反,如果您在数字部分之前有任意数量的字母,例如'ABCDEFG0123',您可以使用patindex
来获取子字符串的起始索引,如此;
select substring(emptype, patindex('%[0-9]%',emptype), len(emptype)
答案 1 :(得分:0)
;with CTE as (Select Name,SUBSTRING(Name,2,5) as Number, ROW_NUMBER()
OVER (PARTITION By SUBSTRING(Name,2,5) ORDER BY Name) AS Row
from #Temp)
Select Temp.Name
From CTE C
Cross Apply (Select Name FRom CTE T Where T.Number=C.Number) as Temp
Where C.Row>1
答案 2 :(得分:0)
Select A.*
From EmpTbl A
Inner Join
EmpTbl B
On SubString(A.EmpType, 2, 4) = SubString(B.EmpType, 2, 4) And
SubString(A.EmpType, 1, 1) <> SubString(B.EmpType , 1, 1)
答案 3 :(得分:0)
select top 4 id from
(
select id,rn=row_number()over(partition by right(id,3) order by right(id,3)) from #t
)x
答案 4 :(得分:0)
这是最小的查询:
select substr(a.emptype, 2) num
join emptbl a
join emptbl b on substr(a.emptype, 2) = substr(b.emptype, 2)
and a.emptype != b.emptype