mysql compute no of leaves thru months - mysql

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

Related

SUM Data for every month in a year

I have table like this
IdExpense Description TotalExpense Dt_Expense
1 A 500 2012/03/12
I want calculate ever data in spesific month
ex:
Januari February
1500 500
I tried this Query and always error :
SELECT
SUM(TotalExpense CASE WHEN MONTH(Dt_Expense) = 1 THEN 1) AS 'January',
SUM(TotalExpense CASE WHEN MONTH(Dt_Expense) = 2 THEN 2) AS 'February',
SUM(TotalExpense CASE WHEN MONTH(Dt_Expense) = 3 THEN 3) AS 'March',
SUM(TotalExpense CASE WHEN MONTH(Dt_Expense) = 4 THEN 4) AS 'April',
SUM(TotalExpense CASE WHEN MONTH(Dt_Expense) = 5 THEN 5) AS 'May',
SUM(TotalExpense CASE WHEN MONTH(Dt_Expense) = 6 THEN 6) AS 'June',
SUM(TotalExpense CASE WHEN MONTH(Dt_Expense) = 7 THEN 7) AS 'July',
SUM(TotalExpense CASE WHEN MONTH(Dt_Expense) = 8 THEN 8) AS 'August',
SUM(TotalExpense CASE WHEN MONTH(Dt_Expense) = 9 THEN 9) AS 'September',
SUM(TotalExpense CASE WHEN MONTH(Dt_Expense) = 10 THEN 10) AS 'October',
SUM(TotalExpense CASE WHEN MONTH(Dt_Expense) = 11 THEN 11) AS 'November',
SUM(TotalExpense CASE WHEN MONTH(Dt_Expense) = 12 THEN 12) AS 'December'
FROM
expense
WHERE
Dt_Expense BETWEEN '2017/01/01' AND '2017/12/31'
Thank You
You could change to this
SELECT
SUM(CASE WHEN MONTH(Dt_Expense) = 1 THEN TotalExpense ELSE 0 END) AS January,
SUM(CASE WHEN MONTH(Dt_Expense) = 2 THEN TotalExpense ELSE 0 END) AS February,
SUM(CASE WHEN MONTH(Dt_Expense) = 3 THEN TotalExpense ELSE 0 END) AS March,
SUM(CASE WHEN MONTH(Dt_Expense) = 4 THEN TotalExpense ELSE 0 END) AS April,
SUM(CASE WHEN MONTH(Dt_Expense) = 5 THEN TotalExpense ELSE 0 END) AS May,
SUM(CASE WHEN MONTH(Dt_Expense) = 6 THEN TotalExpense ELSE 0 END) AS June,
SUM(CASE WHEN MONTH(Dt_Expense) = 7 THEN TotalExpense ELSE 0 END) AS July,
SUM(CASE WHEN MONTH(Dt_Expense) = 8 THEN TotalExpense ELSE 0 END) AS August,
SUM(CASE WHEN MONTH(Dt_Expense) = 9 THEN TotalExpense ELSE 0 END) AS September,
SUM(CASE WHEN MONTH(Dt_Expense) = 10 THEN TotalExpense ELSE 0 END) AS October,
SUM(CASE WHEN MONTH(Dt_Expense) = 11 THEN TotalExpense ELSE 0 END) AS November,
SUM(CASE WHEN MONTH(Dt_Expense) = 12 THEN TotalExpense ELSE 0 END) AS December
FROM
expense
WHERE
Dt_Expense BETWEEN '2017/01/01' AND '2017/12/31';

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.

how to reuse the resultset in mysql query

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

Use date range in where clause as column names

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

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