Looking for some suggestions here on a project that I am currently working on.
I have a table that consists of
Vehicle Name
Number of Contracts
Sale Amount
Ratio
I would like to add a Row Number to the table based on certain criteria.
IF there are 5 or more Vehicles where Ratio = 0 then RowNumber() OVER Order By Sale Amount but only apply the row number where Ratio = 0
"everything else" RowNumber() OVER Order By Ratio
I would like the row number to continue not start all over when applying the Row Number to the "everything else". So if the condition is met apply RowNumber where Ratio = 0 using Sale Amount. Sort the rest based on Ratio. Row numbers must be continuous and not start over.if the above condition fails then just use RowNumber() Over Order By Ratio
What I am basically trying to do here is add a second condition for ranking if the first one fails. So if condition 1 fails then rank on second condition.
I would paste a table but just realized how complicated getting a table in here is. Sorry!
Here is an example of something I was trying but did not work. It kept applying the row number using Ratio no matter if the case statement is true.
Select
,ROW_NUMBER()
OVER
(ORDER BY
(CASE WHEN SUM(CASE WHEN RATIO = 0 THEN 1 ELSE 0 END) >= 5
THEN Sale_Amount
ELSE Ratio END) ASC
)as Rows1
FROM Table
Not Sure if RowNumber() is the best in this situation.
Any answers or tips would be helpful! Thanks in advance!
Here is an attempt of showing my data:
Original Data Set:
+---------+----------------+-------------+--------+
| Vehicle | # of Contracts | Sale Amount | Ratio |
+---------+----------------+-------------+--------+
| A | 1 | $800 | 0.00% |
| B | 1 | $800 | 0.00% |
| C | 1 | $978 | 0.00% |
| D | 4 | $2,069 | 0.00% |
| E | 2 | $1,600 | 0.00% |
| F | 6 | $4,210 | 0.00% |
| G | 3 | $1,289 | 0.00% |
| H | 5 | $1,590 | 0.00% |
| I | 10 | $8,456 | 13.45% |
| J | 12 | $9,000 | 10.56% |
| K | 6 | $2,458 | 20.00% |
| L | 9 | $3,607 | 34.50% |
| M | 10 | $7,987 | 70.80% |
| N | 2 | $954 | 96.89% |
| O | 2 | $800 | 34.67% |
| P | 3 | $800 | 23.09% |
| Q | 5 | $901 | 12.78% |
| R | 7 | $1,008 | 45.78% |
| T | 9 | $765 | 20.00% |
| U | 2 | $345 | 1.00% |
| V | 4 | $834 | 45.00% |
+---------+----------------+-------------+--------+
Expected Result:
+-----------+---------+----------------+-------------+--------+
| RowNumber | Vehicle | # of Contracts | Sale Amount | Ratio |
+-----------+---------+----------------+-------------+--------+
| 1 | F | 6 | $4,210 | 0.00% |
| 2 | H | 5 | $1,590 | 0.00% |
| 3 | D | 4 | $2,069 | 0.00% |
| 4 | G | 3 | $1,289 | 0.00% |
| 5 | E | 2 | $1,600 | 0.00% |
| 6 | A | 1 | $800 | 0.00% |
| 7 | B | 1 | $800 | 0.00% |
| 8 | C | 1 | $978 | 0.00% |
| 9 | U | 2 | $345 | 1.00% |
| 10 | J | 12 | $9,000 | 10.56% |
| 11 | Q | 5 | $901 | 12.78% |
| 12 | I | 10 | $8,456 | 13.45% |
| 13 | K | 6 | $2,458 | 20.00% |
| 14 | T | 9 | $765 | 20.00% |
| 15 | P | 3 | $800 | 23.09% |
| 16 | L | 9 | $3,607 | 34.50% |
| 17 | O | 2 | $800 | 34.67% |
| 18 | V | 4 | $834 | 45.00% |
| 19 | R | 7 | $1,008 | 45.78% |
| 20 | M | 10 | $7,987 | 70.80% |
| 21 | N | 2 | $954 | 96.89% |
+-----------+---------+----------------+-------------+--------+