Use date range in where clause as column names - mysql

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';

Related

How to merge two SQL/MySQL queries in to a single query?

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
;

Reducing null rows in mysql query

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',

SQL Query: Number of applications by month, county, & program

I need to generate a list of expenditures & Number of applications by month, county & program. So far i have been able to get a list of expenditures but i am having trouble getting a list of number of applications per month. Here is the query i have thus far but the number of applications is incorrect.
select
servicecounty AS County,
program,
sum(case when month(entrydate) = 1 and year(entrydate) = 2014 then totalpaymenttotal else 0 end) as January,
sum(case when month(entrydate) = 2 and year(entrydate) = 2014 then totalpaymenttotal else 0 end) as February,
sum(case when month(entrydate) = 3 and year(entrydate) = 2014 then totalpaymenttotal else 0 end) as March,
sum(case when month(entrydate) = 4 and year(entrydate) = 2014 then totalpaymenttotal else 0 end) as April,
sum(case when month(entrydate) = 5 and year(entrydate) = 2014 then totalpaymenttotal else 0 end) as May,
sum(case when month(entrydate) = 6 and year(entrydate) = 2014 then totalpaymenttotal else 0 end) as June,
sum(case when month(entrydate) = 7 and year(entrydate) = 2014 then totalpaymenttotal else 0 end) as July,
sum(case when month(entrydate) = 8 and year(entrydate) = 2014 then totalpaymenttotal else 0 end) as August,
sum(case when month(entrydate) = 9 and year(entrydate) = 2014 then totalpaymenttotal else 0 end) as September,
sum(case when month(entrydate) = 10 and year(entrydate) = 2014 then totalpaymenttotal else 0 end) as October,
sum(case when month(entrydate) = 11 and year(entrydate) = 2014 then totalpaymenttotal else 0 end) as November,
sum(case when month(entrydate) = 12 and year(entrydate) = 2014 then totalpaymenttotal else 0 end) as December,
sum(case when month(entrydate) = 1 and year(entrydate) = 2015 then totalpaymenttotal else 0 end) as [January 15],
sum(case when month(entrydate) = 2 and year(entrydate) = 2015 then totalpaymenttotal else 0 end) as [February 15],
sum(case when month(entrydate) = 3 and year(entrydate) = 2015 then totalpaymenttotal else 0 end) as [March 15],
sum(case when month(entrydate) = 4 and year(entrydate) = 2015 then totalpaymenttotal else 0 end) as [April 15]
from Sheet$
group by servicecounty, program
UNION ALL
select
servicecounty AS County,
program,
COUNT(case when month(entrydate) = 1 and year(entrydate) = 2014 then ApplicationID else 0 end) as January,
count(case when month(entrydate) = 2 and year(entrydate) = 2014 then ApplicationID else 0 end) as February,
count(case when month(entrydate) = 3 and year(entrydate) = 2014 then ApplicationID else 0 end) as March,
count(case when month(entrydate) = 4 and year(entrydate) = 2014 then ApplicationID else 0 end) as April,
count(case when month(entrydate) = 5 and year(entrydate) = 2014 then ApplicationID else 0 end) as May,
count(case when month(entrydate) = 6 and year(entrydate) = 2014 then ApplicationID else 0 end) as June,
count(case when month(entrydate) = 7 and year(entrydate) = 2014 then ApplicationID else 0 end) as July,
count(case when month(entrydate) = 8 and year(entrydate) = 2014 then ApplicationID else 0 end) as August,
count(case when month(entrydate) = 9 and year(entrydate) = 2014 then ApplicationID else 0 end) as September,
count(case when month(entrydate) = 10 and year(entrydate) = 2014 then ApplicationID else 0 end) as October,
count(case when month(entrydate) = 11 and year(entrydate) = 2014 then ApplicationID else 0 end) as November,
count(case when month(entrydate) = 12 and year(entrydate) = 2014 then ApplicationID else 0 end) as December,
Count(case when month(entrydate) = 1 and year(entrydate) = 2015 then ApplicationID else 0 end) as [January 15],
Count(case when month(entrydate) = 2 and year(entrydate) = 2015 then ApplicationID else 0 end) as [February 15],
Count(case when month(entrydate) = 3 and year(entrydate) = 2015 then ApplicationID else 0 end) as [March 15],
Count(case when month(entrydate) = 4 and year(entrydate) = 2015 then ApplicationID else 0 end) as [April 15]
from Sheet$
group by servicecounty, program
ORDER BY program
Here is what the report should look like:
Expenditures by month, program & county:
http://i.stack.imgur.com/yJ26A.jpg
Number of Apps by month, Program & county:
http://i.stack.imgur.com/7Aqkk.png
The table contains the following fields: ServiceCounty, TotalPaymentTotal, Program, ApplicationID, EntryDate
Thanks in advance for the help.
Your counts in the second subquery are all returning the same value. Remember, count() counts the number of non-NULL values. And, 0 is not null.
Three solutions:
Change the count() to sum() and the then to then 1.
Remove the else.
Change the else 0 to else NULL.
These are in order of my personal preference.

mysql compute no of leaves thru months

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

How can I GROUP BY more than one row/column?

I'm looking for a way to group more than one column in mysql.
The table is pretty much:
Customer, Product, Date
Joe, Apple, 2011-01-01
Henry, Banana, 2011-05-26
Sally, Peach, 2011-06-02
Jane, Strawberry, 2010-06-25
What I wanna do is count the NUMBER of Produkts each customer bought and group that by the month of the year.
So it would look like.
COUNT(Product) | January | February | March ....... | Total
If I just use GROUP BY product, MONTH(date) I get
Banana, January, 2
Banana, February, 3
Banana, March, 1
and so on.
Is there a way to do this, so that I get as many rows as I have distinct products and then a column for each month?
I'm really trying NOT to do this lateron in PHP because it get's incredibly slow.
Thanks a lot, already!
SELECT Product,
SUM(CASE WHEN MONTH(date) = 1 THEN 1 ELSE 0 END) Jan,
SUM(CASE WHEN MONTH(date) = 2 THEN 1 ELSE 0 END) Feb,
SUM(CASE WHEN MONTH(date) = 3 THEN 1 ELSE 0 END) Mar,
SUM(CASE WHEN MONTH(date) = 4 THEN 1 ELSE 0 END) Apr,
SUM(CASE WHEN MONTH(date) = 5 THEN 1 ELSE 0 END) May,
SUM(CASE WHEN MONTH(date) = 6 THEN 1 ELSE 0 END) Jun,
SUM(CASE WHEN MONTH(date) = 7 THEN 1 ELSE 0 END) Jul,
SUM(CASE WHEN MONTH(date) = 8 THEN 1 ELSE 0 END) Aug,
SUM(CASE WHEN MONTH(date) = 9 THEN 1 ELSE 0 END) Sep,
SUM(CASE WHEN MONTH(date) = 10 THEN 1 ELSE 0 END) Oct,
SUM(CASE WHEN MONTH(date) = 11 THEN 1 ELSE 0 END) Nov,
SUM(CASE WHEN MONTH(date) = 12 THEN 1 ELSE 0 END) Dec,
COUNT(date) Total
FROM
MyTable
GROUP BY
product