简体   繁体   中英

SQL Query on displaying a result by eliminating on a condition

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

  1. Remove the records where code = PK and filter is not blank
  2. Update the records having code = PK and filter as blank to 'XXXX'

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM