Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I'm running the following query:
select
year(date) year_date,
sum(case when month(date) = 1 then price end) January,
sum(case when month(date) = 2 then price end) February,
sum(case when month(date) = 3 then price end) March,
sum(case when month(date) = 3 then price end) May,
sum(case when month(date) = 3 then price end) April,
sum(case when month(date) = 3 then price end) June,
sum(case when month(date) = 3 then price end) July,
sum(case when month(date) = 3 then price end) August,
sum(case when month(date) = 3 then price end) September,
sum(case when month(date) = 3 then price end) October,
sum(case when month(date) = 3 then price end) November,
sum(case when month(date) = 3 then price end) December,
from orders
group by year(date)
order by year_date
I have another query to get a monthly sum of expenses:
SELECT SUM(expense) AS expenses_total FROM expenses
Any way to add this to the first query so as the end result of each month is minus the expenses?
just join those 2 tables?
select
year(dt) year_date,
sum(case when month(dt) = 1 then price end) - e.exp January,
sum(case when month(dt) = 2 then price end) - e.exp February,
sum(case when month(dt) = 3 then price end) - e.exp March,
sum(case when month(dt) = 4 then price end) - e.exp May,
sum(case when month(dt) = 5 then price end) - e.exp April,
sum(case when month(dt) = 6 then price end) - e.exp June,
sum(case when month(dt) = 7 then price end) - e.exp July,
sum(case when month(dt) = 8 then price end) - e.exp August,
sum(case when month(dt) = 9 then price end) - e.exp September,
sum(case when month(dt) = 10 then price end) - e.exp October,
sum(case when month(dt) = 11 then price end) - e.exp November,
sum(case when month(dt) = 12 then price end) - e.exp December,
e.exp
from orders o
JOIN (select SUM(expense) AS exp from expenses) e ON 1=1
group by year(dt), exp
order by year_date
value of orders table
result
fiddle
Related
I fetched the following data:
MONTH | TOTAL
-------------------------
Jan | 100
Feb | 200
Mar | 300
Using this query:
$query = "SELECT DATE_FORMAT(date,'%b') AS MONTH, SUM(col1+col2) AS TOTAL FROM myTable GROUP BY YEAR(date),MONTH(date)";
How can I edit the above query or re-write to get the following result:
JAN | FEB | MAR
-------------------------
100 | 200 | 300
I have gone through almost all the the other similar posts. However, sql transposing, to me, is very confusing. Any input is much appreciated!
You can use conditional aggregation. The following will work in either SQL Server or MySQL:
select year(date),
sum(case when month(date) = 1 then col1 + col2 else 0 end) as jan,
sum(case when month(date) = 2 then col1 + col2 else 0 end) as feb,
sum(case when month(date) = 3 then col1 + col2 else 0 end) as mar
from mytable
group by year(date)
order by year(date);
EDIT (regarding the comment):
select year(date),
sum(case when month(date) = 1 then val else 0 end) as jan,
sum(case when month(date) = 2 then val else 0 end) as feb,
sum(case when month(date) = 3 then val else 0 end) as mar
from (select t.*, (col1 + col2) as val
from mytable
) t
group by year(date)
order by year(date);
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.
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 am new to mysql.
I have on survey with clicks, period(date). Now i have to find out number of clicks per month, like:
MON CLICKS
nov 0
oct 34
sep 67
aug 89
I have used code like this:
select MONTHNAME(period) mon, IFNULL(count(id),0) as Clicks
from survey
where period > DATE_SUB(now(), INTERVAL 3 MONTH)
group by EXTRACT(MONTH FROM period)
It is not working for with no records.
Here one thing I suppose there is no record in that month it should show 0: if there is no record in nov the number of clicks should be 0.
my table structure was like this
CREATE TABLE `survey` (
`id` int(2) NOT NULL auto_increment,
`period` datetime default NULL)
for last four weeks i have used
SELECT uq.timespan, COALESCE(tsq.TotalClicks, 0) as Clicks FROM (
SELECT '22-28 days' as timespan
union SELECT '15-21 days'
union SELECT '8-14 days'
union SELECT 'up to 7 days'
)uq LEFT JOIN (
SELECT CASE
WHEN submitdate >= NOW() - INTERVAL 4 WEEK
AND submitdate < NOW() - INTERVAL 3 WEEK THEN '22-28 days'
WHEN submitdate >= NOW() - INTERVAL 3 WEEK
AND submitdate < NOW() - INTERVAL 2 WEEK THEN '15-21 days'
WHEN submitdate >= NOW() - INTERVAL 2 WEEK
AND submitdate < NOW() - INTERVAL 1 WEEK THEN '8-14 days'
WHEN submitdate >= NOW() - INTERVAL 1 WEEK THEN 'up to 7 days'
END Weeksubmitdate,
count(id) TotalClicks
FROM survey
WHERE submitdate >= NOW() - INTERVAL 4 WEEK
GROUP BY Weeksubmitdate
)tsq ON uq.timespan = tsq.Weeksubmitdate
Any help?
I usually do Pivot table to achieve this. Assuming your click information is stored into a table named SURVEY and assuming only the date/time of the click is stored into one column of the SURVEY table (which is all what you need) then here is one way to do it:
select year(period),
sum(case when month(period)=1 then 1 else 0 end) jan,
sum(case when month(period)=2 then 1 else 0 end) feb,
sum(case when month(period)=3 then 1 else 0 end) mar,
sum(case when month(period)=4 then 1 else 0 end) apr,
sum(case when month(period)=5 then 1 else 0 end) may,
sum(case when month(period)=6 then 1 else 0 end) jun,
sum(case when month(period)=7 then 1 else 0 end) jul,
sum(case when month(period)=8 then 1 else 0 end) aug,
sum(case when month(period)=9 then 1 else 0 end) sep,
sum(case when month(period)=10 then 1 else 0 end) oct,
sum(case when month(period)=11 then 1 else 0 end) nov,
sum(case when month(period)=11 then 1 else 0 end) dec
from survey
group by year(period)
The output is something like:
---------------------------------------------------------------------------------
| Year | JAN | FEB | MAR | APR | MAY | JUN | JUL | AUG | SEP | OCT | NOV | DEC |
---------------------------------------------------------------------------------
| 2012 | 5 | 20 | 13 | 0 | 0 | 65 | 15 | 0 | 0 | 21 | 0 | 0 |
---------------------------------------------------------------------------------
I even set up the same Fiddle SQL for you
SQL Fiddle Demo
An alternative way (Column based for the last 4 months even with ZERO count):
SQL Fiddle Demo
SELECT mon,
sum(clicks) clicks
FROM ( SELECT month(period) mnth,
date_format(period,'%b') mon,
count(1) clicks
FROM survey
WHERE month(period) BETWEEN month(curdate()) - 4 AND month(curdate())
GROUP BY 1, 2
UNION ALL
SELECT 1 mnth, 'Jan' mon, 0 clicks
UNION ALL
SELECT 2 mnth, 'Feb' mon, 0 clicks
UNION ALL
SELECT 3 mnth, 'Mar' mon, 0 clicks
UNION ALL
SELECT 4 mnth, 'Apr' mon, 0 clicks
UNION ALL
SELECT 5 mnth, 'May' mon, 0 clicks
UNION ALL
SELECT 6 mnth, 'Jun' mon, 0 clicks
UNION ALL
SELECT 7 mnth, 'Jul' mon, 0 clicks
UNION ALL
SELECT 8 mnth, 'Aug' mon, 0 clicks
UNION ALL
SELECT 9 mnth, 'Sep' mon, 0 clicks
UNION ALL
SELECT 10 mnth, 'Oct' mon, 0 clicks
UNION ALL
SELECT 11 mnth, 'Nov' mon, 0 clicks
UNION ALL
SELECT 12 mnth, 'Dec' mon, 0 clicks) a
WHERE mnth BETWEEN month(curdate()) - 4 AND month(curdate())
GROUP BY 1
ORDER BY mnth
You need to join to a table that contains all month names. Here's one way to do it:
select
mon,
ifnull(count(id), 0) as Clicks
from (select 'nov' as mon union select 'oct' union select 'sep' union select 'aug') m
left join survey on MONTHNAME(period) = mon
where submitdate > DATE_SUB(now(), INTERVAL 3 MONTH)
group by 1
select MONTHNAME(STR_TO_DATE(month(period), '%m'))as 'month',count(*) as clicks
from survey group by month(period)
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