I cannot find similar question related specially to my case. So I post this question. And also ne need to provide sample data because below informations are enough.
My matches table is:
home_team_id
away_team_id
htft
11
9
2/2
18
17
X/2
20
19
2/2
1
8
X/2
4
12
1/X
14
2
2/2
3
16
1/1
13
15
2/X
7
10
1/1
5
6
1/1
9
13
1/1
and teams table is:
team_id
team_name
5
Arsenal
6
Aston Villa
7
Brentford
8
Brighton & Hove Albion
9
Burnley
12
Chelsea
19
Crystal Palace
18
Everton
20
Leeds United
31
Leicester City
32
Liverpool
I have 2 queries. One is show home matches count and second one is away team count results. There are working fine.
But how can I merge these two queries at one query?
My tables structures are;
teams table fields: team_id, team_name
matches table fields: home_team_id, away.team_id, htft
Home Query:
SELECT m.home_team_id,t.team_name as Home,
SUM(CASE WHEN m.htft = '1/1' THEN 1 ELSE 0 END) AS '1/1',
SUM(CASE WHEN m.htft = 'X/1' THEN 1 ELSE 0 END) AS 'X/1',
SUM(CASE WHEN m.htft = 'X/X' THEN 1 ELSE 0 END) AS 'X/X',
SUM(CASE WHEN m.htft = '2/2' THEN 1 ELSE 0 END) AS '2/2',
SUM(CASE WHEN m.htft = 'X/2' THEN 1 ELSE 0 END) AS 'X/2',
SUM(CASE WHEN m.htft = '1/X' THEN 1 ELSE 0 END) AS '1/X',
SUM(CASE WHEN m.htft = '2/X' THEN 1 ELSE 0 END) AS '2/X',
SUM(CASE WHEN m.htft = '2/1' THEN 1 ELSE 0 END) AS '2/1',
SUM(CASE WHEN m.htft = '1/2' THEN 1 ELSE 0 END) AS '1/2'
FROM matches m, teams t
where m.home_team_id = t.team_id
GROUP BY m.home_team_id;
Output:
home_team_id
Home
1/1
X/1
X/X
2/2
X/2
1/X
2/X
2/1
1/2
5
Arsenal
1
0
0
0
0
0
0
0
0
7
Brentford
1
0
0
0
0
0
0
0
0
9
Burnley
1
0
0
0
0
0
0
0
0
18
Everton
0
0
0
0
1
0
0
0
0
20
Leeds United
0
0
0
1
0
0
0
0
0
Away Query:
SELECT m.away_team_id,t.team_name as Away,
SUM(CASE WHEN m.htft = '1/1' THEN 1 ELSE 0 END) AS '1/1',
SUM(CASE WHEN m.htft = 'X/1' THEN 1 ELSE 0 END) AS 'X/1',
SUM(CASE WHEN m.htft = 'X/X' THEN 1 ELSE 0 END) AS 'X/X',
SUM(CASE WHEN m.htft = '2/2' THEN 1 ELSE 0 END) AS '2/2',
SUM(CASE WHEN m.htft = 'X/2' THEN 1 ELSE 0 END) AS 'X/2',
SUM(CASE WHEN m.htft = '1/X' THEN 1 ELSE 0 END) AS '1/X',
SUM(CASE WHEN m.htft = '2/X' THEN 1 ELSE 0 END) AS '2/X',
SUM(CASE WHEN m.htft = '2/1' THEN 1 ELSE 0 END) AS '2/1',
SUM(CASE WHEN m.htft = '1/2' THEN 1 ELSE 0 END) AS '1/2'
FROM matches m, teams t
where m.away_team_id = t.team_id
GROUP BY m.away_team_id;
Output:
away_team_id
Away
1/1
X/1
X/X
2/2
X/2
1/X
2/X
2/1
1/2
6
Aston Villa
1
0
0
0
0
0
0
0
0
8
Brighton & Hove Albion
0
0
0
0
1
0
0
0
0
9
Burnley
0
0
0
1
0
0
0
0
0
12
Chelsea
0
0
0
0
0
1
0
0
0
19
Crystal Palace
0
0
0
1
0
0
0
0
0
How can I merge or combine both queries. I think if both queries possible to merge it will give the result I expected.
Thanks in advance to those who will help.
Expected result :
Team
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)
Arsenal
2
1
0
1
1
2
1
0
1
Aston Villa
0
1
0
1
1
2
1
0
1
Everton
1
1
2
1
0
1
1
2
1
Current Home results Query
Current Away results Query
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'm trying to count the number of occurences based severity level (1-5) on distinct dates. Note I have another table but severity levels are words (High, Medium and Low...not 1 to 5).
Example of DB:
DATE LEVEL COUNT
---- ----- -----
05/11/2018 3 14
05/11/2018 5 11
05/11/2018 5 11
05/12/2018 3 14
05/12/2018 2 14
05/13/2018 2 11
05/13/2018 1 12
Expected output
Date 1 2 3 4 5
--------- -- -- -- -- --
05/11/2018 0 0 14 0 22
05/12/2018 0 14 14 0 0
05/13/2018 12 11 0 0 0
Expected output 2
Level 05/11/2018 05/12/2018 05/13/2018
--------- ---------- ---------- ----------
1 0 0 12
2 0 14 11
3 14 14 0
4 0 0 0
5 22 0 0
I tried
SELECT CONCAT(DAY(`DATE`) ,MONTH(`DATE`) , YEAR(`DATE`)) AS DDMMYYYY ,
COUNT(DISTINCT LEVEL) as NumCount
FROM `myDatabase`
GROUP BY CONCAT(DAY(`DATE`),MONTH(`DATE`), YEAR(`DATE`) )
but I'm getting the number of different counts..
Any guidance would be appreciated! Thx!
You can't really do pivot tables in MySQL. However with a fixed number of columns (such as expected output #1) you can simulate them with CASE statements e.g.
select date_format(date, '%d%m%Y') as Date,
sum(case when level=1 then count else 0 end) as `1`,
sum(case when level=2 then count else 0 end) as `2`,
sum(case when level=3 then count else 0 end) as `3`,
sum(case when level=4 then count else 0 end) as `4`,
sum(case when level=5 then count else 0 end) as `5`
from table1
group by Date
Output:
Date 1 2 3 4 5
11052018 0 0 14 0 22
12052018 0 14 14 0 0
13052018 12 11 0 0 0
I have BIGINT column in my Redshift table, and I want a query that will:
Count how many times the value '1' appears in each bit position across the binary value in all the rows of this column
Will show it in a way that I'll be able to take the x top bits_positions.
For example (I'm already writing the integer values as binary to simplify the example):
column
--------
11011110 = 222
00000000 = 0
11111100 = 252
00011000 = 24
11111100 = 252
00011000 = 24
11000010 = 194
76543210 <- bit_position
will return a table like:
bit_position count
0 0
1 2
2 3
3 5
4 5
5 2
6 4
7 4
In this case I'll be able to get the top five bit_position: (3,4,6,7,2)
Note: I'll might have up to 64 bit_positions for a column.
You can use a bit-wise AND & to check for each position.
Here's an example going across rows:
SELECT SUM(CASE WHEN bit_col & 64 > 0 THEN 1 ELSE 0 END) "1000000"
, SUM(CASE WHEN bit_col & 32 > 0 THEN 1 ELSE 0 END) "0100000"
, SUM(CASE WHEN bit_col & 16 > 0 THEN 1 ELSE 0 END) "0010000"
, SUM(CASE WHEN bit_col & 8 > 0 THEN 1 ELSE 0 END) "0001000"
, SUM(CASE WHEN bit_col & 4 > 0 THEN 1 ELSE 0 END) "0000100"
, SUM(CASE WHEN bit_col & 2 > 0 THEN 1 ELSE 0 END) "0000010"
, SUM(CASE WHEN bit_col & 1 > 0 THEN 1 ELSE 0 END) "0000001"
FROM my_table
;
1000000 | 0100000 | 0010000 | 0001000 | 0000100 | 0000010 | 0000001
---------+---------+---------+---------+---------+---------+---------
11 | 8 | 11 | 13 | 11 | 9 | 8
To have the results in a single column you need to use union:
SELECT 1 AS "col", SUM(CASE WHEN bit_col & 64 > 0 THEN 1 ELSE 0 END) AS bit_count FROM my_table
UNION ALL SELECT 2 AS "col", SUM(CASE WHEN bit_col & 32 > 0 THEN 1 ELSE 0 END) AS bit_count FROM my_table
UNION ALL SELECT 3 AS "col", SUM(CASE WHEN bit_col & 16 > 0 THEN 1 ELSE 0 END) AS bit_count FROM my_table
UNION ALL SELECT 4 AS "col", SUM(CASE WHEN bit_col & 8 > 0 THEN 1 ELSE 0 END) AS bit_count FROM my_table
UNION ALL SELECT 5 AS "col", SUM(CASE WHEN bit_col & 4 > 0 THEN 1 ELSE 0 END) AS bit_count FROM my_table
UNION ALL SELECT 6 AS "col", SUM(CASE WHEN bit_col & 2 > 0 THEN 1 ELSE 0 END) AS bit_count FROM my_table
UNION ALL SELECT 7 AS "col", SUM(CASE WHEN bit_col & 1 > 0 THEN 1 ELSE 0 END) AS bit_count FROM my_table
ORDER BY bit_count DESC
;
position | bit_count
----------+-----------
6 | 6
7 | 6
4 | 4
5 | 4
2 | 0
3 | 0
1 | 0
http://docs.aws.amazon.com/redshift/latest/dg/r_OPERATOR_SYMBOLS.html
EDIT: If you would like something more dynamic you will need to look into using a UDF. You could start with my f_bitwise_to_string UDF as a template and add what you need from there. https://github.com/awslabs/amazon-redshift-udfs/blob/master/scalar-udfs/f_bitwise_to_string.sql
I'm trying to show all policy business units but is only showing when exist values
My tables:
|policies|
|id| |client| |policy_business_unit_id| |cia_ensure_id| |state|
1 MATT 1 1 0
2 STEVE 1 1 0
3 BILL 1 2 0
4 LARRY 1 2 1
5 MATT 1 2 1
6 STEVE 2 2 2
7 BILL 2 2 2
8 LARRY 2 2 1
9 MATT 2 1 1
|policy_business_units|
|id| |name| |comercial_area_id|
1 LIFE 1
2 ROB 1
3 CAR 1
4 RARE 1
5 RARE2 1
|comercial_areas|
|id| |name|
1 MICROSOFT
2 APPLE
|cia_ensures|
|id| |name|
1 ORANGE
2 BT
3 ATT
4 MOVISTAR
5 SPRINT
I'm getting this result it's ok but is not showing all policy_business units
UNIT V1 A1 N1 V2 A2 N2 V3 A3 N3 V4 A4 N4 V5 A5 N5
LIFE 2 0 0 1 2 0 0 0 0 0 0 0 0 0 0
ROB 0 1 0 0 1 2 0 0 0 0 0 0 0 0 0
Here is the query http://sqlfiddle.com/#!2/6909d8/1
set ##local.group_concat_max_len=10000;
select distinct #sql := concat('SELECT pb.name as unit,',
group_concat(concat(
'SUM(CASE WHEN p.state =0 AND ce.id=',id,' THEN 1 ELSE 0 END ) AS v',id,',
SUM(CASE WHEN p.state =1 AND ce.id=',id,' THEN 1 ELSE 0 END ) AS a',id,',
SUM(CASE WHEN p.state =2 AND ce.id=',id,' THEN 1 ELSE 0 END ) AS n',id)),
'
FROM cia_ensures ce
LEFT JOIN policies p on ce.id = p.cia_ensure_id
INNER JOIN policy_business_units pb ON pb.id = p.policy_business_unit_id
INNER JOIN comercial_areas ca ON ca.id = pb.comercial_area_id
AND ca.id=1
Group by p.policy_business_unit_id;')
from cia_ensures
where id in(1,2,3,4,5,6,7,8,10);
prepare stmt from #sql;
execute stmt;
I'm trying to show this:
UNIT V1 A1 N1 V2 A2 N2 V3 A3 N3 V4 A4 N4 V5 A5 N5
LIFE 2 0 0 1 2 0 0 0 0 0 0 0 0 0 0
ROB 0 1 0 0 1 2 0 0 0 0 0 0 0 0 0
RARE 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
RARE2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Please somebody can help me?
I will appreciate all help.
Please check below changes if it helps you, I did not execute it just do some changes in query, so need to check at your end:
set ##local.group_concat_max_len=10000;
select distinct #sql := concat('SELECT pb.name as unit,',
group_concat(concat(
'SUM(CASE WHEN p.state =0 AND ce.id=',id,' THEN 1 ELSE 0 END ) AS v',id,',
SUM(CASE WHEN p.state =1 AND ce.id=',id,' THEN 1 ELSE 0 END ) AS a',id,',
SUM(CASE WHEN p.state =2 AND ce.id=',id,' THEN 1 ELSE 0 END ) AS n',id)),
'
FROM
policy_business_units pb
LEFT JOIN policies p
ON pb.id = p.policy_business_unit_id
LEFT JOIN cia_ensures ce
on ce.id = p.cia_ensure_id
LEFT JOIN comercial_areas ca
ON ca.id = pb.comercial_area_id
AND ca.id=1
Group by pb.name;')
from cia_ensures
where id in(1,2,3,4,5,6,7,8,10);
prepare stmt from #sql;
execute stmt;
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';