How to reduce those null rows? I was trying using DISTINCT for id`s. SUM() is casting varchars to numbers. As a result i need only one products_id with all attributes.
I already have this as a query (I've done some pivoting and filtering from bigger db):
SELECT DISTINCT
tab1.products_id,
(CASE WHEN tab1.products_extra_fields_id = 2 THEN tab1.products_extra_fields_value END) AS 'cpu_producer',
(CASE WHEN tab1.products_extra_fields_id = 3 THEN tab1.products_extra_fields_value END) AS 'cpu_clock',
(CASE WHEN tab1.products_extra_fields_id = 4 THEN tab1.products_extra_fields_value END) AS 'cpu_family',
(CASE WHEN tab1.products_extra_fields_id = 5 THEN tab1.products_extra_fields_value END) AS 'cpu_description',
(CASE WHEN tab1.products_extra_fields_id = 8 THEN tab1.products_extra_fields_value END) AS 'gpu_producer',
(CASE WHEN tab1.products_extra_fields_id = 9 THEN tab1.products_extra_fields_value END) AS 'screen_diagonal',
(CASE WHEN tab1.products_extra_fields_id = 10 THEN tab1.products_extra_fields_value END) AS 'system',
(CASE WHEN tab1.products_extra_fields_id = 11 THEN tab1.products_extra_fields_value END) AS 'screen_resolution',
(CASE WHEN tab1.products_extra_fields_id = 12 THEN tab1.products_extra_fields_value END) AS 'gpu_memory',
(CASE WHEN tab1.products_extra_fields_id = 13 THEN tab1.products_extra_fields_value END) AS 'screen_coating',
(CASE WHEN tab1.products_extra_fields_id = 14 THEN tab1.products_extra_fields_value END) AS 'screen_touch',
(CASE WHEN tab1.products_extra_fields_id = 21 THEN tab1.products_extra_fields_value END) AS 'color',
(CASE WHEN tab1.products_extra_fields_id = 22 THEN tab1.products_extra_fields_value END) AS 'type',
(CASE WHEN tab1.products_extra_fields_id = 23 THEN tab1.products_extra_fields_value END) AS 'cpu_threads',
(CASE WHEN tab1.products_extra_fields_id = 24 THEN tab1.products_extra_fields_value END) AS 'ram_type',
(CASE WHEN tab1.products_extra_fields_id = 25 THEN tab1.products_extra_fields_value END) AS 'ram',
(CASE WHEN tab1.products_extra_fields_id = 26 THEN tab1.products_extra_fields_value END) AS 'drive_type',
(CASE WHEN tab1.products_extra_fields_id = 27 THEN tab1.products_extra_fields_value END) AS 'drive_size',
(CASE WHEN tab1.products_extra_fields_id = 28 THEN tab1.products_extra_fields_value END) AS 'gpu_name'
FROM
(Select
products_to_products_extra_fields.products_id,
products_to_products_extra_fields.products_extra_fields_id,
products_to_products_extra_fields.products_extra_fields_value,
products_extra_fields.products_extra_fields_name
From
products_extra_fields Inner Join
products_to_products_extra_fields On products_to_products_extra_fields.products_extra_fields_id =
products_extra_fields.products_extra_fields_id
ORDER BY `products_to_products_extra_fields`.`products_id` ASC)tab1
You can do, by doing an outer Select
wITH A GROUP BY AND max;
The aggregation function depends on the data and whether there are only 1 or more possibilities , so that you must use GROUP_CONCAT ti get them all
SELECT
products_id,
MAX(cpu_producer) cpu_producer,
MAX(cpu_clock) cpu_clock
# ...
FROM
(SELECT DISTINCT
tab1.products_id,
(CASE WHEN tab1.products_extra_fields_id = 2 THEN tab1.products_extra_fields_value END) AS 'cpu_producer',
(CASE WHEN tab1.products_extra_fields_id = 3 THEN tab1.products_extra_fields_value END) AS 'cpu_clock',
(CASE WHEN tab1.products_extra_fields_id = 4 THEN tab1.products_extra_fields_value END) AS 'cpu_family',
(CASE WHEN tab1.products_extra_fields_id = 5 THEN tab1.products_extra_fields_value END) AS 'cpu_description',
(CASE WHEN tab1.products_extra_fields_id = 8 THEN tab1.products_extra_fields_value END) AS 'gpu_producer',
(CASE WHEN tab1.products_extra_fields_id = 9 THEN tab1.products_extra_fields_value END) AS 'screen_diagonal',
(CASE WHEN tab1.products_extra_fields_id = 10 THEN tab1.products_extra_fields_value END) AS 'system',
(CASE WHEN tab1.products_extra_fields_id = 11 THEN tab1.products_extra_fields_value END) AS 'screen_resolution',
(CASE WHEN tab1.products_extra_fields_id = 12 THEN tab1.products_extra_fields_value END) AS 'gpu_memory',
(CASE WHEN tab1.products_extra_fields_id = 13 THEN tab1.products_extra_fields_value END) AS 'screen_coating',
(CASE WHEN tab1.products_extra_fields_id = 14 THEN tab1.products_extra_fields_value END) AS 'screen_touch',
(CASE WHEN tab1.products_extra_fields_id = 21 THEN tab1.products_extra_fields_value END) AS 'color',
(CASE WHEN tab1.products_extra_fields_id = 22 THEN tab1.products_extra_fields_value END) AS 'type',
(CASE WHEN tab1.products_extra_fields_id = 23 THEN tab1.products_extra_fields_value END) AS 'cpu_threads',
(CASE WHEN tab1.products_extra_fields_id = 24 THEN tab1.products_extra_fields_value END) AS 'ram_type',
(CASE WHEN tab1.products_extra_fields_id = 25 THEN tab1.products_extra_fields_value END) AS 'ram',
(CASE WHEN tab1.products_extra_fields_id = 26 THEN tab1.products_extra_fields_value END) AS 'drive_type',
(CASE WHEN tab1.products_extra_fields_id = 27 THEN tab1.products_extra_fields_value END) AS 'drive_size',
(CASE WHEN tab1.products_extra_fields_id = 28 THEN tab1.products_extra_fields_value END) AS 'gpu_name'
FROM
(Select
products_to_products_extra_fields.products_id,
products_to_products_extra_fields.products_extra_fields_id,
products_to_products_extra_fields.products_extra_fields_value,
products_extra_fields.products_extra_fields_name
From
products_extra_fields Inner Join
products_to_products_extra_fields On products_to_products_extra_fields.products_extra_fields_id =
products_extra_fields.products_extra_fields_id
)tab1 ) t1
GROUP BY products_id
ORDER BY products_id ASC
Remove DISTINCT
Add GROUP BY tab1.products_id
Wrap each separate (CASE WHEN ... END) into MAX(), like:
MAX(CASE WHEN tab1.products_extra_fields_id = 2
THEN tab1.products_extra_fields_value
END) AS 'cpu_producer',
Related
I checked several post related my subject, but I cannot find my answer. I also posted similar question but can't get any answer so I prepared posting again.
Because my table's columns have same values and want to count by team and HTFT values.
Expected result :
Team Totals
HTFT(1/1)
HTFT(X/1)
HTFT(X/X)
HTFT(2/2)
HTFT(X/2)
HTFT(1/X)
HTFT(2/X)
HTFT(2/1)
HTFT(1/2)
Adana Demirspor
1
0
1
0
1
1
0
0
0
Alanyaspor
2
0
0
2
0
0
0
0
0
Altay
2
0
0
1
1
0
0
0
0
Antalyaspor
1
2
0
0
0
1
0
0
0
Beşiktaş
3
0
1
0
0
0
0
0
0
Çaykur Rizespor
2
0
1
0
1
0
0
0
0
Fatih Karagümrük
3
0
1
0
0
0
0
0
0
Fenerbahçe
0
1
1
0
2
0
0
0
0
Galatasaray
0
1
0
0
0
0
2
0
0
Gaziantep
2
1
1
0
0
0
0
0
0
Giresunspor
2
0
0
1
0
0
0
0
0
Göztepe
0
0
0
0
1
2
0
1
0
Hatayspor
1
1
0
0
1
0
1
0
0
İstanbul Başakşehir
0
0
0
2
0
0
0
2
0
Kasımpaşa
2
0
0
0
0
0
2
0
0
Kayserispor
2
0
0
1
0
1
0
0
0
Konyaspor
1
0
1
0
1
0
0
1
0
Sivasspor
1
0
1
0
1
1
0
0
0
Trabzonspor
1
0
0
2
0
0
1
0
0
Yeni Malatyaspor
2
0
0
1
1
0
0
0
0
I have below fiddle link that have my tables, sample database and two queries.
My Sample database fiddle link
I want to merge two queries in one query is it possible?
Regards,
WITH
Home AS (
SELECT m.home_team_id AS team_id,t.team_name,
SUM(CASE WHEN m.htft = '1/1' THEN 1 ELSE 0 END) AS 'h 1/1',
SUM(CASE WHEN m.htft = 'X/1' THEN 1 ELSE 0 END) AS 'h X/1',
SUM(CASE WHEN m.htft = 'X/X' THEN 1 ELSE 0 END) AS 'h X/X',
SUM(CASE WHEN m.htft = '2/2' THEN 1 ELSE 0 END) AS 'h 2/2',
SUM(CASE WHEN m.htft = 'X/2' THEN 1 ELSE 0 END) AS 'h X/2',
SUM(CASE WHEN m.htft = '1/X' THEN 1 ELSE 0 END) AS 'h 1/X',
SUM(CASE WHEN m.htft = '2/X' THEN 1 ELSE 0 END) AS 'h 2/X',
SUM(CASE WHEN m.htft = '2/1' THEN 1 ELSE 0 END) AS 'h 2/1',
SUM(CASE WHEN m.htft = '1/2' THEN 1 ELSE 0 END) AS 'h 1/2'
FROM matches m
INNER JOIN teams t on m.home_team_id = t.team_id
GROUP BY 1,2
),
Away AS (
SELECT m.away_team_id AS team_id,t.team_name,
SUM(CASE WHEN m.htft = '1/1' THEN 1 ELSE 0 END) AS 'a 1/1',
SUM(CASE WHEN m.htft = 'X/1' THEN 1 ELSE 0 END) AS 'a X/1',
SUM(CASE WHEN m.htft = 'X/X' THEN 1 ELSE 0 END) AS 'a X/X',
SUM(CASE WHEN m.htft = '2/2' THEN 1 ELSE 0 END) AS 'a 2/2',
SUM(CASE WHEN m.htft = 'X/2' THEN 1 ELSE 0 END) AS 'a X/2',
SUM(CASE WHEN m.htft = '1/X' THEN 1 ELSE 0 END) AS 'a 1/X',
SUM(CASE WHEN m.htft = '2/X' THEN 1 ELSE 0 END) AS 'a 2/X',
SUM(CASE WHEN m.htft = '2/1' THEN 1 ELSE 0 END) AS 'a 2/1',
SUM(CASE WHEN m.htft = '1/2' THEN 1 ELSE 0 END) AS 'a 1/2'
FROM matches m
INNER JOIN teams t on m.away_team_id = t.team_id
GROUP BY 1,2
)
SELECT *
FROM Home
JOIN Away USING (team_id, team_name)
ORDER BY 1
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=8c7d73ac3d6f892d7c9d415cf3baef3d
Please try this,
SELECT
Home as Team
,SUM(1by1) as '1/1'
,SUM(Xby1) as 'X/1'
,SUM(XbyX) AS 'X/X'
,SUM(2by2) AS '2/2'
,SUM(Xby2) AS 'X/2'
,SUM(1byX) AS '1/X'
,SUM(2byX) AS '2/X'
,SUM(2by1) AS '2/1'
,SUM(1by2) AS '1/2'
FROM
(
SELECT m.home_team_id ,t.team_name as Home,
SUM(CASE WHEN m.htft = '1/1' THEN 1 ELSE 0 END) AS '1by1',
SUM(CASE WHEN m.htft = 'X/1' THEN 1 ELSE 0 END) AS 'Xby1',
SUM(CASE WHEN m.htft = 'X/X' THEN 1 ELSE 0 END) AS 'XbyX',
SUM(CASE WHEN m.htft = '2/2' THEN 1 ELSE 0 END) AS '2by2',
SUM(CASE WHEN m.htft = 'X/2' THEN 1 ELSE 0 END) AS 'Xby2',
SUM(CASE WHEN m.htft = '1/X' THEN 1 ELSE 0 END) AS '1byX',
SUM(CASE WHEN m.htft = '2/X' THEN 1 ELSE 0 END) AS '2byX',
SUM(CASE WHEN m.htft = '2/1' THEN 1 ELSE 0 END) AS '2by1',
SUM(CASE WHEN m.htft = '1/2' THEN 1 ELSE 0 END) AS '1by2'
FROM matches m
INNER JOIN teams t on m.home_team_id = t.team_id
GROUP BY m.home_team_id,Home
union all
SELECT m.away_team_id,t.team_name as Away,
SUM(CASE WHEN m.htft = '1/1' THEN 1 ELSE 0 END) AS '1by1',
SUM(CASE WHEN m.htft = 'X/1' THEN 1 ELSE 0 END) AS 'Xby1',
SUM(CASE WHEN m.htft = 'X/X' THEN 1 ELSE 0 END) AS 'XbyX',
SUM(CASE WHEN m.htft = '2/2' THEN 1 ELSE 0 END) AS '2by2',
SUM(CASE WHEN m.htft = 'X/2' THEN 1 ELSE 0 END) AS 'Xby2',
SUM(CASE WHEN m.htft = '1/X' THEN 1 ELSE 0 END) AS '1byX',
SUM(CASE WHEN m.htft = '2/X' THEN 1 ELSE 0 END) AS '2byX',
SUM(CASE WHEN m.htft = '2/1' THEN 1 ELSE 0 END) AS '2by1',
SUM(CASE WHEN m.htft = '1/2' THEN 1 ELSE 0 END) AS '1by2'
FROM matches m
INNER JOIN teams t on m.away_team_id = t.team_id
GROUP BY m.away_team_id,Away
)A
GROUP BY TEAM
order by 1
;
query with team id filter,
SELECT
home_team_id as team_id
,Home as Team
,SUM(1by1) as '1/1'
,SUM(Xby1) as 'X/1'
,SUM(XbyX) AS 'X/X'
,SUM(2by2) AS '2/2'
,SUM(Xby2) AS 'X/2'
,SUM(1byX) AS '1/X'
,SUM(2byX) AS '2/X'
,SUM(2by1) AS '2/1'
,SUM(1by2) AS '1/2'
FROM
(
SELECT m.home_team_id ,t.team_name as Home,
SUM(CASE WHEN m.htft = '1/1' THEN 1 ELSE 0 END) AS '1by1',
SUM(CASE WHEN m.htft = 'X/1' THEN 1 ELSE 0 END) AS 'Xby1',
SUM(CASE WHEN m.htft = 'X/X' THEN 1 ELSE 0 END) AS 'XbyX',
SUM(CASE WHEN m.htft = '2/2' THEN 1 ELSE 0 END) AS '2by2',
SUM(CASE WHEN m.htft = 'X/2' THEN 1 ELSE 0 END) AS 'Xby2',
SUM(CASE WHEN m.htft = '1/X' THEN 1 ELSE 0 END) AS '1byX',
SUM(CASE WHEN m.htft = '2/X' THEN 1 ELSE 0 END) AS '2byX',
SUM(CASE WHEN m.htft = '2/1' THEN 1 ELSE 0 END) AS '2by1',
SUM(CASE WHEN m.htft = '1/2' THEN 1 ELSE 0 END) AS '1by2'
FROM matches m
INNER JOIN teams t on m.home_team_id = t.team_id
GROUP BY m.home_team_id,Home
union all
SELECT m.away_team_id,t.team_name as Away,
SUM(CASE WHEN m.htft = '1/1' THEN 1 ELSE 0 END) AS '1by1',
SUM(CASE WHEN m.htft = 'X/1' THEN 1 ELSE 0 END) AS 'Xby1',
SUM(CASE WHEN m.htft = 'X/X' THEN 1 ELSE 0 END) AS 'XbyX',
SUM(CASE WHEN m.htft = '2/2' THEN 1 ELSE 0 END) AS '2by2',
SUM(CASE WHEN m.htft = 'X/2' THEN 1 ELSE 0 END) AS 'Xby2',
SUM(CASE WHEN m.htft = '1/X' THEN 1 ELSE 0 END) AS '1byX',
SUM(CASE WHEN m.htft = '2/X' THEN 1 ELSE 0 END) AS '2byX',
SUM(CASE WHEN m.htft = '2/1' THEN 1 ELSE 0 END) AS '2by1',
SUM(CASE WHEN m.htft = '1/2' THEN 1 ELSE 0 END) AS '1by2'
FROM matches m
INNER JOIN teams t on m.away_team_id = t.team_id
GROUP BY m.away_team_id,Away
)A
where home_team_id=8
GROUP BY TEAM, home_team_id
order by 1
;
I have 2 result sets, how do i combine them such that they are in a single row?
Result 1:
North | South| East | West
---------------------------
5 0 0 5
SQL:
SELECT
SUM(CASE WHEN region = 'North' THEN threatlevel ELSE 0 END) AS North,
SUM(CASE WHEN region = 'South' THEN threatlevel ELSE 0 END) AS South,
SUM(CASE WHEN region = 'East' THEN threatlevel ELSE 0 END) AS East,
SUM(CASE WHEN region = 'West' THEN threatlevel ELSE 0 END) AS West,
FROM incident
GROUP BY typeOfIncident
HAVING typeOfIncident LIKE '%H1N5%';
Result 2:
North1 | South1 | East1 | West1
--------------------------------
0 5 5 8
SQL:
SELECT
SUM(CASE WHEN region = 'North' THEN threatlevel ELSE 0 END) AS North1,
SUM(CASE WHEN region = 'South' THEN threatlevel ELSE 0 END) AS South1,
SUM(CASE WHEN region = 'East' THEN threatlevel ELSE 0 END) AS East1,
SUM(CASE WHEN region = 'West' THEN threatlevel ELSE 0 END) AS West1,
FROM incident
GROUP BY typeOfIncident
HAVING typeOfIncident LIKE 'Zombie%';
OUTPUT:
North | South | East | West | North1 | South1 | East1 | West1
--------------------------------------------------------------
5 0 0 5 0 5 5 8
I've tried using union, cross join etc. No results were returned. Any help would be appreciated.
Try this:
SELECT
SUM(CASE WHEN typeOfIncident LIKE '%H1N5%' AND
region = 'North' THEN threatlevel ELSE 0 END) AS North,
SUM(CASE WHEN typeOfIncident LIKE '%H1N5%' AND
region = 'South' THEN threatlevel ELSE 0 END) AS South,
SUM(CASE WHEN typeOfIncident LIKE '%H1N5%' AND
region = 'East' THEN threatlevel ELSE 0 END) AS East,
SUM(CASE WHEN typeOfIncident LIKE '%H1N5%' AND
region = 'West' THEN threatlevel ELSE 0 END) AS West,
SUM(CASE WHEN typeOfIncident LIKE 'Zombie%' AND
region = 'North' THEN threatlevel ELSE 0 END) AS North1,
SUM(CASE WHEN typeOfIncident LIKE 'Zombie%' AND
region = 'South' THEN threatlevel ELSE 0 END) AS South1,
SUM(CASE WHEN typeOfIncident LIKE 'Zombie%' AND
region = 'East' THEN threatlevel ELSE 0 END) AS East1,
SUM(CASE WHEN typeOfIncident LIKE 'Zombie%' AND
region = 'West' THEN threatlevel ELSE 0 END) AS West1
FROM incident
I have a table
Name duration date_leave_from date_leave_to
John 1 2015-03-01 2015-03-01
Doe .5 2015-03-02 2015-03-02
John .5 2015-03-02 2015-03-02
Doe 1 2015-01-03 2015-01-03
Doe 1 2015-02-04 2015-02-04
I would like to get the data in the following format:
Name || Jan || Feb || March || April .. to DEC
John || 1 || 0 || 1.5 || 0
Doe || 0 || 1 || .5 || 0
You can do like this:
SELECT name,
SUM(CASE WHEN MONTH(from_date) = '1' THEN duration else 0 END) AS Jan,
SUM(CASE WHEN MONTH(from_date) = '2' THEN duration else 0 END) AS Feb,
SUM(CASE WHEN MONTH(from_date) = '3' THEN duration else 0 END) AS March,
SUM(CASE WHEN MONTH(from_date) = '4' THEN duration else 0 END) AS April,
SUM(CASE WHEN MONTH(from_date) = '5' THEN duration else 0 END) AS May,
SUM(CASE WHEN MONTH(from_date) = '6' THEN duration else 0 END) AS Jun,
SUM(CASE WHEN MONTH(from_date) = '7' THEN duration else 0 END) AS Jul,
SUM(CASE WHEN MONTH(from_date) = '8' THEN duration else 0 END) AS Aug,
SUM(CASE WHEN MONTH(from_date) = '9' THEN duration else 0 END) AS Sep,
SUM(CASE WHEN MONTH(from_date) = '10' THEN duration else 0 END) AS Oct,
SUM(CASE WHEN MONTH(from_date) = '11' THEN duration else 0 END) AS Nov,
SUM(CASE WHEN MONTH(from_date) = '12' THEN duration else 0 END) AS December
FROM test
GROUP BY name
This is the fiddle
I have result set like this after executing "select * from temp"
id | Month | value1 | value2 |
------------------------------
1 | Apr | 100 | 150
2 | May | 50 | 75
3 | Jan | 50 | 75
5 | Feb | 50 | 75
6 | mar | 50 | 75
I want to change it to column to rows like pivot table.
i used case when statement to change it.
select
'value1' as Field,
SUM(CASE
WHEN dc.month = 'January' THEN dc.value1
ELSE ''
END) AS January,
SUM(CASE
WHEN dc.month = 'February' THEN dc.value1
ELSE ''
END) AS February,
SUM(CASE
WHEN dc.month = 'March' THEN dc.value1
ELSE ''
END) AS March
SUM(CASE
WHEN dc.month = 'April' THEN dc.value1
ELSE ''
END) AS April,
SUM(CASE
WHEN dc.month = 'May' THEN dc.value1
ELSE ''
END) AS May
from
(select
* from
temp)dc
Field | Jan | Feb | Mar | Apr | May
-------------------------------------
Value1 | 50 | 50 | 50 | 100 | 50
I want to get same result set for value2 in same query without using UNION. Because temp table has lot of data's.
Field | Jan | Feb | Mar | Apr | May
-------------------------------------
Value1 | 50 | 50 | 50 | 100 | 50
Value1 | 75 | 75 | 75 | 150 | 75
This solution uses a union, but only to create "fake" rows to represent each value* column, and then joins and groups by those rows:
SELECT
`field`,
SUM(CASE
WHEN dc.month = 'Jan' THEN
CASE `field`
WHEN 'value1' THEN dc.value1
WHEN 'value2' THEN dc.value2
END
ELSE ''
END) AS January,
SUM(CASE
WHEN dc.month = 'Feb' THEN
CASE `field`
WHEN 'value1' THEN dc.value1
WHEN 'value2' THEN dc.value2
END
ELSE ''
END) AS February,
SUM(CASE
WHEN dc.month = 'Mar' THEN
CASE `field`
WHEN 'value1' THEN dc.value1
WHEN 'value2' THEN dc.value2
END
ELSE ''
END) AS March,
SUM(CASE
WHEN dc.month = 'Apr' THEN
CASE `field`
WHEN 'value1' THEN dc.value1
WHEN 'value2' THEN dc.value2
END
ELSE ''
END) AS April,
SUM(CASE
WHEN dc.month = 'May' THEN
CASE `field`
WHEN 'value1' THEN dc.value1
WHEN 'value2' THEN dc.value2
END
ELSE ''
END) AS May
FROM
(SELECT
* FROM temp
)dc
INNER JOIN (
SELECT 'value1' AS `field`
UNION SELECT 'value2'
) AS value_columns
GROUP BY `field`
If its just 2 colums i.e. value1 and value2 then you can use UNION as DEMO
select
'value1' as Field,
SUM(
CASE
WHEN dc.month = 'Jan' THEN dc.value1
ELSE ''
END) AS January,
SUM(
CASE
WHEN dc.month = 'Feb' THEN dc.value1
ELSE ''
END) AS February,
SUM(
CASE
WHEN dc.month = 'Mar' THEN dc.value1
ELSE ''
END) AS March,
SUM(
CASE
WHEN dc.month = 'Apr' THEN dc.value1
ELSE ''
END) AS April,
SUM(
CASE
WHEN dc.month = 'May' THEN dc.value1
ELSE ''
END)AS May
from
temp dc
UNION
select
'value2' as Field,
SUM(
CASE
WHEN dc.month = 'Jan' THEN dc.value2
ELSE ''
END) AS January,
SUM(
CASE
WHEN dc.month = 'Feb' THEN dc.value2
ELSE ''
END) AS February,
SUM(
CASE
WHEN dc.month = 'Mar' THEN dc.value2
ELSE ''
END) AS March,
SUM(
CASE
WHEN dc.month = 'Apr' THEN dc.value2
ELSE ''
END) AS April,
SUM(
CASE
WHEN dc.month = 'May' THEN dc.value2
ELSE ''
END)AS May
from temp dc
I'm trying to dynamically use the dates within the date range of the where clause as the column names in the results. I know I can hard code this using a case/when statement:
sum(case when day(date_accessed) = 1 THEN 1 ELSE 0 END) AS `Jan 1 2014`...
Doing this every month will be cumbersome. I also know I can use php or some other scripting language to build a table, but I'm trying to accomplish all of this in mysql. Here's the query I have.
SELECT count(*) AS `Total Clicks`, day(date_accessed)
FROM Client_Activity_Log a WHERE date_accessed between '2014-01-01' AND '2014-01-31'
GROUP BY day(date_accessed);
The above query will put each date into it's own row and total it, but I'd like each date to be column. I want the results to look like this (all days of month):
Jan 1 2014 Jan 2 2014
150 200
I may have found a decent solution. Let me know if anyone has a better one:
I may have figured out the easiest solution. It won't provide the month or year in the column name but it may be as close as I get:
SELECT
SUM(CASE WHEN day(date_accessed) = 1 THEN 1 ELSE 0 END) AS '1',
SUM(CASE WHEN day(date_accessed) = 2 THEN 1 ELSE 0 END) AS `2`,
SUM(CASE WHEN day(date_accessed) = 3 THEN 1 ELSE 0 END) AS `3`,
SUM(CASE WHEN day(date_accessed) = 4 THEN 1 ELSE 0 END) AS `4`,
SUM(CASE WHEN day(date_accessed) = 5 THEN 1 ELSE 0 END) AS `5`,
SUM(CASE WHEN day(date_accessed) = 6 THEN 1 ELSE 0 END) AS `6`,
SUM(CASE WHEN day(date_accessed) = 7 THEN 1 ELSE 0 END) AS `7`,
SUM(CASE WHEN day(date_accessed) = 8 THEN 1 ELSE 0 END) AS `8`,
SUM(CASE WHEN day(date_accessed) = 9 THEN 1 ELSE 0 END) AS `9`,
SUM(CASE WHEN day(date_accessed) = 10 THEN 1 ELSE 0 END) AS `10`,
SUM(CASE WHEN day(date_accessed) = 11 THEN 1 ELSE 0 END) AS `11`,
SUM(CASE WHEN day(date_accessed) = 12 THEN 1 ELSE 0 END) AS `12`,
SUM(CASE WHEN day(date_accessed) = 13 THEN 1 ELSE 0 END) AS `13`,
SUM(CASE WHEN day(date_accessed) = 14 THEN 1 ELSE 0 END) AS `14`,
SUM(CASE WHEN day(date_accessed) = 15 THEN 1 ELSE 0 END) AS `15`,
SUM(CASE WHEN day(date_accessed) = 16 THEN 1 ELSE 0 END) AS `16`,
SUM(CASE WHEN day(date_accessed) = 17 THEN 1 ELSE 0 END) AS `17`,
SUM(CASE WHEN day(date_accessed) = 18 THEN 1 ELSE 0 END) AS `18`,
SUM(CASE WHEN day(date_accessed) = 19 THEN 1 ELSE 0 END) AS `19`,
SUM(CASE WHEN day(date_accessed) = 20 THEN 1 ELSE 0 END) AS `20`,
SUM(CASE WHEN day(date_accessed) = 21 THEN 1 ELSE 0 END) AS `21`,
SUM(CASE WHEN day(date_accessed) = 22 THEN 1 ELSE 0 END) AS `22`,
SUM(CASE WHEN day(date_accessed) = 23 THEN 1 ELSE 0 END) AS `23`,
SUM(CASE WHEN day(date_accessed) = 24 THEN 1 ELSE 0 END) AS `24`,
SUM(CASE WHEN day(date_accessed) = 25 THEN 1 ELSE 0 END) AS `25`,
SUM(CASE WHEN day(date_accessed) = 26 THEN 1 ELSE 0 END) AS `26`,
SUM(CASE WHEN day(date_accessed) = 27 THEN 1 ELSE 0 END) AS `27`,
SUM(CASE WHEN day(date_accessed) = 28 THEN 1 ELSE 0 END) AS `28`,
SUM(CASE WHEN day(date_accessed) = 29 THEN 1 ELSE 0 END) AS `29`,
SUM(CASE WHEN day(date_accessed) = 30 THEN 1 ELSE 0 END) AS `30`,
SUM(CASE WHEN day(date_accessed) = 31 THEN 1 ELSE 0 END) AS `31`
FROM Client_Activity_Log a WHERE date_accessed between '2014-01-01' AND '2014-01-31';