This question already has answers here:
How can I return pivot table output in MySQL?
(10 answers)
Closed 9 years ago.
I have got some result for this query but instead of it i want to change a result like below
Result to be shown::
2011 2012
26267.620000000003 404603.20999999996
144384.36 173245.96000000002
199704.47999999998 208524.42
Query
SELECT
YEAR (PAYMENTDATE) AS YEARNUMBER,
MONTH (PAYMENTDATE) AS MONNUMBER,
SUM (AMOUNT) AS MONTOTAL
FROM
PAYMENTS
GROUP BY
YEAR (PAYMENTDATE)
Result:: for above query
2011 26267.620000000003
2011 144384.36
2012 173245.96000000002
2012 208524.42
Try this:
SELECT MONNUMBER,
MAX(CASE WHEN YEARNUMBER = 2011 THEN MONTOTAL ELSE 0 END) AS Year2011,
MAX(CASE WHEN YEARNUMBER = 2012 THEN MONTOTAL ELSE 0 END) AS Year2011
FROM (SELECT YEAR(PAYMENTDATE) AS YEARNUMBER, MONTH(PAYMENTDATE) AS MONNUMBER,
SUM(AMOUNT) AS MONTOTAL
FROM PAYMENTS
GROUP BY YEAR(PAYMENTDATE)
) AS A
GROUP BY MONNUMBER
Related
I am working on a warehouse project. In this project I need to get output a report where it will summarize month wise issued(Sold) items. The output is per expecting from below MySQL query.
SELECT Product,Stock,SUM(CASE WHEN MONTH(Issue_Date) = 1 THEN `Issue_Qty` END) jan, SUM(CASE WHEN MONTH(Issue_Date) = 2 THEN `Issue_Qty` END) feb, SUM(CASE WHEN MONTH(Issue_Date) = 3 THEN `Issue_Qty` END) mar, -------------- SUM(CASE WHEN MONTH(Issue_Date) = 12 THEN `Issue_Qty` END) December FROM product WHERE YEAR(Issue_Date) = 2021 GROUP by Product ORDER BY Product ASC
But issue is I am not able to get output data those items are not issued on 2021. And if I remove where clause then all previous years sold items data are displaying in this report by respective month wise.
I want to get my report like below screen shot wise, where last two rows items not issued in 2021 but these items stock quantity will only display in report. Looking help from MySQL Guru to solve this problem. Thanks in advance for your help on this regard.
As all product info is needed but balance will calculated by specific year and month so move WHERE clause info in every case statement along with year.
Stock value is summed up other wise it must be place on GROUP BY clause.
-- MySQL
SELECT Product,SUM(Stock) Stock
, SUM(CASE WHEN YEAR(Issue_Date) = 2021 AND MONTH(Issue_Date) = 1 THEN `Issue_Qty` END) jan
, SUM(CASE WHEN YEAR(Issue_Date) = 2021 AND MONTH(Issue_Date) = 2 THEN `Issue_Qty` END) feb
, SUM(CASE WHEN YEAR(Issue_Date) = 2021 AND MONTH(Issue_Date) = 3 THEN `Issue_Qty` END) mar
--------------
, SUM(CASE WHEN YEAR(Issue_Date) = 2021 AND MONTH(Issue_Date) = 12 THEN `Issue_Qty` END) December
FROM product
GROUP by Product
ORDER BY Product
Two ways for selecting year and month
YEAR(tdate) = '2021' AND MONTH(tdate) = 1
DATE_FORMAT(tdate, "%Y-%m") = '2021-01'
Please check from url https://dbfiddle.uk/?rdbms=mysql_5.7&fiddle=e9e3e8ede5de378e2f4ed8df2b9fbe76
This question already has answers here:
MySQL monthly Sale of last 12 months including months with no Sale
(3 answers)
Get Record Per Month But Also Get Zero If No Records That Month
(2 answers)
Mysql to select month-wise record even if data not exist
(2 answers)
MySQL query to get last 12 months data grouped by month including zero counts
(1 answer)
Closed 4 years ago.
I want get the last 3 months data from current date then i will get four month names in that i have only 3 month data but i do not have one month data in middle.
SELECT
count(v.visit_id) as count,
MONTHNAME(v.updated) AS Month_name
FROM patient_status as p, visit_history_details as v
WHERE v.visit_id = p.visit_id and v.hospital_code = 'id'
and p.doctor_id = '2' and v.updated >= now()-interval 3 month
GROUP by Month_name
My Result are coming like this:
January 10
December 12
October 10
But I want this result:
January 10
December 12
November 0
October 10
please help me how to solve this issue
For the month you should use a subquery for get all the month you need:
select monthname(my_date), ifnull(count, 0)
from (
select str_to_date('2018-01-01', '%Y-%m-%d') as my_date
union
select str_to_date('2018-10-01', '%Y-%m-%d')
union
select str_to_date('2018-11-01', '%Y-%m-%d')
union
select str_to_date('2018-12-01', '%Y-%m-%d')
) t
left join (
SELECT
count(v.visit_id) as count,
MONTHNAME(v.updated) AS Month_name,
MONTH(v.updated) as my_month
FROM patient_status as p
INNER JOIN visit_history_details as v
ON v.visit_id = p.visit_id
WHERE v.hospital_code = 'id'
and p.doctor_id = '2'
and v.updated >= now()-interval 3 month
GROUP by Month_name
) r on r.my_month = t.my_month
And you should avoid old implicit join syntax and use the explicit syntax.
Or as suggested by Salman A for a more general solution:
select monthname(my_date), ifnull(count, 0)
from (
select LAST_DAY(CURRENT_TIMESTAMP) + INTERVAL 1 DAY - INTERVAL 1 MONTH as my_date
union
select LAST_DAY(CURRENT_TIMESTAMP) + INTERVAL 1 DAY - INTERVAL 2 MONTH
union
select LAST_DAY(CURRENT_TIMESTAMP) + INTERVAL 1 DAY - INTERVAL 3 MONTH
union
select LAST_DAY(CURRENT_TIMESTAMP) + INTERVAL 1 DAY - INTERVAL 4 MONTH
) t
left join (
SELECT
count(v.visit_id) as count,
MONTHNAME(v.updated) AS Month_name,
MONTH(v.updated) as my_month
FROM patient_status as p
INNER JOIN visit_history_details as v
ON v.visit_id = p.visit_id
WHERE v.hospital_code = 'id'
and p.doctor_id = '2'
and v.updated >= now()-interval 3 month
WHERE v.visit_id = p.visit_id and v.hospital_code = 'id'
and p.doctor_id = '2' and v.updated >= now()-interval 3 month
GROUP by Month_name
Maybe there is a simple fix but I can't seam to figure it out. I'll try my best to explain my situation.
I'm working on a MySQL query that will return results within date range (in column A), and for date range - 1 year (in column B). I need to group results by month day and not by year. So I would like to have something like this:
2014 2013
----------------
01-01 6 8
01-03 7 0
01-04 4 1
01-08 0 13
01-21 11 7
In my current query (below) I get results like this (because of ELSE in CASE):
2014 2013
----------------
01-01 0 8
01-03 7 0
01-04 0 1
01-08 0 13
01-21 0 7
QUERY:
SELECT
DATE_FORMAT(table.date, '%e.%c.') AS date,
(CASE WHEN DATE(table.date) BETWEEN '2014-01-01' AND '2014-02-01' THEN ROUND(SUM(table.field), 2) ELSE 0 END) AS field_2014,
(CASE WHEN DATE(table.date) BETWEEN '2013-01-01' AND '2013-02-01' THEN ROUND(SUM(table.field), 2) ELSE 0 END) AS field_2013
FROM table
WHERE
(DATE(table.date) BETWEEN '2014-05-01' AND '2014-06-01' OR DATE(table.date) BETWEEN '2013-05-01' AND '2013-06-01')
GROUP BY
DATE_FORMAT(table.date, '%c.%e.')
What should I put in ELSE and how can I achieve this functionality?
Thank you for your time
You need aggregation functions. I would recommend:
SELECT DATE_FORMAT(t.date, '%e.%c.') AS date,
SUM(CASE WHEN year(t.date) = 2014 THEN ROUND(SUM(t.field), 2) ELSE 0 END) AS field_2014,
SUM(CASE WHEN year(t.date) = 2013 THEN ROUND(SUM(t.field), 2) ELSE 0 END) AS field_2013
FROM table t
WHERE year(t.date) in (2013, 2014) and month(t.date) = 5
GROUP BY DATE_FORMAT(t.date, '%c.%e.');
I would also recommend using the format '%m-%d'. Having the month then the year means that order by will work on the column. Having all the dates be the same width ("05/01" rather than "5/1") better corresponds to your desired output.
This question already has answers here:
How to generate data in MySQL?
(4 answers)
Closed 8 years ago.
I have a query that counts all Work ID Numbers by month and year and then creates a chart for the past 13 months using jpgraph. It works great except there are no Work ID Numbers in July so the chart totally skips July.
Query Results:
5
16
15
11
3
12
4
8
2
9
13
12
Desired Results:
5
16
15
11
3
12
0
4
8
2
9
13
12
As you can see I need the (0) zero in order for my chart to work, however since there are no Work ID Number in July my query simply skips it. Here is my query:
SELECT COUNT( WORK_ID_NUM ) AS count,
DATE FROM SERVICE_JOBS
WHERE (DATE BETWEEN '$lastyear' AND '$date')
AND JOB_TYPE LIKE 'Street Light'
GROUP BY YEAR( DATE ), MONTH( DATE )
ORDER BY DATE
sqlFiddle Demo
SELECT IFNULL(count,0) as count,theDate as Date
FROM
(SELECT #month := #month+INTERVAL 1 MONTH as theDate
FROM service_jobs,(SELECT #month:='$lastyear' - INTERVAL 1 MONTH)as T1
LIMIT 13)as T2
LEFT JOIN
(SELECT COUNT(WORK_ID_NUM)as count,DATE
FROM service_jobs
WHERE (DATE BETWEEN '$lastyear' AND '$date')
AND JOB_TYPE LIKE 'Street Light'
GROUP BY YEAR(DATE), MONTH(DATE)) T3
ON (YEAR(theDate) = YEAR(DATE) AND MONTH(theDate) = MONTH(DATE))
ORDER BY theDate;
To get your query to return a row for July, you need to have a row with July in it. You could create a table with all the dates between $lastyear and $date in it and then outer join from that to SERVICE_JOB.
SELECT COUNT( WORK_ID_NUM ) AS count,
allDates.DATE
FROM AllDates
Left outer join SERVICE_JOB
on AllDates.DATE = SERVICE_JOB.DATE
WHERE (AllDates.DATE BETWEEN '$lastyear' AND '$date') AND
(SERVICE_JOB.WORK_ID_NUM is NULL OR JOB_TYPE LIKE 'Street Light')
GROUP BY YEAR( AllDates.DATE ), MONTH( AllDates.DATE )
ORDER BY AllDates.DATE
In SQL Server it would be pretty easy to make a Common Table Expression that could fill AllDates for you based on $lastyear and $date. Not sure about MySql.
This question already has answers here:
Calculate a running total in MySQL
(5 answers)
Closed 8 years ago.
I'm using the following statement to read out user-registrations
SELECT DAY(p.creationInstant) as day, MONTH(p.creationInstant) as month,
YEAR(p.creationInstant) as year, count(p.id) as users
FROM person p WHERE p.enrollmentStatus ="Enrolled"
GROUP BY year, month, day
ORDER BY year, month, day
This will give me the following output:
day month year users
1 1 2013 3
2 1 2013 5
3 1 2013 7
...
Now I'd like to have a 4th column that sums up the users:
day month year users **totalUsers**
1 1 2013 3 3
2 1 2013 5 8
3 1 2013 7 15
...
But somehow I can't figure it out how to do it with SQL (dbms: mySQL).
So that's how my statement looks after the hint to the 'running totals' question and it works like a charm:
SET #runtot:=0;
SELECT q1.day, q1.month, q1.year, q1.users,
(#runtot := #runtot + q1.users) AS totalUsers
FROM (
SELECT DAY(p.creationInstant) as day, MONTH(p.creationInstant) as month,
YEAR(p.creationInstant) as year, count(p.id) as users
FROM PERSON p where p.enrollmentStatus ="Enrolled"
GROUP BY year, month, day
ORDER BY year, month, day) as q1
select day,
month,
year,
(SELECT sum(a.id)
FROM person a
WHERE a.enrollmentStatus = "Enrolled"
and DAY(a.creationInstant) <= b.day)
from (SELECT DAY(p.creationInstant) as day,
MONTH(p.creationInstant) as month,
YEAR(p.creationInstant) as year,
count(p.id) as users
FROM person p
WHERE p.enrollmentStatus = "Enrolled"
GROUP BY year, month, day
ORDER BY year, month, day) b