2

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.

  1. 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% |  
+-----------+---------+----------------+-------------+--------+  
Felix Pamittan
  • 31,544
  • 7
  • 41
  • 67
user2533550
  • 23
  • 1
  • 4
  • 1
    Please post sample data and expected result. You can create one in [sql fiddle](http://sqlfiddle.com/#!6) – Felix Pamittan May 11 '15 at 02:58
  • Thanks! I went to the site but it didnt load. I was trying to use senseful solutions but when I pasted the table in it came out very weird. – user2533550 May 11 '15 at 03:03
  • Paste it anyway, let us edit them. – Felix Pamittan May 11 '15 at 03:05
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders May 11 '15 at 03:08
  • @johnsaunders - thank you for the clarification. This is my first question and will be sure to not make that mistake in the future – user2533550 May 11 '15 at 03:12
  • Your result does not order by `Sale Amount`, instead it's ordering by `# of Contracts DESC', for the first criteria. – Felix Pamittan May 11 '15 at 03:33

2 Answers2

1

You can use SUM OVER() and ROW_NUMBER()

Based on your result, I order the first criteria by # of Contracts DESC.

SQL Fiddle

WITH Cte AS(
    SELECT *,
        ZeroRatio = SUM(CASE WHEN Ratio = 0 THEN 1 ELSE 0 END) OVER()
    FROM TestData
),
CteRN AS(
    SELECT *,
        RN = ROW_NUMBER() OVER(ORDER BY 
            CASE WHEN ZeroRatio >= 5 AND Ratio = 0 THEN NumContracts END DESC,
            Ratio
        )
    FROM Cte
)
SELECT
    RN, Vehicle, NumContracts, SaleAmount, Ratio
FROM CteRN
ORDER BY RN
Felix Pamittan
  • 31,544
  • 7
  • 41
  • 67
  • Worked like a charm!!! I did not use the Sum OVER() - for some reason my query was running so slow when I added this. It looks like I was close but in the case statement i was missing "Ratio = 0". You saved me a bunch of time! Thanks again! – user2533550 May 11 '15 at 12:57
0

I don't think your example output follows what you said you're after, so may need to adjust the ORDER BY fields, but this is what I think you're after:

;with cte AS (SELECT *,COUNT(*) OVER(PARTITION BY Ratio) AS Ratio_CT 
              FROM Table1
              )
SELECT *,ROW_NUMBER() OVER(ORDER BY CASE WHEN Ratio_CT >= 5 AND Ratio = 0 THEN [Sale Amount] END DESC,Ratio) AS Rows1
FROM cte

Demo: SQL Fiddle

Hart CO
  • 34,064
  • 6
  • 48
  • 63