sql query to get month wise count - mysql

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.

Related

How to Sum "case when" clause after left join without duplication

How do I get the SUM of an amount in a CASE WHEN clause?
Table real:
id
name
goal
year
10
ronaldo
5
2022
10
ronaldo
5
2022
11
messi
5
2022
11
messi
5
2022
10
ronaldo
10
2021
11
messi
10
2021
Table target:
id
name
goal
year
10
ronaldo
10
2022
11
messi
10
2022
10
ronaldo
10
2021
11
messi
10
2021
I tried inner join, but the result was wrong:
id
name
real 2022
target 2022
real 2021
target 2021
10
ronaldo
20
30
20
30
11
messi
20
30
20
30
Desired result:
id
name
real 2022
target 2022
real 2021
target 2021
10
ronaldo
10
10
10
10
11
messi
10
10
10
10
<?php
$sql = $pdo->prepare("SELECT *,
SUM( case when YEAR(real.year) = YEAR(CURDATE()) then real.goal else 0 end) AS goal_now,
SUM( case when YEAR(real.year) = YEAR(CURDATE() - INTERVAL 1 YEAR) then real.goal else 0 end) AS goal_then,
SUM( case when YEAR(target.year) = YEAR(CURDATE()) then target.goal else 0 end) AS goal_target,
SUM( case when YEAR(target.year) = YEAR(CURDATE() - INTERVAL 1 YEAR) then target.goal else 0 end) AS goal_target_then
FROM real
left join target
on id_real = id_target
group by real.id_real
having
real.id_real LIKE '1%'
");
$sql->execute();
while($data = $sql->fetch()){
?>
select
id,
name,
sum(real_goal_now) as real_goal_now,
sum(real_goal_then) as real_goal_then,
sum(target_goal_now) as target_goal_now,
sum(target_goal_then) as target_goal_then
from
(
select
a.id,
a.name,
case
when a.year = year(curdate()) then a.goal else 0 end as real_goal_now,
case
when a.year = year(curdate() - interval 1 year) then a.goal else 0 end as real_goal_then,
case
when b.year = year(curdate()) then b.goal else 0 end as target_goal_now,
case
when b.year = year(curdate() - interval 1 year) then b.goal else 0 end as target_goal_then
from
(
select
id, name, year, sum(goal) goal
from
real
group by
id, year) a,
(
select
id, name, year, sum(goal) goal
from
target
group by
id, year) b
where
a.id = b.id
and a.year = b.year
group by
a.id, a.year) c
group by
id;
<?php
$sql = $pdo->prepare("SELECT *,
SUM( case when YEAR(real.year) = YEAR(CURDATE()) then real.goal else 0 end) AS goal_now,
SUM( case when YEAR(real.year) = YEAR(CURDATE() - INTERVAL 1 YEAR) then real.goal else 0 end) AS goal_then
FROM real
left join ( select *,
SUM( case when YEAR(target.year) = YEAR(CURDATE()) then target.goal else 0 end) AS goal_target,
SUM( case when YEAR(target.year) = YEAR(CURDATE() - INTERVAL 1 YEAR) then target.goal else 0 end) AS goal_target_then
from target
group by id_target ) as t
on real.id_real = t.id_target
group by real.id_real
having
real.id_real LIKE '1%'
");
$sql->execute();
while($data = $sql->fetch()){
?>

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.

How to get last month sale and this month sale in sql server 2008

How to get last month sale and this month sale in sql server 2008
Here my table structure is:
DailyDispatchID int Unchecked
Name varchar(50) Checked
Qty int Checked
Month varchar(50) Checked
i tried this:
SELECT *
FROM (SELECT Name,
Gas,
Qty,
Month
FROM DailyDispatch) AS s
PIVOT (Sum(QTY)
FOR [month] IN(aug,
sep)) AS P
i am getting the output,but its taking only from August and September monthly only. it should generate automatically this month and previous sales. how to sort out this.,
you need to set the parameter in the in clause according to your current month and last month
SELECT Name,
Gas,
Qty,
[Month]
FROM DailyDispatch
WHERE [Month] IN (SELECT CASE
WHEN Month(Getdate()) = 1 THEN 'jan'
WHEN Month(Getdate()) = 2 THEN 'feb'
WHEN Month(Getdate()) = 3 THEN 'mar'
WHEN Month(Getdate()) = 4 THEN 'apr'
WHEN Month(Getdate()) = 5 THEN 'may'
WHEN Month(Getdate()) = 6 THEN 'jun'
WHEN Month(Getdate()) = 7 THEN 'jul'
WHEN Month(Getdate()) = 8 THEN 'aug'
WHEN Month(Getdate()) = 9 THEN 'sep'
WHEN Month(Getdate()) = 10 THEN 'oct'
WHEN Month(Getdate()) = 11 THEN 'nov'
WHEN Month(Getdate()) = 12 THEN 'dec'
END [month]
UNION ALL
SELECT CASE
WHEN Month(Dateadd(mm, -1, Getdate())) = 1 THEN 'jan'
WHEN Month(Dateadd(mm, -1, Getdate())) = 2 THEN 'feb'
WHEN Month(Dateadd(mm, -1, Getdate())) = 3 THEN 'mar'
WHEN Month(Dateadd(mm, -1, Getdate())) = 4 THEN 'apr'
WHEN Month(Dateadd(mm, -1, Getdate())) = 5 THEN 'may'
WHEN Month(Dateadd(mm, -1, Getdate())) = 6 THEN 'jun'
WHEN Month(Dateadd(mm, -1, Getdate())) = 7 THEN 'jul'
WHEN Month(Dateadd(mm, -1, Getdate())) = 8 THEN 'aug'
WHEN Month(Dateadd(mm, -1, Getdate())) = 9 THEN 'sep'
WHEN Month(Dateadd(mm, -1, Getdate())) = 10 THEN 'oct'
WHEN Month(Dateadd(mm, -1, Getdate())) = 11 THEN 'nov'
WHEN Month(Dateadd(mm, -1, Getdate())) = 12 THEN 'dec'
END [month])

Count records for each age range

I have a mysql table
TABLE name test
Id INT PRIMARY KEY
Lahir Date
Gender CHAR(1)
Nama Varchar(100)
id_desa CHAR(4)
How can I get report like this?
id_desa 0-10 11-20 21-30 31-40 41-50
10B 1 2 0 1 8
10C 2 4 7 1 0.
try
select
id_desa,
sum(case when FLOOR(DATEDIFF(Lahir,NOW())/365) between 0 and 10 then 1 else 0 end) as '0-10',
sum(case when FLOOR(DATEDIFF(Lahir,NOW())/365) between 11 and 20 then 1 else 0 end) as '11-20',
sum(case when FLOOR(DATEDIFF(Lahir,NOW())/365) between 21 and 30 then 1 else 0 end) as '21-30',
sum(case when FLOOR(DATEDIFF(Lahir,NOW())/365) between 31 and 40 then 1 else 0 end) as '31-40',
sum(case when FLOOR(DATEDIFF(Lahir,NOW())/365) between 41 and 50 then 1 else 0 end) as '41-50'
from tbl
You can try to use this query.
I assume that the table name is name_test.
SELECT id_desa,
(SELECT Count(*) FROM name_test WHERE DATE_FORMAT(FROM_DAYS(DATEDIFF(Now(),Lahir)), "%y")+0 BETWEEN 0 AND 10) as "0-10",
(SELECT Count(*) FROM name_test WHERE DATE_FORMAT(FROM_DAYS(DATEDIFF(Now(),Lahir)), "%y")+0 BETWEEN 11 AND 20) as "11-20",
(SELECT Count(*) FROM name_test WHERE DATE_FORMAT(FROM_DAYS(DATEDIFF(Now(),Lahir)), "%y")+0 BETWEEN 21 AND 30) as "21-30",
(SELECT Count(*) FROM name_test WHERE DATE_FORMAT(FROM_DAYS(DATEDIFF(Now(),Lahir)), "%y")+0 BETWEEN 31 AND 40) as "31-40",
(SELECT Count(*) FROM name_test WHERE DATE_FORMAT(FROM_DAYS(DATEDIFF(Now(),Lahir)), "%y")+0 BETWEEN 41 AND 50) as "41-50"
FROM name_test
MySQL has a very nice function called TIMESTAMPDIFF
SELECT
id_desa,
SUM(TIMESTAMPDIFF(YEAR, Lahir, CURRENT_DATE) BETWEEN 0 AND 10) `0-10`,
SUM(TIMESTAMPDIFF(YEAR, Lahir, CURRENT_DATE) BETWEEN 11 AND 20) `11-20`,
SUM(TIMESTAMPDIFF(YEAR, Lahir, CURRENT_DATE) BETWEEN 21 AND 30) `21-30`,
SUM(TIMESTAMPDIFF(YEAR, Lahir, CURRENT_DATE) BETWEEN 31 AND 40) `31-40`,
SUM(TIMESTAMPDIFF(YEAR, Lahir, CURRENT_DATE) BETWEEN 41 AND 50) `41-50`,
SUM(TIMESTAMPDIFF(YEAR, Lahir, CURRENT_DATE) > 50) `50+`
FROM tablename
GROUP BY id_desa

Mysql Case - Count Fields thats fall in curtain hours

I am trying to set up a report that has the following fields :
Pretty much need the report to show the date, the total records that fall with in that date (so I group by Date) and then on a per hour basis for a 12 hour work day (from 8AM - 8PM) I need to count when a records is present within those times. After some brain storming and brain farts I thought why not use a case. This is what I have tried :
SELECT
DATE_FORMAT(signintime, '%b %d, %Y') Date,
COUNT(session_id) as Total,
SUM(CASE WHEN HOUR(signintime) > 08 AND HOUR(signintime) < 09 THEN 1 ELSE 0 END) '8AM-9PM',
SUM(CASE WHEN HOUR(signintime) > 09 AND HOUR(signintime) < 10 THEN 1 ELSE 0 END) '9AM-10AM',
SUM(CASE WHEN HOUR(signintime) > 10 AND HOUR(signintime) < 11 THEN 1 ELSE 0 END) '10AM-11AM',
SUM(CASE WHEN HOUR(signintime) > 11 AND HOUR(signintime) < 12 THEN 1 ELSE 0 END) '11AM-12PM',
SUM(CASE WHEN HOUR(signintime) > 12 AND HOUR(signintime) < 13 THEN 1 ELSE 0 END) '12PM-1PM',
SUM(CASE WHEN HOUR(signintime) > 13 AND HOUR(signintime) < 14 THEN 1 ELSE 0 END) '1PM-2PM',
SUM(CASE WHEN HOUR(signintime) > 14 AND HOUR(signintime) < 15 THEN 1 ELSE 0 END) '2PM-3PM',
SUM(CASE WHEN HOUR(signintime) > 15 AND HOUR(signintime) < 16 THEN 1 ELSE 0 END) '3PM-4PM',
SUM(CASE WHEN HOUR(signintime) > 16 AND HOUR(signintime) < 17 THEN 1 ELSE 0 END) '4PM-5PM',
SUM(CASE WHEN HOUR(signintime) > 17 AND HOUR(signintime) < 18 THEN 1 ELSE 0 END) '5PM-6PM',
SUM(CASE WHEN HOUR(signintime) > 18 AND HOUR(signintime) < 19 THEN 1 ELSE 0 END) '6PM-7PM',
SUM(CASE WHEN HOUR(signintime) > 19 AND HOUR(signintime) < 20 THEN 1 ELSE 0 END) '7PM-8PM'
FROM session
WHERE session.status = '3'
GROUP by HOUR(signintime), Date;
If you notice the picture above, the total is 1 for that entire day (Apr 19, 2013) now if you notice the times (8AM - 8PM) they are all zeroed out. I am unsure as to where to turn to / where to debug this. Hopefully with another set of eyes I can get this moving along.
Regards.
You should not need to use both the > and < to get the result, you can just check the hour value:
select DATE_FORMAT(signintime, '%b %d, %Y') Date,
count(session_id) as Total,
SUM(CASE WHEN HOUR(signintime) = 08 THEN 1 ELSE 0 END) '8AM-9AM',
SUM(CASE WHEN HOUR(signintime) = 09 THEN 1 ELSE 0 END) '9AM-10AM',
SUM(CASE WHEN HOUR(signintime) = 10 THEN 1 ELSE 0 END) '10AM-11AM',
SUM(CASE WHEN HOUR(signintime) = 11 THEN 1 ELSE 0 END) '11AM-12PM',
SUM(CASE WHEN HOUR(signintime) = 12 THEN 1 ELSE 0 END) '12PM-1PM',
SUM(CASE WHEN HOUR(signintime) = 13 THEN 1 ELSE 0 END) '1PM-2PM',
SUM(CASE WHEN HOUR(signintime) = 14 THEN 1 ELSE 0 END) '2PM-3PM',
SUM(CASE WHEN HOUR(signintime) = 15 THEN 1 ELSE 0 END) '3PM-4PM',
SUM(CASE WHEN HOUR(signintime) = 16 THEN 1 ELSE 0 END) '4PM-5PM',
SUM(CASE WHEN HOUR(signintime) = 17 THEN 1 ELSE 0 END) '5PM-6PM',
SUM(CASE WHEN HOUR(signintime) = 18 THEN 1 ELSE 0 END) '6PM-7PM',
SUM(CASE WHEN HOUR(signintime) = 19 THEN 1 ELSE 0 END) '7PM-8PM'
from session
WHERE session.status = '3'
group by DATE_FORMAT(signintime, '%b %d, %Y');
See SQL Fiddle with Demo