count new users by each months in sql - mysql

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.

Related

Mysql - Payments table by month column

I'm struggling - I have a table payments
Date
Ref
1.1.22
1
1.2.22
2
1.3.22
1
1.4.22
3
1.3.22
2
1.2.22
3
and a table of ref
Name
Ref
jo
1
Steve
2
Chris
3
I'm trying to get my sql to state
Name
Jan
Feb
Mar
Apr
Jo
Paid
Not
Paid
Not
Chris
Not
Paid
Not
Paid
Etc, so far I can but each name has one line for each month, when I want them in one row
`SELECT m.memID,m.Pay_Ref, p.ref,
(case when MONTHNAME(p.DATE) = 'January' then 'Paid' else 'Not' end) as January,
(case when MONTHNAME(p.DATE) = 'February' then 'Paid' else 'Not' end) as February,
(case when MONTHNAME(p.DATE) = 'March' then 'Paid' else 'Not' end) as March,
(case when MONTHNAME(p.DATE) = 'April' then 'Paid' else 'Not' end) as April,
(case when MONTHNAME(p.DATE) = 'May' then 'Paid' else 'Not' end) as May,
(case when MONTHNAME(p.DATE) = 'June' then 'Paid' else 'Not' end) as June,
(case when MONTHNAME(p.DATE) = 'July' then 'Paid' else 'Not' end) as July,
(case when MONTHNAME(p.DATE) = 'August' then 'Paid' else 'Not' end) as August,
(case when MONTHNAME(p.DATE) = 'September' then 'Paid' else 'Not' end) as September,
(case when MONTHNAME(p.DATE) = 'October' then 'Paid' else 'Not' end) as October,
(case when MONTHNAME(p.DATE) = 'November' then 'Paid' else 'Not' end) as November,
(case when MONTHNAME(p.DATE) = 'December' then 'Paid' else 'Not' end) as December
FROM Members AS m
INNER JOIN AllPayments AS p ON m.Pay_Ref = p.ref`

Get Data for Grouping all Months in a year using MySql and DB2

below is my query
SELECT DISTINCT e2.status as status, e2.year as year, e2.month as month,e2.Jan,e2.Feb,e2.Mar,e2.Apr,e2.May,e2.Jun,e2.Jul,e2.Aug,e2.Sep,e2.Oct,e2.Nov, e2.Dece, e2.countTotal FROM tranx_history e1 INNER JOIN (
SELECT YEAR(e.create_dt) AS year, MONTH(e.create_dt) AS month,
SUM(CASE WHEN MONTH(e.create_dt) = 01 THEN 1 ELSE 0 END) AS Jan,
SUM(CASE WHEN MONTH(e.create_dt) = 02 THEN 1 ELSE 0 END) AS Feb,
SUM(CASE WHEN MONTH(e.create_dt) = 03 THEN 1 ELSE 0 END) AS Mar,
SUM(CASE WHEN MONTH(e.create_dt) = 04 THEN 1 ELSE 0 END) AS Apr,
SUM(CASE WHEN MONTH(e.create_dt) = 05 THEN 1 ELSE 0 END) AS May,
SUM(CASE WHEN MONTH(e.create_dt) = 06 THEN 1 ELSE 0 END) AS Jun,
SUM(CASE WHEN MONTH(e.create_dt) = 07 THEN 1 ELSE 0 END) AS Jul,
SUM(CASE WHEN MONTH(e.create_dt) = 08 THEN 1 ELSE 0 END) AS Aug,
SUM(CASE WHEN MONTH(e.create_dt) = 09 THEN 1 ELSE 0 END) AS Sep,
SUM(CASE WHEN MONTH(e.create_dt) = 10 THEN 1 ELSE 0 END) AS Oct,
SUM(CASE WHEN MONTH(e.create_dt) = 11 THEN 1 ELSE 0 END) AS Nov,
SUM(CASE WHEN MONTH(e.create_dt) = 12 THEN 1 ELSE 0 END) AS Dece,
SUM(CASE WHEN MONTH(e.create_dt) >= 01 AND MONTH(e.create_dt) <= 12 THEN 1 ELSE 0 END) as countTotal,
e.trnx_status as status
from tranx_history e
GROUP BY e.trnx_status, MONTH(e.create_dt),YEAR(e.create_dt) ) e2 ON YEAR(e1.create_dt) = e2.year AND MONTH(e1.create_dt) = e2.month ORDER BY e2.year asc, e2.month asc
For the Above query, The Output we are getting is first it is grouping by Jan then in the next row it is grouping by Feb even though status and year are same
i want the output Jan and Feb with same status and year in a single row not in different rows

sql query to get month wise count

below is my data present in my database.
startingdate - varchar
tablename - couponentry
coupon price month startingdate
100 15 1-month 02-03-2015
101 15 1-month 04-03-2015
102 15 1-month 05-03-2015
103 15 1-month 07-04-2015
104 15 1-month 08-04-2015
105 15 1-month 15-05-2015
106 15 1-month 18-05-2015
107 15 2-month 02-02-2015
108 15 2-month 04-02-2015
109 15 2-month 05-02-2015
110 15 2-month 07-03-2015
111 15 2-month 08-03-2015
112 15 2-month 15-05-2015
113 15 2-month 18-05-2015
114 15 2-month 18-05-2015
I need count of total coupon based on 1-month,2-month and so on..and also need to display month wise seprate totalcount of coupon.
FOR EX - In above data - total 7 coupon created in 1-month.
from this 3 coupon comes in march,2 in april and 2 in may month....
like wise...
expected output like below
month jan feb mar april may june ... totalcoupon totalprice
1-month 3 2 2 7 105
2-month 3 2 3 8 120
below is my query which i tried...
SELECT month,COUNT(CE.coupon) As Total,SUM(CE.Price) AS TotalPrice FROM coupon_entry CE LEFT JOIN subagentmaster SAM ON CE.subagentid = SAM.id LEFT JOIN tehsilmaster TM ON SAM.tehsil = TM.id LEFT JOIN city_master CM ON CE.city = CM.id WHERE CM.cityname = 'nagpur' GROUP BY CE.month
but this is getting below output
month totalcoupon totalprice
1-month 7 105
2-month 8 120
Below Query will help you :
SELECT
MONTH ,
SUM(IF(MONTH(STR_TO_DATE(startingdate,'%d-%m-%Y')) = 1, 1, 0)) AS Jan,
SUM(IF(MONTH(STR_TO_DATE(startingdate,'%d-%m-%Y')) = 2, 1, 0)) AS Feb,
SUM(IF(MONTH(STR_TO_DATE(startingdate,'%d-%m-%Y')) = 3, 1, 0)) AS Mar,
SUM(IF(MONTH(STR_TO_DATE(startingdate,'%d-%m-%Y')) = 4, 1, 0)) AS Apr,
SUM(IF(MONTH(STR_TO_DATE(startingdate,'%d-%m-%Y')) = 5, 1, 0)) AS May,
SUM(IF(MONTH(STR_TO_DATE(startingdate,'%d-%m-%Y')) = 6, 1, 0)) AS Jun,
SUM(IF(MONTH(STR_TO_DATE(startingdate,'%d-%m-%Y')) = 7, 1, 0)) AS Jul,
SUM(IF(MONTH(STR_TO_DATE(startingdate,'%d-%m-%Y')) = 8, 1, 0)) AS Aug,
SUM(IF(MONTH(STR_TO_DATE(startingdate,'%d-%m-%Y')) = 9, 1, 0)) AS Sep,
SUM(IF(MONTH(STR_TO_DATE(startingdate,'%d-%m-%Y')) = 10, 1, 0)) AS OCT,
SUM(IF(MONTH(STR_TO_DATE(startingdate,'%d-%m-%Y')) = 11, 1, 0)) AS Nov,
SUM(IF(MONTH(STR_TO_DATE(startingdate,'%d-%m-%Y')) = 12, 1, 0)) AS `Dec`
,COUNT(*) totalCoupon
,SUM(price) totalprice
FROM mytable
GROUP BY MONTH ;
Ignoring the whole nagpur bit...
SELECT month
, SUM(CASE WHEN startingdate >= '2015-01-01' AND startingdate < '2015-02-01' THEN 1 ELSE 0 END) 'jan'
, SUM(CASE WHEN startingdate >= '2015-02-01' AND startingdate < '2015-03-01' THEN 1 ELSE 0 END) 'feb'
, SUM(CASE WHEN startingdate >= '2015-03-01' AND startingdate < '2015-04-01' THEN 1 ELSE 0 END) 'mar'
, SUM(CASE WHEN startingdate >= '2015-04-01' AND startingdate < '2015-05-01' THEN 1 ELSE 0 END) 'apr'
, SUM(CASE WHEN startingdate >= '2015-05-01' AND startingdate < '2015-06-01' THEN 1 ELSE 0 END) 'may'
, SUM(CASE WHEN startingdate >= '2015-06-01' AND startingdate < '2015-07-01' THEN 1 ELSE 0 END) 'jun'
, SUM(CASE WHEN startingdate >= '2015-07-01' AND startingdate < '2015-08-01' THEN 1 ELSE 0 END) 'jul'
, SUM(CASE WHEN startingdate >= '2015-08-01' AND startingdate < '2015-09-01' THEN 1 ELSE 0 END) 'aug'
, SUM(CASE WHEN startingdate >= '2015-09-01' AND startingdate < '2015-10-01' THEN 1 ELSE 0 END) 'sep'
, SUM(CASE WHEN startingdate >= '2015-10-01' AND startingdate < '2015-11-01' THEN 1 ELSE 0 END) 'oct'
, SUM(CASE WHEN startingdate >= '2015-11-01' AND startingdate < '2015-12-01' THEN 1 ELSE 0 END) 'nov'
, SUM(CASE WHEN startingdate >= '2015-12-01' AND startingdate < '2016-01-01' THEN 1 ELSE 0 END) 'dec'
, COUNT(*) totalcoupon
, SUM(price) totalprice
FROM couponentry
GROUP
BY month;
However, as you mention xampp - it would be simpler (and more scalable) to do this in PHP.

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;

SQL - Make a custom table with month based columns and year based rows

I am trying to make an awesome custom table which counts the amount of rows and organises them so that if there are three rows with a date in January 2013, four in March 2014 and five in October 2014 the table would show up as:
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
2013 3 0 0 0 0 0 0 0 0 0 0 0
2014 0 0 4 0 0 0 0 0 0 5 0 0
I would recommend using a view, rather than a new table, this way when your underlying data changes your new table won't be out of sync.
Since you have not given much sample data I have had to assume a structure, but you would want something like this:
CREATE TABLE T (`Date` DATETIME);
INSERT T (`Date`)
VALUES
('2013-01-01'), ('2013-01-02'), ('2013-01-03'),
('2014-03-01'), ('2014-03-02'), ('2014-03-03'),
('2014-10-01'), ('2014-10-01'), ('2014-10-01'),
('2014-10-01'), ('2014-10-01');
CREATE VIEW V
AS
SELECT YEAR(`Date`) AS `Year`,
COUNT(CASE WHEN MONTH(`Date`) = 1 THEN 1 END) AS `Jan`,
COUNT(CASE WHEN MONTH(`Date`) = 2 THEN 1 END) AS `Feb`,
COUNT(CASE WHEN MONTH(`Date`) = 3 THEN 1 END) AS `Mar`,
COUNT(CASE WHEN MONTH(`Date`) = 4 THEN 1 END) AS `Apr`,
COUNT(CASE WHEN MONTH(`Date`) = 5 THEN 1 END) AS `May`,
COUNT(CASE WHEN MONTH(`Date`) = 6 THEN 1 END) AS `Jun`,
COUNT(CASE WHEN MONTH(`Date`) = 7 THEN 1 END) AS `Jul`,
COUNT(CASE WHEN MONTH(`Date`) = 8 THEN 1 END) AS `Aug`,
COUNT(CASE WHEN MONTH(`Date`) = 9 THEN 1 END) AS `Sep`,
COUNT(CASE WHEN MONTH(`Date`) = 10 THEN 1 END) AS `Oct`,
COUNT(CASE WHEN MONTH(`Date`) = 11 THEN 1 END) AS `Nov`,
COUNT(CASE WHEN MONTH(`Date`) = 12 THEN 1 END) AS `Dec`
FROM T
GROUP BY YEAR(`Date`);
Example on SQL Fiddle