How to count each month without repeating the lines? - mysql

I'm trying to create all months in a query instead of use several lines.
Here is the information:
CREATE TABLE quotes (
id INT,
created_at DATE,
num_policy INT);
INSERT INTO quotes VALUES
( 1, '2014-01-01',1234),
( 2, '2014-02-06',5678),
( 3, '2013-04-24',9123),
( 4, '2013-05-24',4567),
( 5, '2014-06-20',8912);
I tried this query: http://sqlfiddle.com/#!2/2f4f51/5
SET #year := 2014;
SELECT count(*) AS jan FROM `quotes` WHERE (created_at BETWEEN CONCAT( #year, "-01-01") AND CONCAT( #year, "-01-31")) ;
SELECT count(*) AS feb FROM `quotes` WHERE (created_at BETWEEN CONCAT( #year, "-02-01") AND CONCAT( #year, "-02-29")) ;
......
SELECT count(*) AS dem FROM `quotes` WHERE (created_at BETWEEN CONCAT( #year, "-12-01") AND CONCAT( #year, "-12-31")) ;
I'm trying to show all months in one line
|jan| |feb| |mar| |apr| ......
1 1 0 0
Please somebody can help me?
I will appreciate all kind of help.

This could do the magic:
SELECT CASE WHEN MonthSET #year := 2014;
SELECT SUM(CASE WHEN Month(created_at) = 1 THEN 1 ELSE 0 END) AS jan,
SUM(CASE WHEN Month(created_at) = 2 THEN 1 ELSE 0 END) AS feb,
SUM(CASE WHEN Month(created_at) = 3 THEN 1 ELSE 0 END) AS mar,
SUM(CASE WHEN Month(created_at) = 4 THEN 1 ELSE 0 END) AS apr,
SUM(CASE WHEN Month(created_at) = 5 THEN 1 ELSE 0 END) AS may,
SUM(CASE WHEN Month(created_at) = 6 THEN 1 ELSE 0 END) AS jun,
SUM(CASE WHEN Month(created_at) = 7 THEN 1 ELSE 0 END) AS jul,
SUM(CASE WHEN Month(created_at) = 8 THEN 1 ELSE 0 END) AS aug,
SUM(CASE WHEN Month(created_at) = 9 THEN 1 ELSE 0 END) AS sep,
SUM(CASE WHEN Month(created_at) = 10 THEN 1 ELSE 0 END) AS oct,
SUM(CASE WHEN Month(created_at) = 11 THEN 1 ELSE 0 END) AS nov,
SUM(CASE WHEN Month(created_at) = 12 THEN 1 ELSE 0 END) AS `dec`
FROM `quotes`
WHERE YEAR(created_at) = #year
Link to a SQL Fiddle:
http://sqlfiddle.com/#!2/2f4f51/13/1

How about using group by?
SELECT month(created_at), count(*) AS cnt
FROM `quotes`
WHERE (created_at BETWEEN CONCAT( #year, "-01-01") AND CONCAT( #year, "-01-31"))
GROUP BY month(created_at)
ORDER BY 1;
This does produce the values on separate lines. To get them on one line, use conditional aggregation:
SELECT SUM(month(created_at) = 1) as jan,
SUM(month(created_at) = 2) as feb,
. . .
FROM quotes
;WHERE (created_at BETWEEN CONCAT( #year, '-01-01') AND CONCAT( #year, '-01-31'));

Related

Mysql calculate multiple SUM() varialbe

I want to calculate 2 sum() i have tried query as below it works but very slowly. Any ideas to make it better?
SELECT customer,
SUM(CASE WHEN book_day BETWEEN '2020-01-01' AND '2020-01-31'
THEN pax+free
ELSE 0
END) AS January,
SUM(CASE WHEN book_day BETWEEN '2020-02-01' AND '2020-02-31'
THEN pax+free
ELSE 0
END) AS February,
( SUM(CASE WHEN book_day BETWEEN '2020-01-01' AND '2020-01-31'
THEN pax+free
ELSE 0
END) +
SUM(CASE WHEN book_day BETWEEN '2020-02-01' AND '2020-02-31'
THEN pax+free
ELSE 0
END) ) AS total
FROM rezervations
How can i make simplier like January + February as total
keep BETWEEN '2020-01-01' AND '2020-02-29' in where clause.
make sure there is index on book_day column
SELECT customer,
SUM(CASE WHEN book_day BETWEEN '2020-01-01' AND '2020-01-31' THEN pax+free ELSE 0 END) as January,
SUM(CASE WHEN book_day BETWEEN '2020-02-01' AND '2020-02-29' THEN pax+free ELSE 0 END) as February,
(SUM(CASE WHEN book_day BETWEEN '2020-01-01' AND '2020-02-29' THEN pax+free ELSE 0 END) ) as total
FROM rezervations
WHERE
book_day BETWEEN '2020-01-01' AND '2020-02-29'

count new users by each months in sql

i am trying to count new users recorded by each month of this year.
like
Need data month wise, which is registerd by month new users
JAn Feb Mar April May ..... Dec
1 2 4 2 5 ..... 1
through created_at date and user Id.
here is user_table
id created_at
1 2020-01-09 22:38:55
2 2020-02-09 22:38:55
3 2020-02-09 22:38:55
4 2020-03-09 22:38:55
5 2020-03-09 22:38:55
6 2020-03-09 22:38:55
7 2020-04-09 22:38:55
8 2020-04-09 22:38:55
9 2020-05-09 22:38:55
i am trying with this query
SELECT ut.id, Month(FROM_UNIXTIME(ut.created_at)), Count(*)
from $userTable ut
where FROM_UNIXTIME(ut.created_at) >= CURDATE() - INTERVAL 1 YEAR
GROUP BY Month(FROM_UNIXTIME(ut.created_at));
You can group by year and sum by month like this:
select YEAR(created_at) as year,
sum(case when Month(created_at) = 1 then 1 else 0 end) AS Jan,
sum(case when Month(created_at) = 2 then 1 else 0 end) AS Feb,
sum(case when Month(created_at) = 3 then 1 else 0 end) AS Mar,
sum(case when Month(created_at) = 4 then 1 else 0 end) AS Apr,
sum(case when Month(created_at) = 5 then 1 else 0 end) AS May,
sum(case when Month(created_at) = 6 then 1 else 0 end) AS Jun,
sum(case when Month(created_at) = 7 then 1 else 0 end) AS Jul,
sum(case when Month(created_at) = 8 then 1 else 0 end) AS Aug,
sum(case when Month(created_at) = 9 then 1 else 0 end) AS Sep,
sum(case when Month(created_at) = 10 then 1 else 0 end) AS Oct,
sum(case when Month(created_at) = 11 then 1 else 0 end) AS Nov,
sum(case when Month(created_at) = 12 then 1 else 0 end) AS Dec from ut group by YEAR(created_at)
all columns that being used in the select that are not an aggregated function need to be grouped, ut.id is not group and in your case should be removed from the select
Try
SELECT Month(FROM_UNIXTIME(ut.created_at)), Count(*) from $userTable ut
where FROM_UNIXTIME(ut.created_at) >= CURDATE() - INTERVAL 1 YEAR
GROUP BY Month(FROM_UNIXTIME(ut.created_at));
If you want to mix data from this year and last year for the past 12 months, you can use:
SELECT SUM(MONTH(FROM_UNIXTIME(ut.created_at)) = 1) AS Jan,
SUM(MONTH(FROM_UNIXTIME(ut.created_at)) = 2) AS Feb,
SUM(MONTH(FROM_UNIXTIME(ut.created_at)) = 3) AS Mar,
SUM(MONTH(FROM_UNIXTIME(ut.created_at)) = 4) AS Apr,
SUM(MONTH(FROM_UNIXTIME(ut.created_at)) = 5) AS May,
SUM(MONTH(FROM_UNIXTIME(ut.created_at)) = 6) AS Jun,
SUM(MONTH(FROM_UNIXTIME(ut.created_at)) = 7) AS Jul,
SUM(MONTH(FROM_UNIXTIME(ut.created_at)) = 8) AS Aug,
SUM(MONTH(FROM_UNIXTIME(ut.created_at)) = 9) AS Sep,
SUM(MONTH(FROM_UNIXTIME(ut.created_at)) = 10) AS Oct,
SUM(MONTH(FROM_UNIXTIME(ut.created_at)) = 11) AS Nov,
SUM(MONTH(FROM_UNIXTIME(ut.created_at)) = 12) AS Dec
FROM ut
WHERE FROM_UNIXTIME(ut.created_at) >= CURDATE() - INTERVAL 1 YEAR
Note: This assumes that FROM_UNIXTIME() is a actually necessary. It also uses a convenient MySQL shortcut for conditional aggreation.

MySQL SUM Amount per month and group by caterory

I have table:
User nvarchar(30)
Date datetime
Total decimal(8,2)
And i need to get data per month like this:
I can get it this way (without month columns):
SELECT User, MONTHNAME(FROM_UNIXTIME(Date)) as month, SUM(Total) as total FROM `wp_banking_expenses` group by month,User
But i need it to make as on image above...
You can try to use condition aggregate function.
SELECT User,
SUM(CASE WHEN MONTHNAME(Date) = 'January' THEN Total END) as Jan,
SUM(CASE WHEN MONTHNAME(Date) = 'February' THEN Total END) as Feb,
SUM(CASE WHEN MONTHNAME(Date) = 'March' THEN Total END) as Mar,
SUM(CASE WHEN MONTHNAME(Date) = 'April' THEN Total END) as Apr,
SUM(CASE WHEN MONTHNAME(Date) = 'May' THEN Total END) as May,
SUM(CASE WHEN MONTHNAME(Date) = 'June' THEN Total END) as JUN,
SUM(CASE WHEN MONTHNAME(Date) = 'July' THEN Total END) as JUL,
SUM(CASE WHEN MONTHNAME(Date) = 'August' THEN Total END) as AUG,
SUM(CASE WHEN MONTHNAME(Date) = 'September' THEN Total END) as SEP,
SUM(CASE WHEN MONTHNAME(Date) = 'October' THEN Total END) as OCT,
SUM(CASE WHEN MONTHNAME(Date) = 'November' THEN Total END) as NOV,
SUM(CASE WHEN MONTHNAME(Date) = 'December' THEN Total END) as DEC
FROM `wp_banking_expenses`
group by User
use conditional aggregation using case when expression
select user,max(case when month='January' then total end) as Jan,
max(case when month='February' then total end) as Feb,
max(case when month='March' then total end) as Mar,
max(case when month='April' then total end) as Apr,
max(case when month='May' then total end) as May,
max(case when month='June' then total end) as June,
----
from
(
SELECT User, MONTHNAME(Date) as month, SUM(Total) as total
FROM `wp_banking_expenses`
group by month,User
)A group by user

MySQL select by month returns NULL for all months

Using MySQL 5.6 I am trying to select the total inspections for each month in the past 12 months with an output of 0 if there are none. I appear to be missing something here as the output is all NULL. The date_inspected field is just a regular SQL date
From what I understood, the structure should be [conditional statement, output variable, default value] but even the 0 gets ignored in favor of NULL. I am trying to understand what I am doing wrong here.
Table:
Column Type Null
id int(11) No
inspector_id int(11) Yes
company_id int(11) Yes
date_inspected date No
start_time datetime No
end_time datetime No
Query:
SELECT
SUM(IF(MONTH = 'Jan', total, 0)) AS 'Januari',
SUM(IF(MONTH = 'Feb', total, 0)) AS 'Februari',
SUM(IF(MONTH = 'Mar', total, 0)) AS 'Maart',
SUM(IF(MONTH = 'Apr', total, 0)) AS 'April',
SUM(IF(MONTH = 'May', total, 0)) AS 'Mei',
SUM(IF(MONTH = 'Jun', total, 0)) AS 'Juni',
SUM(IF(MONTH = 'Jul', total, 0)) AS 'Juli',
SUM(IF(MONTH = 'Aug', total, 0)) AS 'Augustus',
SUM(IF(MONTH = 'Sep', total, 0)) AS 'September',
SUM(IF(MONTH = 'Oct', total, 0)) AS 'Oktober',
SUM(IF(MONTH = 'Nov', total, 0)) AS 'November',
SUM(IF(MONTH = 'Dec', total, 0)) AS 'December',
SUM(total) AS all_months
FROM (
SELECT MONTH(date_inspected) AS MONTH, COUNT(*) AS total
FROM inspection
WHERE date_inspected BETWEEN NOW() AND Date_add(NOW(), interval - 12 month)
GROUP BY MONTH
) AS SubTable
Output
{ ["Januari"]=> NULL
["Februari"]=> NULL
["Maart"]=> NULL
["April"]=> NULL
["Mei"]=> NULL
["Juni"]=> NULL
["Juli"]=> NULL
["Augustus"]=> NULL
["September"]=> NULL
["Oktober"]=> NULL
["November"]=> NULL
["December"]=> NULL
["all_months"]=> NULL }
Update:
SOLUTION by Gordon Linoff
SELECT
SUM(CASE WHEN MONTH(date_inspected) = 1 THEN 1 ELSE 0 END) AS 'Januari',
SUM(CASE WHEN MONTH(date_inspected) = 2 THEN 1 ELSE 0 END) AS 'Februari',
SUM(CASE WHEN MONTH(date_inspected) = 3 THEN 1 ELSE 0 END) AS 'Maart',
SUM(CASE WHEN MONTH(date_inspected) = 4 THEN 1 ELSE 0 END) AS 'April',
SUM(CASE WHEN MONTH(date_inspected) = 5 THEN 1 ELSE 0 END) AS 'Mei',
SUM(CASE WHEN MONTH(date_inspected) = 6 THEN 1 ELSE 0 END) AS 'Juni',
SUM(CASE WHEN MONTH(date_inspected) = 7 THEN 1 ELSE 0 END) AS 'Juli',
SUM(CASE WHEN MONTH(date_inspected) = 8 THEN 1 ELSE 0 END) AS 'Augustus',
SUM(CASE WHEN MONTH(date_inspected) = 9 THEN 1 ELSE 0 END) AS 'September',
SUM(CASE WHEN MONTH(date_inspected) = 10 THEN 1 ELSE 0 END) AS 'Oktober',
SUM(CASE WHEN MONTH(date_inspected) = 11 THEN 1 ELSE 0 END) AS 'November',
SUM(CASE WHEN MONTH(date_inspected) = 12 THEN 1 ELSE 0 END) AS 'December'
FROM inspection
WHERE date_inspected BETWEEN Date_add(NOW(), interval - 12 month) AND NOW()
As I understand it, we've given the SUM() a conditional CASE statement that if the current record's date_inspected's MONTH is equal to the MySQL constant for that value, return true and add it to the total, else do nothing.
More on MySQL CASE
between must use the smaller value first and not the other way around. Change
BETWEEN NOW() AND Date_add(NOW(), interval - 12 month)
to
BETWEEN Date_add(NOW(), interval - 12 month) and NOW()
And month() returns a number of the month and not the name.
Juergen is correct on one problem in your query. Another is that MONTH() returns a number, not a string.
And, you can further simplify the query. A subquery is not needed:
SELECT SUM(CASE WHEN MONTH(date_inspected) = 1 THEN 1 ELSE 0 END)) AS 'Januari',
SUM(CASE WHEN MONTH(date_inspected) = 2 THEN 1 ELSE 0 END)) AS 'Februari',
. . .
FROM inspection
WHERE date_inspected BETWEEN Date_add(NOW(), interval - 12 month) AND NOW();

SQL Group by "Folder Name" by month and then total

My new query is giving me few errors:
Error: ORA-0093: SQL command not properly ended.
select coalesce(a.group_name, 'Total') as group_name,
sum(case when month (a.sent_date)=1 then a.total_sent else 0 end) as January,
sum(case when month(a.sent_date)=2 then a.total_sent else 0 end) as February,
sum(case when month(a.sent_date)=3 then a.total_sent else 0 end) as March,
sum(case when month(a.sent_date)=4 then a.total_sent else 0 end) as April,
sum(case when month(a.sent_date)=5 then a.total_sent else 0 end) as May,
sum(case when month(a.sent_date)=6 then a.total_sent else 0 end) as June,
sum(case when month(a.sent_date)=7 then a.total_sent else 0 end) as July,
sum(case when month(a.sent_date)=8 then a.total_sent else 0 end) as August,
sum(case when month(a.sent_date)=9 then a.total_sent else 0 end) as September,
sum(case when month(a.sent_date)=10 then a.total_sent else 0 end) as October,
sum(case when month(a.sent_date)=11 then a.total_sent else 0 end) as November,
sum(case when month(a.sent_date)=12 then a.total_sent else 0 end) as December
from c_group a
where a.partner_id=123 AND
a.sent_date >= '01-JAN-2012'
and a.sent_date <= '31-DEC-2012'
group by a.group_name with rollup;
=========
This is my first time posting here and also a beginner at queries.
I am running a query which returns various folder names for all days. I want to group by the folder name and do a sum of the totals for each folder name by months and then a total of each column at the bottom. This is the query I am running:
select a.group_name, a.sent_date, a.total_sent
from
c_group a
where
a.partner_id=123
and a.sent_date >= '01-JAN-2012'
and a.sent_date <= '31-DEC-2012'
Displays as follows:
GROUP_NAME SENT_DATE TOTAL_SENT
Group A 1-Jan-12 37
Group B 3-Jan-12 25
Group C 1-May-12 10
Group D 1-May-12 8
Group D 1-Jan-12 11
Group A 1-Dec-12 9
I need the results to display as:
January February March April May June July August September October November December
Group A
Group B
Group C
Group D
...
....
...
....
Total Sum of above Sum of above Sum of above Sum of above Sum of above Sum of above Sum of above Sum of above Sum of above Sum of above Sum of above Sum of above
You want to combine conditional aggregation with rollup:
select coalesce(a.group_name, 'Total') as group_name,
sum(case when month(a.sent_date) = 1 then a.total_sent else 0 end) as January,
sum(case when month(a.sent_date) = 2 then a.total_sent else 0 end) as February,
. . .
sum(case when month(a.sent_date) = 12 then a.total_sent else 0 end) as December
from c_group a
where a.partner_id = 123 AND
a.sent_date >= '01-JAN-2012' AND
a.sent_date <= '31-DEC-2012'
group by a.group_name with rollup;