Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

How to write a codition based on min rank

$
0
0

Hello All,

could you please let me know how to write a codition based on min rank,

Create table TableA1 (PatID int, blockName varchar(20), RankID int)


insert into TableA1 values (123, 'Cancer', 5)
insert into TableA1 values (235, 'Hearts', 6)
insert into TableA1 values (345, 'Child' ,1)
insert into TableA1 values (123, 'OutPatient', 3)
insert into TableA1 values (567, 'OutPatient', 4)
insert into TableA1 values (789, 'Inbound' , 7)
insert into TableA1 values (567, 'OutPatient', 3)
insert into TableA1 values (678, 'Cancer', 5)
insert into TableA1 values (789, 'Hearts', 6)
insert into TableA1 values (789, 'KidneySpl', 9)
insert into TableA1 values (345, 'OutPatient', 3)

select * from TableA1 order by 1


Create table TableB1 (PatID int, ModelId int )

insert into TableB1 values (123,114346)
insert into TableB1 values (235,226554)
insert into TableB1 values (345,336544)
insert into TableB1 values (567,446789)
insert into TableB1 values (678,558987)
insert into TableB1 values (789,667998)


select * from TableB1 order by 1

--Joining
select a.*, b.ModelID from TableA1 A inner join TableB1 B
on a.PatID = b.PatID
order by 1,3

Required outcome is based on the RankID . So if for the same PatID that appears more times in TableA1, the ModelID (from TableB) goes to the record with highest rankID.

expected Results will be

PatID blockName RankID ModelID
123 OutPatient 3 114346
123 Cancer 5
235 Hearts 6 226554
345 Child 1 336544
345 OutPatient 3
567 OutPatient 3 446789
567 OutPatient 4
678 Cancer 5 558987
789 Hearts 6 667998
789 Inbound 7
789 KidneySpl 9

Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>