Search code examples
sqldatabaseprocedurehana

Deleting values on Hana Table based on a field value


I am working on a hana table and i am trying to delete a table if it contains value from a list.

A  B   
22 01 
22 01
22 02
22 06
23 01
23 01
23 06

I will like to drop some values from this table and have this.

A  B   
22 01 
22 01
22 06
23 01
23 01
23 06

Basically i will like to most likely do a count and check if column B consists of 01 AND 02, if it does drop 02 and if it consists of only 01 leave as it is.

This seems virtually impossible with almost every sql script i have tried

SELECT BP, COUNT(*) AS SO FROM "EH"."BP_CUST" GROUP BY BP;

This scripts gets the count of each row and put it in SO column.

after that maybe do an if statement on the SO column and delete if the B field contains 01 and 02?

I tried doing and IF statement then select and i could not get it to work either.

A  B
22 01 
22 01 
22 02 
22 06 
23 01 
23 01 
23 06 
24 02

Becomes 

A  B
22 01 
22 01 
22 06 
23 01 
23 01 
23 06 
24 02

Solution

  • If I understand correctly, you want:

    select c.*
    from "EH"."BP_CUST" c
    where c.b <> '02' or
          not exists (select 1
                      from "EH"."BP_CUST" c2 
                      where c2.a = c.a and c2.b = '01'
                     );
    

    Your question says "delete". But I think the intention is to select "02" rows only when there is no "01" row for the same a (and all other rows).