0

hi I have a table as below temp_layer_table:-

id    count    name
1      0       v
2      3       r
1      5       t
4      0       f
4      6       g
2      0       r
3      6       r
3      0       g
5      0       t

now I need to write a condition where if id is 1 or 4 or 3 and count = 0 for all 3 ids then i should eliminate those records

id    count    name
2      3       r
1      5       t
4      6       g
2      0       r
3      6       r
5      0       t

I have tried below hive query but does't work

code 1:-

insert into table final_layer_table 
select n.*
  from temp_layer_table n 
 where id = '1' 
   or  id = '4' 
   or  id = '3' 
  and count != '0' ;

code2:-

 insert into table final_layer_table 
 select n.*
   from temp_layer_table n 
  where id in ('1','4','3') 
    and count != '0' ;
Strawberry
  • 33,750
  • 13
  • 40
  • 57
Rahul Varma
  • 550
  • 5
  • 23
  • `AND` has higher precedence than `OR`. I.e. `where id = '1' or id = '4' or id = '3' and count != '0'` means `where id = '1' or id = '4' or (id = '3' and count != '0')`... https://stackoverflow.com/questions/1241142/sql-logic-operator-precedence-and-and-or – jarlh Jan 13 '20 at 09:03
  • Given that both queries would result in a syntax error, I'm not quite sure what you're after. – Strawberry Jan 13 '20 at 10:31

2 Answers2

1

if id is 1 or 4 or 3 and count = 0 for all 3 ids then i should eliminate those records

I would phrase this with a not condition:

where not (id in (1, 3, 4) and count = 0)
GMB
  • 216,147
  • 25
  • 84
  • 135
0

You want rows where either:

  • The ids are not 1, 3, 4; or
  • count <> 0.

That would be:

where id not iN (1, 3, 4) or
      count <> 0
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786