I am trying to query the correct price by going through and choosing between 2 prices.
These are in order of importance from highest to lowest:
- if
[tran_type]is equal toQuotthan choose this price - if
[default_level]is greater than 0, going to be a number from 1-8
The [default_level] is a place holder with a numerical value which is used to signify a pricing level from 1 to 8.
The price when [default_level] is greater than 0 is stored as the blank [start_cust_or_group] in the same table and [level_] is the value it is tied with for the 1-8 pricing level.
So say if [default_level] is 6 then the correct price I need pulled is for [start_cust_or_group] to equal blank [level_] would be 6 and the latest date available [start_date]
| item_or_pricecode | uom_code | tran_type | default_level | level_ | price |
|---|---|---|---|---|---|
| 27MIETE11K | EA | 6 | 0 | 0.00 | |
| 22RGRD45BS | CT | Quot | 0 | 144.10 |
Below is what I would like the final outcome to be.
For the first item (27MIETE11K) it sees tran_type was blank then it moved on and found default_level was 6 so it chooses Level_ where its 6 and start_cust_or_group is blank and it takes that price. For the next item (22RGRD45BS) it sees tran_type is Quot so it populates the price in the LevelResult. This part of the query with Quot works.
| item_or_pricecode | uom_code | LevelResult |
|---|---|---|
| 27MIETE11K | EA | 29.57 |
| 22RGRD45BS | CT | 144.10 |
SELECT
[item_or_pricecode], [uom_code],
CASE
WHEN [tran_type] <> '' THEN [price]
WHEN [default_level] > 0 THEN XXXXXX
ELSE 0
END AS LevelResult
FROM
[test].[tablepricing].[pricing]
WHERE
[start_cust_or_group] = '7000192'
AND [cust_shipto_num] = '2'
AND [RowDeleted] = 0
ORDER BY
[start_date] DESC
That code works if I put [default_level] but that just returns the value for default_level in the LevelResult column.
I need to have a select query where I put XXXXXX. I don't know how to pass the [default_level] to the select query to match with the [level_] or if it's even possible.
SELECT
x.[item_or_pricecode], x.[uom_code], x.[level_], x.[price],x.[start_date]
FROM
(SELECT
x.[item_or_pricecode], x.[uom_code], x.[level_], x.[price],x.[start_date],
ROW_NUMBER() OVER (PARTITION BY x.[item_or_pricecode] ORDER BY x.[start_date] DESC, x.[level_] ASC) AS rn
FROM
[test].[tablepricing].[pricing] AS x
WHERE
[start_cust_or_group] = ''
AND [RowDeleted] = 0
AND [item_or_pricecode] = '27MIETE11K') AS x
WHERE
x.rn <= 8
| item_or_pricecode | uom_code | level_ | price | start_date |
|---|---|---|---|---|
| 27MIETE11K | EA | 1 | 38.65 | 2024-01-15 |
| 27MIETE11K | EA | 2 | 34.81 | 2024-01-15 |
| 27MIETE11K | EA | 3 | 33.05 | 2024-01-15 |
| 27MIETE11K | EA | 4 | 31.43 | 2024-01-15 |
| 27MIETE11K | EA | 5 | 30.49 | 2024-01-15 |
| 27MIETE11K | EA | 6 | 29.57 | 2024-01-15 |
| 27MIETE11K | EA | 7 | 28.97 | 2024-01-15 |
| 27MIETE11K | EA | 8 | 28.39 | 2024-01-15 |
This is a separate query to show the level prices and how I got it to find the latest date for each entry.
Normally if I just did
WHERE item_or_pricecode = '27MIETE11K'
AND start_cust_or_group = ''
on the pricing table I would get 78 entries with older prices mixed in.
x) in the inner and outer scope. That makes for really difficult and confusing reading of your code, and can lead to very unexpected and hard to diagnose problems at times. Use something useful, meaningful, and use a different alias for each level in your queryORDER BY x.[start_date] DESC- what is this column? pls, add to data source example. Also about[start_cust_or_group] = ''pricingor more? Please show table (tables), data example and desired output.