I have three tables, need to get one with summary for the category. Also need to add a summary row.
The main problem is I don`t know how to work with BOOLEAN type.
Tried use
SUM(CASE WHEN was_showed = 'TRUE' THEN 1 ELSE 0 END) but when I tried it just for table1 it returned "3" in every cases...when exactly it should be "6"
The first table1
id was_showed
1 FALSE
2 TRUE
3 TRUE
4 TRUE
5 TRUE
6 FALSE
7 TRUE
8 TRUE
9 TRUE
the second table2
id category
1 test1
2 test2
3 test1
4 test1
5 (null)
6 (null)
7 test1
8 test2
9 test2
the third table3
id was_bought
2 TRUE
4 TRUE
5 TRUE
7 TRUE
The result I want to get by categories:
category | sum(was_showed) | sum(was_bougth)/sum(was_showed)
test1 | 3 | 2/3
test2 | 2 | 1/3
NULL | 1 | 1
last row should be:
all | sum(was_showed) by all rows | sum(was_bougth)/sum(was_showed) by all rows
UPDATE: SQL Fiddle