Latest Type for the every ID
I have a table like below.
create table ##Test (id nvarchar(20), dat datetime, idType nvarchar(10))
insert into ##test values (‘1001′,’2024-05-01’, ‘A’)
insert into ##test values (‘1001′,’2024-05-02’, ‘B’)
insert into ##test values (‘1001′,’2024-05-03’, ‘C’)
insert into ##test values (‘1001′,’2024-05-04’, ‘C’)
insert into ##test values (‘1002′,’2024-05-01’, ‘A’)
insert into ##test values (‘1003′,’2024-05-01’, ‘B’)
insert into ##test values (‘1003′,’2024-05-02’, ‘C’)
insert into ##test values (‘1003′,’2024-05-03’, ‘A’)
select * from ##Test
drop table ##test
i need latest distinct last two idType against every ID
I need the result like below
create table ##result (id nvarchar(20), result nvarchar(10))
insert into ##result values (‘1001′,’C’)
insert into ##result values (‘1001′,’B’)
insert into ##result values (‘1002′,’A’)
insert into ##result values (‘1003′,’A’)
insert into ##result values (‘1003′,’C’)
select * from ##result
drop table ##result
I have a table like below. create table ##Test (id nvarchar(20), dat datetime, idType nvarchar(10))insert into ##test values (‘1001′,’2024-05-01’, ‘A’)insert into ##test values (‘1001′,’2024-05-02’, ‘B’)insert into ##test values (‘1001′,’2024-05-03’, ‘C’)insert into ##test values (‘1001′,’2024-05-04’, ‘C’)insert into ##test values (‘1002′,’2024-05-01’, ‘A’)insert into ##test values (‘1003′,’2024-05-01’, ‘B’)insert into ##test values (‘1003′,’2024-05-02’, ‘C’)insert into ##test values (‘1003′,’2024-05-03’, ‘A’)select * from ##Testdrop table ##test i need latest distinct last two idType against every ID I need the result like below create table ##result (id nvarchar(20), result nvarchar(10))insert into ##result values (‘1001′,’C’)insert into ##result values (‘1001′,’B’)insert into ##result values (‘1002′,’A’)insert into ##result values (‘1003′,’A’)insert into ##result values (‘1003′,’C’)select * from ##resultdrop table ##result Read More