I am trying to use rank for getting a rank for each alphabet and select that rank in having function using parameters
Ex: Here is my table data:
Alphabet Date
A 2019-12-19 12:31:43.633
A 2019-12-19 12:31:43.650
B 2019-11-07 11:37:08.560
select *
from (select alphabet, date,
dense_Rank() over (partition by alphabet order by date) RankOrder
from mytable
) A
group by alphabet, date, RankOrder
having RankOrder = 1
By using the above query here is my Result:
Alphabet Date Rank Order
A 2019-12-19 12:31:43.633 1
B 2019-11-07 11:37:08.560 1
What if I had to do this for multiple alphabets using parameters? using declare @palphabet int='A',@pyear nvarchar(20)=2019 How can I add the parameters to the above query?
You can add
whereclause :I don't think
GROUP BYwithHAVINGclause is required here. As you have a raw data not the aggregate data & you are returning only 1 row for eachalphabetby usingwhere RankOrder = 1.Note : You can't use
INTAlphabetas base table contains text value. So, change the type of variable@palphabet.