I am trying to write a SQL query for this sample input and output: with the following condition: The hierarchy will be the same as below
Can someone help me on this?
I could try only the following code for 1st condition, but this is removing all the records and giving incorrect result:
select *,
from table t1
where code='PK' and length('filter')=0
SELECT T1.KEY,
T1.CODE,
CASE
WHEN CODE = 'PK' AND COALESCE(FILTER,0) = 0 THEN 'XXXX'
ELSE T1.FILTER
END AS FILTER
FROM TABLE T1
WHERE CODE != 'PK' OR (CODE='PK' AND COALESCE(FILTER,0) = 0)
In your query length('filter')
returns 6
select key, code, IF(code = 'PK', 'XXXX', filter) as filter
from table_t1
where (
code != 'PK'
OR
code = 'PK' AND (filter IS NULL OR filter = '')
)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.