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)
Get data from table using group by
-
23-07-2023 - |
سؤال
I have a Emptbl
in which I have EmpType
Column.
In EmpType I have following data for example :
E0123
M0123
E1245
E4578
M1245
E0478
M4789
E4762
Now I want to get only those emp data which have same EmpType
for example below data:
E0123
M0123
E1245
M1245
And want to show this data as group by as 0123
and 1245
So how to get above data? I use UNION
but it does not get valida data.
Thanks
المحلول 2
نصائح أخرى
Try this:
select substring(emptype, 2, len(emptype))
from emptbl
group by substring(emptype, 2, len(emptype))
having count(*) > 1
The hard-coded 2 is based on your sample data. If instead you had an arbitrary number of letters before the numeric part, e.g. 'ABCDEFG0123', you could use patindex
to get the starting index for your substring like so;
select substring(emptype, patindex('%[0-9]%',emptype), len(emptype)
;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
Here is the fiddle sample
select top 4 id from
(
select id,rn=row_number()over(partition by right(id,3) order by right(id,3)) from #t
)x
This is the smallest query that works:
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
لا تنتمي إلى StackOverflow