I'm trying to give a certain number of point to my row depending on some condition, I can't figure out how to write it.
For a given Id, FormatCode and Price I want to give the value 1. For subsequent rows, for the same Id and Price, if the FormatCode is a multiple of a previous row with the same Id and price, I want to give the same value.
For instance :
00010405, 100, 0.3218 = 1
00010405, 400, 0.3218 = 1
(400 % 100 = 0)
00010405, 500, 0.3126 = 2
(500 % 100 = 0, but the price is different)
00010405, 1000, 0.3126 = 2
(1000 % 500 and 1000 % 100 = 0, but the price of the format code 100 is different, hence it will take the value 2 because it has the same price)
Id | Format Code | Price | Value | Row |
---|---|---|---|---|
00010405 | 100 | 0.3218 | 1 | 1 |
00010405 | 400 | 0.3218 | 1 | 2 |
00010405 | 500 | 0.3126 | 2 | 3 |
00010405 | 1000 | 0.3126 | 2 | 4 |
SELECT
Id,
FormatCode,
Price,
Value,
ROW_NUMER() OVER (
PARTITION BY Id
ORDER BY FormatCode
)
FROM Table
1000 % 500
or1000 % 100
? Why? There's really not enough information for us to do more than guess what your logic is beyond these 3 vague examples. - LarnuFormatCode
has a value of500
the previous row has a value of400
yet your example expression states100
is the comparison: "(500 % 100 = 0, but the price is different)". Does "A previous row", mean any of them? If so, why not state "(500 % 400 = 100 however, 500 % 100 = 0, but the price is different)" so that that condition is clear? - Larnu