Select with count - mysql

So.. here is the thing.. Im trying to get a report based on this table on Mysql.
Table have these columns
statusvalue submitdate employeeNumber
2 2016-11-17 17:49:05 2
1 2016-11-17 17:49:11 4
3 2016-11-17 17:49:16 4
4 2016-11-17 17:50:58 2
5 2016-11-17 17:51:07 5
Status 0 = Pending
1= Started
2=inprogress
3= cancelled
4= terminated
5=unknown
now i need to select data as below
date day pending started inprogress cancelled terminated total
2017-04-17 Monday 0 1 4 0 1 6
2017-04-16 Sunday 0 1 4 0 1 6
so far i can get the date and the day
select date(submitdate) as subdate, case weekday(date(submitdate))
WHEN 0 then 'PENDING'
WHEN 1 then 'STARTED'
WHEN 2 then 'INPROGRESS'
WHEN 3 then 'CANCELLED'
WHEN 4 then 'TERMINATED'
WHEN 5 then 'UNKNOWN' END as submitdate
from tb_status t1 where employeenumber=15 group by subdate order by subdate ;
however not quite sure how to do a count based on status.. any help would be greatly appreciated.

You just need a basic pivot query here. Aggregate over the submission date, and then tally the various types of status as you go along.
SELECT
DATE(submitdate) AS subdate,
WEEKDAY(submitdate) AS day,
SUM(CASE WHEN WEEKDAY(DATE(submitdate)) = 0 THEN 1 END) AS PENDING,
SUM(CASE WHEN WEEKDAY(DATE(submitdate)) = 1 THEN 1 END) AS STARTED,
SUM(CASE WHEN WEEKDAY(DATE(submitdate)) = 2 THEN 1 END) AS INPROGRESS,
SUM(CASE WHEN WEEKDAY(DATE(submitdate)) = 3 THEN 1 END) AS CANCELLED,
SUM(CASE WHEN WEEKDAY(DATE(submitdate)) = 4 THEN 1 END) AS TERMINATED,
SUM(CASE WHEN WEEKDAY(DATE(submitdate)) = 5 THEN 1 END) AS UNKNOWN
FROM tb_sattus t1
WHERE employeenumber = 15
GROUP BY DATE(submitdate)
ORDER BY subdate
Note that your current query is not correct, because you are selecting string literals based on the status. Instead, in my query the various status types appear as column names, with the counts you want instead being selected.

Give this a try:
SELECT
`submitDate` as `date`,
DATE_FORMAT(`submitDate`,'%a') as `day`,
SUM(if(`statusvalue` = 0,1,0)) as `pending`,
SUM(if(`statusvalue` = 1,1,0)) as `started`,
SUM(if(`statusvalue` = 2,1,0)) as `inprogress`,
SUM(if(`statusvalue` = 3,1,0)) as `cancelled`,
SUM(if(`statusvalue` = 4,1,0)) as `terminated`,
SUM(if(`statusvalue` = 5,1,0)) as `unknown`,
SUM(if(NOT `statusvalue` IN (0,1,2,3,4,5),1,0)) as `invalid`,
COUNT(`statusvalue`) as `total`
FROM `tbstatus`
GROUP BY `submitDate`
ORDER BY `submitDate`;

Related

Count data with mysql query

i have this following table
id kelurahan status
1 Pegambiran Netral
2 Pegambiran Netral
3 Kejaksan Positif
4 Kesenden Positif
5 Pegambiran Negatif
i want to get result like this
kelurahan count_positif count_netral count_negatif total
Pegambiran 0 2 1 3
Kejaksan 1 0 0 1
Kesenden 1 0 0 1
i tried this query
SELECT kelurahan,
(SELECT COUNT(status) FROM tbl_monitoring WHERE status = 'Positif' GROUP BY kelurahan LIMIT 1) AS count_positif,
(SELECT COUNT(status) FROM tbl_monitoring WHERE status = 'Netral' GROUP BY kelurahan) AS count_netral,
(SELECT COUNT(status) FROM tbl_monitoring WHERE status = 'Negatif' GROUP BY kelurahan) AS count_negatif,
COUNT(kelurahan) AS total
FROM tbl_monitoring GROUP BY kelurahan
i get the result like this
any help would be appreciated, thanks.
You should use SUM, not COUNT.
Tested on dbfiddle
SELECT
kelurahan,
SUM(CASE WHEN status = 'Positif' THEN 1 ELSE 0 END) AS count_positif,
SUM(CASE WHEN status = 'Netral' THEN 1 ELSE 0 END) AS count_netral,
SUM(CASE WHEN status = 'Negatif' THEN 1 ELSE 0 END) AS count_negatif,
SUM(1) AS total
FROM tbl_monitoring
GROUP BY kelurahan;

MySQL- Count total records based on different values of same column

I have a table as follows
table_user
id name status flag country
====================================================================================================
1 AB 1 0 US
2 BC 1 0 UK
3 CD 0 0 IN
4 DE 3 0 BR
5 EF 3 0 UK
6 FG 2 0 IN
7 GH 4 0 IN
I want to count the no. of records where
status = 1 as totalactiverecords,
status = 0 as totalinactiverecords,
status = 3 as totalrecentactiverecords,
status = 2 as totalemailnotverifiedrecords,
status = 4 as totalemailverifiedrecords,
, and country is UK, all in a single SELECT statement.
Is there any specific way to do it?
I have thought of something like
SELECT COUNT(*) as totalactiverecords WHERE status=1
COUNT(*) as totalinactiverecordsWHERE status=0,
COUNT(*) as totalemailnotverifiedrecords WHERE status=2,
COUNT(*) as totalrecentactiverecords WHERE status=3,
COUNT(*) as totalemailverifiedrecords WHERE status=4
FROM table_user where country=IN
,but that does not seem to work.
You can try this query:
SELECT count(case status when '1' then 1 else null end) as totalactiverecords,
count(case status when '0' then 1 else null end) as totalinactiverecords,
count(case status when '2' then 1 else null end) as totalemailnotverifiedrecords,
count(case status when '3' then 1 else null end) as totalrecentactiverecords,
count(case status when '4' then 1 else null end) as totalemailverifiedrecords
FROM table_user where country='IN'
I usually use CASE WHEN in this kind of problem:
SELECT sum(case when status = 1 then 1 end) as totalactiverecords,
COUNT sum(case when status = 0 then 1 end) as totalinactiverecords,
COUNT sum(case when status = 2 then 1 end) as totalemailnotverifiedrecord,
COUNT sum(case when status = 3 then 1 end) as totalrecentactiverecords,
COUNT sum(case when status = 4 then 1 end) as totalemailverifiedrecords
FROM table_user where country=IN

MySQL - users' monthly spend in the first 6 months of initial subscription purchase

I am trying to create report that shows subscription price (AmountPerMonth) month by month for 6 months for all users - where Month1 is the date the user has purchased the 1st subscription date (NOT the registration date) , and Month2 etc are the subsequent months from that date, varying for each account.
The format of the table for this report
I have managed to pull the first 2 months table, but can't figure out how to continue up to the 6th month. Thank you in advance!
SELECT F1.Id, F1.Month1, F2.Month2
FROM
(SELECT Id, AmountPerMonth AS Month1, ActionDate
FROM MONTLYSPEND
GROUP BY ID
HAVING MIN(ActionDate)) AS F1,
(SELECT t1.Id R, t2.AmountPerMonth AS Month2, MIN(t2.ActionDate)
FROM MONTLYSPEND t1
INNER JOIN MONTLYSPEND t2
ON t1.Id = t2.Id
AND t1.ActionDate < t2.ActionDate
GROUP BY t1.Id) AS F2
WHERE F1.id = F2.R
;
Turns out there were two ways - Stored Procedure, which takes too much memory, or using CASE WHEN, this pivots the table as well. Hope it's useful to people who have to generate reports showing various activity per user day by day or month by month on the x axis. The main difficulty I had was the fact that Month_1 (first purchased subscription) was a different date for every user. This report can be used to analyse your users behavior in the first 6 months of their subscription.
The report generated by this query looks like this:
+--------+----------+------------------+---------+---------+--------+
| UserId | Currency | FirstSubscrPurch | Month_1 | Month_2 | etc... |
+--------+----------+------------------+---------+---------+--------+
| 123 | GBP | 2010-05-27 | 34.00 | 27.00 | 0.00 |
+--------+----------+------------------+---------+---------+--------+
SELECT F6.USERID, F6.Currency, DATE_FORMAT(F6.FirstSubscrPurch, "%Y-%m-%d") AS FirstSubscrPurch, F6.MONTH_1, F6.MONTH_2,F6.MONTH_3, F6.MONTH_4, F6.MONTH_5, F6.MONTH_6, ROUND(((F6.MONTH_1+F6.MONTH_2+F6.MONTH_3+F6.MONTH_4+F6.MONTH_5+F6.MONTH_6)/6),2) AVERAGE, F6.CURRENCY
FROM (
SELECT
UserId, Currency, FirstSubscrPurch,
SUM(CASE WHEN YEAR_AND_MONTH_INDEX = 0 THEN TOTAL_AMOUNT_PAID ELSE 0 END) MONTH_1,
SUM(CASE WHEN YEAR_AND_MONTH_INDEX = 1 THEN TOTAL_AMOUNT_PAID ELSE 0 END) MONTH_2,
SUM(CASE WHEN YEAR_AND_MONTH_INDEX = 2 THEN TOTAL_AMOUNT_PAID ELSE 0 END) MONTH_3,
SUM(CASE WHEN YEAR_AND_MONTH_INDEX = 3 THEN TOTAL_AMOUNT_PAID ELSE 0 END) MONTH_4,
SUM(CASE WHEN YEAR_AND_MONTH_INDEX = 4 THEN TOTAL_AMOUNT_PAID ELSE 0 END) MONTH_5,
SUM(CASE WHEN YEAR_AND_MONTH_INDEX = 5 THEN TOTAL_AMOUNT_PAID ELSE 0 END) MONTH_6
FROM (
SELECT
hp.UserId, hp.Currency, MIN(hp.Date) AS FirstSubscrPurch,
CONCAT(YEAR(Date),'-',MONTH(Date)) AS YEAR_AND_MONTH,
TIMESTAMPDIFF( MONTH, CONCAT(YEAR(FIRST_PAYMENT_DATE),'-',MONTH(FIRST_PAYMENT_DATE),'-1'), CONCAT(YEAR(Date),'-',MONTH(Date),'-1')) AS YEAR_AND_MONTH_INDEX, -- generates string in format YYYY-M-D
MIN(Date) FIRST_PAYMENT_OF_MONTH,
MAX(Date) LAST_PAYMENT_OF_MONTH,
COUNT(*) NUM_PAYMENTS,
SUM(hp.Amount) TOTAL_AMOUNT_PAID,
SUM(hp.Credits) Credits
FROM payments hp
JOIN (
SELECT UserId, MIN(Date) FIRST_PAYMENT_DATE, ADDDATE(MIN(Date), INTERVAL 6 MONTH) SIX_MONTHS_AFTER_FIRST_PAYMENT
FROM payments hp
GROUP BY UserId
) USER_MIN_ID ON USER_MIN_ID.UserId = hp.UserId
AND hp.Date BETWEEN FIRST_PAYMENT_DATE AND CONCAT(YEAR(SIX_MONTHS_AFTER_FIRST_PAYMENT),'-',MONTH(SIX_MONTHS_AFTER_FIRST_PAYMENT),'-1')
GROUP BY UserId, Currency, YEAR_AND_MONTH
ORDER BY hp.UserId, hp.Date
) F
GROUP BY UserId, Currency
ORDER BY UserId DESC) F6;

MySQL using Sum and Case

I'm trying to create a GridView with ASP.NET connecting to a MySQL database. The data appears like below.
BusinessUnit OrderDate Canceled
UnitA 1/15/2013 N
UnitA 10/1/2013 N
UnitB 10/15/2013 N
UnitB 10/22/2013 N
UnitB 10/22/2013 N
Based on the records above, I'd like the result to appear like below
BusinessUnit TodaysOrders ThisMonthsOrders ThisYearsOrders
UnitA 0 1 2
UnitB 2 3 3
My current code is below. It's giving me error (something about DatabaseName.sum does not exist. Check the
Function Name Parsing and Resolution' section... )
Select
SUM (CASE WHEN (OrderDate)=DATE(NOW()) THEN 1 ELSE 0 END) AS TodaysOrders,
SUM (CASE WHEN YEAR(OrderDate) = YEAR(CURDATE()) AND MONTH(OrderDate) = MONTH(CURDATE()) THEN 1 ELSE 0 END) AS ThisMonthsOrders,
SUM (CASE WHEN YEAR(main_order_managers.creation_date) = YEAR(CURDATE()) THEN 1 ELSE 0 END) AS ThisYearsOrders
code continues
FROM OrderTable WHERE OrderTable.Canceled. <> 'Y';
Is Sum Case the best use here?
The error is caused by the space between function name and parenthesis
SUM (CASE WHEN ...
^^
Read more Function Name Parsing and Resolution
Try
SELECT BusinessUnit,
SUM(CASE WHEN OrderDate = CURDATE() THEN 1 ELSE 0 END) TodaysOrders,
SUM(CASE WHEN DATE_FORMAT(OrderDate, '%Y%m') = DATE_FORMAT(CURDATE(), '%Y%m') THEN 1 ELSE 0 END) ThisMonthsOrders,
SUM(CASE WHEN YEAR(OrderDate) = YEAR(CURDATE()) THEN 1 ELSE 0 END) ThisYearsOrders
FROM OrderTable
WHERE Canceled <> 'Y'
GROUP BY BusinessUnit
Here is SQLFiddle demo

how to select and group mysql data based on the following table

how can I achieve the desired result in mysql if my table looks like this.
result|year
1 |2011
2 |2011
1 |2011
0 |2011
1 |2012
2 |2012
1 = Won, 2 = lost, 0 = draw
Every year can have multiple values like this. Not sure how I can get the desired result like below.
year won lost draw totalPlayed
2011 2 1 1 3
2012 1 1 0 2
I have tried the following query but does not get the desired result
select year,
league_types.league_name,
sum(if(result = 1,1,0)) as won,
sum(if(result = 0,1,0)) as draw,
sum(if(result = 4,1,0)) as noResult,
sum(if(result = 2,1,0)) as lost,
sum(if(result = 3,1,0)) as tied,
sum(if(result > 0 and result < 4,1,0)) as played
from match_score_card
inner join fixtures on match_score_card.match_id = fixtures.match_id
inner join league_types on fixtures.league_id = league_types.league_id
where
team_id = 1 group by year order by year desc
Here is the SQL Fiddle that demonstrates the following query:
SELECT m.year,
SUM(CASE WHEN m.result = 1 THEN 1 ELSE 0 END) AS 'Won',
SUM(CASE WHEN m.result = 2 THEN 1 ELSE 0 END) AS 'Lost',
SUM(CASE WHEN m.result = 0 THEN 1 ELSE 0 END) AS 'Draw',
COUNT(*) AS 'TotalPlayed'
FROM MyTable AS m
GROUP BY m.year
I'm not familiar with that IF function in mySQL, but this standard SQL should work:
select year
, league_types.league_name
, sum(CASE WHEN result = 1 THEN 1 ELSE 0 END) as won
, sum(CASE WHEN result = 2 THEN 1 ELSE 0 END) as lost
, sum(CASE WHEN result = 3 THEN 1 ELSE 0 END) as draw
, sum(CASE WHEN result = 4 THEN 1 ELSE 0 END) as noResult
, sum(CASE WHEN result = 1
or result = 2 THEN 1 ELSE 0 END) as played
from match_score_card
inner join fixtures
on match_score_card.match_id = fixtures.match_id
inner join league_types
on fixtures.league_id = league_types.league_id
where team_id = 1
group by year, league_types.league_name
order by year desc, league_types.league_name
I'm guessing that you only want to count wins and losses as "played".