Get DENSE RANK in a grouped data
Moderators: chulett, rschirm, roy
Did not make much sense to me, have not done anything like that before.DSguru2B wrote:Well you will have to do a column by column check then. Compare column 1, if they are same, check scond column, if same check third, if same, check the 5th column.
Now if the 5th column is same, refresh the rank to 1, if its different, increment the rank.
If any of the first three columns are different, start from 1 again.
Makes sense ???
Can you please provide more details..
![Embarassed :oops:](./images/smilies/icon_redface.gif)
Three stage variables, PrevThree, PrevSales and cond. Their derivations will be
The stage variables should be in the same order. The stage variable cond will have your Rank.
Code: Select all
cond | if @INROWNUM =1 then 1 else if (in.STATE:in.CITY:in.ZIP=
PrevThree) AND (in.SALES <> PrevSales) then cond +1 else 1
PrevThree | in.STATE:in.CITY:in.ZIP
PrevSales | in.SALES
Creativity is allowing yourself to make mistakes. Art is knowing which ones to keep.
You can do it in Oracle
Assuming your table name is Car.
Simple!
Assuming your table name is Car.
Code: Select all
SELECT state, city, zip, sales_number,
dense_rank() OVER (PARTITION BY state, city, zip ORDER BY sales_number DESC) rank
FROM Car
ORDER BY state, city, zip, sales_number DESC
Narasimha Kade
Finding answers is simple, all you need to do is come up with the correct questions.
Finding answers is simple, all you need to do is come up with the correct questions.
DSGuru2B can you show it using RowProcCompareWithPreviousValue
I am doing the following but not getting :
I am doing the following but not getting :
Code: Select all
1)current=read.ST:read.CITY:read.ZIP
2)sv1=RowProcCompareWithPreviousValue(current)
3)sv2=if sv1 then RowProcCompareWithPreviousValue(read.SALES) else Rank=1
4)Rank=if sv2 then sv2 else Rank+1
If women didn't exist, all the money in the world would have no meaning.
-- Aristotle Onassis
-- Aristotle Onassis
-
- Participant
- Posts: 54607
- Joined: Wed Oct 23, 2002 10:52 pm
- Location: Sydney, Australia
- Contact:
The query works great! Thanks narasimha.narasimha wrote:You can do it in Oracle
Assuming your table name is Car.
Simple!Code: Select all
SELECT state, city, zip, sales_number, dense_rank() OVER (PARTITION BY state, city, zip ORDER BY sales_number DESC) rank FROM Car ORDER BY state, city, zip, sales_number DESC
I have one way to do it now.
DSGuru2b, I still having problem getting your code to work. I am getting all Ranks as 1
Did you do exactly what I asked you to do? Sorting, order of stage variables etc. As I said, thats tested code. If it works for me, it will work for you. Go through my post again and do exactly as I said. Can't help you more then that.
Creativity is allowing yourself to make mistakes. Art is knowing which ones to keep.
DSguru2B,
I rechecked it, it works fine for everything except when the sales_number is the same within a group of state, city and metro.
In such conditions we get a 1 instead of the getting the same rank again, because of the condition
(in.SALES <> PrevSales) then cond +1 else 1.
Therefore if in.SALES = PrevSales you will get a 1
Appreciate your help
I rechecked it, it works fine for everything except when the sales_number is the same within a group of state, city and metro.
In such conditions we get a 1 instead of the getting the same rank again, because of the condition
(in.SALES <> PrevSales) then cond +1 else 1.
Therefore if in.SALES = PrevSales you will get a 1
![Sad :(](./images/smilies/icon_sad.gif)
Appreciate your help
Ok, I thought it was to be reset to 1. My bad. Use the following
Code: Select all
cond | if @INROWNUM =1 then 1 else if (in.STATE:in.CITY:in.ZIP=
PrevThree) AND (in.SALES <> PrevSales) then cond +1 else if
(in.STATE:in.CITY:in.ZIP= PrevThree) AND (in.SALES =
PrevSales) then cond else 1
PrevThree | in.STATE:in.CITY:in.ZIP
PrevSales | in.SALES
Creativity is allowing yourself to make mistakes. Art is knowing which ones to keep.