How can I use countif statement in PostgreSQL?
max(COUNTIF(t1.A1:C10,t2.a1),COUNTIF(t1.A1:C10,t2.b1),COUNTIF(t1.A1:C10,t2.c1))
I have table1 which is more then a million rows
| a | b | c | M5 |
|---|---|---|---|
| 16 | 27 | 31 | |
| 3 | 7 | 27 |
and table2 more then 100 rows including different dates after column c
| a | b | c |
|---|---|---|
| 10 | 15 | 16 |
| 30 | 40 | 50 |
| 60 | 70 | 80 |
| 16 | 18 | 37 |
| 5 | 12 | 16 |
| 8 | 31 | 28 |
| 11 | 12 | 13 |
| 7 | 9 | 31 |
| 2 | 7 | 21 |
| 20 | 16 | 27 |
| 8 | 12 | 17 |
| 2 | 8 | 14 |
| 3 | 14 | 15 |
The outcome should be something like this
| a | b | c | M5 |
|---|---|---|---|
| 16 | 27 | 31 | 3 |
| 3 | 7 | 27 | 2 |
Tried the below query but the outcome is not correct
UPDATE table1 SET m5 = greatest(
case When a in(select unnest(array[a,b,c]) from (select * from table2 order by date DESC limit 10) foo) then 1 else 0 END,
case When b in(select unnest(array[a,b,c]) from (select * from table2 order by date DESC limit 10) foo) then 1 else 0 END,
case When c in(select unnest(array[a,b,c]) from (select * from table2 order by date DESC limit 10) foo) then 1 else 0 END)