Problem:
Say there is a simple (yet big) table foods
id name
-- -----------
01 ginger beer
02 white wine
03 red wine
04 ginger wine
I'd like to count how many entries have specific hardcoded patterns, say contain words 'ginger' (LIKE '%ginger%'
) or 'wine' (LIKE '%wine%'
), or whatever else in them, and write these numbers into rows along comments. The result I'm looking for is the following
comment total
--------------- -----
contains ginger 2
for wine lovers 3
Solution 1 (good format but inefficient):
It is possible to use UNION ALL
and construct the following
SELECT * FROM
(
(
SELECT
'contains ginger' AS comment,
sum((name LIKE '%ginger%')::INT) AS total
FROM foods
)
UNION ALL
(
SELECT
'for wine lovers' AS comment,
sum((name LIKE '%wine%')::INT) AS total
FROM foods
)
)
Apparently it works similarly to simply executing multiple queries and sewing them together afterwards. It is very inefficient.
Solution 2 (efficient but bad format):
The following is multiple times faster compared to previous solution
SELECT
sum((name LIKE '%ginger%')::INT) AS contains_ginger,
sum((name LIKE '%wine%')::INT) AS for_wine_lovers
FROM foods
And the result is
contains_ginger for_wine_lovers
--------------- ---------------
2 3
So it is definitely possible to get the same information much faster, but in a wrong format...
Discussion:
What is the best overall approach? What should I do to get the result I want in an efficient manner and preferable format? Or is it really impossible?
By the way, I am writing this for Redshift (based on PostgreSQL).
Thanks.