I really need your help on a mysql query where I am not getting any clue.
Say I have a table with two columns:
entryDate - datetime
Recruits - int
I am trying to building a mysql query to get the output like the image. Please share the query.
You can do this with conditional aggregation and arithmetic:
select monthname(entryDate) as mon,
sum(case when year(entryDate) = 2016 then recruits else 0 end) as recruits_2016,
sum(case when year(entryDate) = 2017 then recruits else 0 end) as recruits_2017,
(sum(case when year(entryDate) = 2017 then recruits end) /
sum(case when year(entryDate) = 2017 then recruits end)
) - 1 as change
from t
where entryDate between '2016-01-01' and entryDate < '2018-01-01'
group by monthname(entryDate)
order by min(entryDate);
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
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;
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.
I have a table (myItems) with an item "id" and "date". Now i want to read out, how many items there are per month (i also want to distinguish between October 2013 and October 2014).
I started with:
SELECT Count(okt.id) AS Oktober, Count(nov.id) AS November
FROM `myItems` as okt,
`myItems` as nov
WHERE (okt.date between '2013-10-01' and '2013-10-31')
OR (nov.date between '2013-11-01' and '2013-11-30')
But it prints out a ridiculously large number. What am i doing wrong?
Try this. This will divide data into months and then do the COUNT :
SELECT SUM(CASE WHEN MONTH(date) = 10 THEN 1 ELSE 0 END) AS Oktober
, SUM(CASE WHEN MONTH(date) = 11 THEN 1 ELSE 0 END) AS November
FROM `myItems`
Demo: SQL Fiddle
With YEAR integrated:
SELECT 2013 as Year
, SUM(CASE WHEN MONTH(date) = 10 THEN 1 ELSE 0 END) AS Oktober
, SUM(CASE WHEN MONTH(date) = 11 THEN 1 ELSE 0 END) AS November
FROM `myItems`
WHERE YEAR(date) = 2013
UNION ALL
SELECT 2014 as Year
, SUM(CASE WHEN MONTH(date) = 10 THEN 1 ELSE 0 END) AS Oktober
, SUM(CASE WHEN MONTH(date) = 11 THEN 1 ELSE 0 END) AS November
FROM `myItems`
WHERE YEAR(date) = 2014;
;
Demo: Fiddle
With inspiration from #user77318
SELECT YEAR(date) as Year, month(date) as month, count(id) as count
FROM myItems
GROUP BY YEAR(date), MONTH(date);
I personally recommend this, more beautiful. Then you can do all the presentation stuffs on Application Layer.
Try this. Group the result by month:
SELECT month(date) as month, count(id) as count FROM myItems WHERE date between '2013-10-01' and '2013-11-30' GROUP BY MONTH(date);
Example of output result:
Month | Count
10 | 100
11 | 200
Probably you are not using join, you are just giving the conditions. First try to join these 2 tables with the primary keys or with some unique and common data column. Then try to execute above query. Somthiong like this:-
SELECT Count(okt.id) AS Oktober, Count(nov.id) AS November
FROM `myItems` as okt,
`myItems` as nov
WHERE okt.id = nov.id
AND (okt.date between '2013-10-01' and '2013-10-31')
OR (nov.date between '2013-11-01' and '2013-11-30');
May be this is helpful for you.
I have a database that looks something like this:
Year Month New Visitor?
2011 Jan Yes
2011 Jan No
2012 Feb No
2012 Feb No
2012 Feb Maybe
I'd like Yes's and No's to be separate columns, per month so I can print it as a chart.I understand that to do this I'll need a result like this:
Year Month Yes No Maybe
2011 Jan 1 1 0
2012 Feb 0 2 1
How might I go about this using only MySQL?
Try:
SELECT `Year`, `Month`,
COUNT(IF (`New Visitor?` = 'Yes', 1, NULL)) AS `Yes`,
COUNT(IF (`New Visitor?` = 'No', 1, NULL)) AS `No`,
COUNT(IF (`New Visitor?` = 'Maybe', 1, NULL)) AS `Maybe`
FROM `table`
GROUP BY `Year`, `Month`;
You can use the following (See SQL Fiddle with demo):
select year, month,
sum(case when newVisitor = 'yes' then 1 else 0 end) yes,
sum(case when newVisitor = 'No' then 1 else 0 end) no,
sum(case when newVisitor = 'Maybe' then 1 else 0 end) maybe
from yourtable
group by year, month