結果を行として取得する場合:
SELECT 'Search' AS Task, COUNT(ID) AS Counts
FROM MyTable
WHERE task IN('search', 'Basic', 'Natural search')
UNION ALL
SELECT 'Query', COUNT(ID)
FROM MyTable
WHERE task IN('Query1', 'Query2', 'Query3')
UNION ALL
SELECT 'Sample', COUNT(ID)
FROM MyTable
WHERE task IN('sample1', 'sample2')
UNION ALL
SELECT 'Test', COUNT(ID)
FROM MyTable
WHERE task IN('test1', 'test2', 'test3');
出力:
| TASK | COUNTS |
|--------|--------|
| Search | 3 |
| Query | 3 |
| Sample | 2 |
| Test | 3 |
結果を列として表示する場合:
SELECT
SUM(CASE WHEN Task = 'Search' THEN 1
WHEN Task = 'Basic' THEN 1
WHEN Task = 'Natural search' THEN 1
ELSE 0 END) AS SEARCH
,SUM(CASE WHEN Task = 'Query1' THEN 1
WHEN Task = 'Query2' THEN 1
WHEN Task = 'Query3' THEN 1
ELSE 0 END) AS Query
,SUM(CASE WHEN Task = 'Sample1' THEN 1
WHEN Task = 'sample2' THEN 1
ELSE 0 END) AS Sample
,SUM(CASE WHEN Task = 'test1' THEN 1
WHEN Task = 'test2' THEN 1
WHEN Task = 'test3' THEN 1
ELSE 0 END) AS Test
FROM MyTable;
出力:
| SEARCH | QUERY | SAMPLE | TEST |
|--------|-------|--------|------|
| 3 | 3 | 2 | 3 |