Item | Status |
---|---|
7080 | On Hold |
7080 | Working on It |
7090 | On Hold |
When Item has same number i.e 7080 and has 'working on it' and 'hold' status, it should show 'Working on it' on both the status.
I wrote a solution, using a case expression, but did not work.
Edit: adaptation of @lemon's answer to my data.
SELECT DISTINCT
SUBSTRING(item, 1, 4) Item,
CASE
WHEN MAX(CASE
WHEN [Eng Status] = 'Working on It'
THEN 1
END) OVER (PARTITION BY Item) = 1
THEN 'Working on It'
ELSE [Eng Status]
END AS Status
FROM
tbl
Same result as previous, still not working.
Item | Status |
---|---|
7080 | On Hold |
7080 | Working on It |
7090 | On Hold |
Edit:
select col1,col2 from tbl
This is the data
2条答案
按热度按时间abithluo1#
You can use a
CASE
expression to assign 'Working on It' to all those records that have at least one of that status "MAX(Status='Working') = 1" for each "Item".If you're using
SUBSTRING(item, 1, 4)
instead ofItem
, you also needOVER(PARTITION BY SUBSTRING(item, 1, 4))
.nx7onnlm2#
It is a bad idea to use the same name for an expression as for the original column. You call your expression
item
, but it is actually just a substring of the originalitem
. This confused you and you partitioned yourMAX OVER
byitem
. The DBMS doesn't yet see the alias you created and assumes the column. Here is the corrected query: