I have a table that stores the date and price of purchases.
For example
date | price
---------------------
2014-1-12 | 6.50
2014-2-34 | 10.99
2015-1-01 | 3.22
2015-2-14 | 4.12
And so on.
What I want to achieve: A query that outputs the SUM of the purchases grouped per month of the year.
The IMPORTANT thing is, though, that I need to have the different years in COLUMNS to be able to make a graph with a separate line for each year. So the output I need is this:
MONTH | 2014 | 2015
JAN | 123.23 | 99.1
FEB | 457.00 | 122.00
MAR | 299.99 | 789.12
... |
NOV | 333.33 | 10.99
DEC | 100.00 | 20.10
Is this even possible? I searched quite a long time for things like "year on year" query etc. But I could not find anything.
Any help is greatly appreciated!
Just use conditional aggregation:
select monthname(date) as mon,
sum(case when year(date) = 2014 then price else 0 end) as price_2014,
sum(case when year(date) = 2015 then price else 0 end) as price_2015
from table t
group by monthname(date)
order by max(month(date));
Related
This question already has answers here:
mySQL Query for summing amount in columns (months)
(3 answers)
Closed 2 years ago.
I'm trying to get an output such as:
Year | January | February | March | April | May | June | July | August | September | October | November | December
Year | January | February | March | April | May | June | July | August | September | October | November | December
..and so on.
Each month column should have its own SUM(price) for that Month and the Year in question. Wherever comes blank (for example, maybe 2020 March is empty) should be blank/zero.
Query so far:
SELECT Year(date) AS y, monthname(date) AS m, SUM(price) AS p
FROM orders GROUP BY monthname(date), Year(date) ORDER BY date
Its returning as this:
2020 January someSum
2020 February anotherSum
...etc
Any way to make it return the results as above left to right rather than up-down or am I just that tired and need to go to sleep? I feel I'm confusing something horribly. Someone clarify it for me or chastise me mightly.
You can use conditional aggregation:
select
year(date) year_date,
sum(case when month(date) = 1 then price end) January,
sum(case when month(date) = 2 then price end) February,
sum(case when month(date) = 3 then price end) March,
...
from orders
group by year(date)
order by year_date
I need to find the difference between the points grouping by Id column.
Id | Year | Points
---+------+-------
1 | 2017 | 10
1 | 2018 | 20
2 | 2017 | 13
2 | 2018 | 16
3 | 2017 | 25
3 | 2018 | 20
Expected result:
Id | Points
---+-------
1 | 10
2 | 3
3 | -5
do aggregation
select
id,sum(case when year = 2018 then points end) -sum(case when year = 2017 then points end) as diff
from tablename group by id
If you want the difference between the years, you don't need group by:
select t2017.id, t2017.points as points_2017,
t2018.points as points_2018,
(t2018.points - t2017.points) as diff
from t t2017 join
t t2018
on t2017.id = t2018.id and
t2017.year = 2017 and
t2018.year = 2018;
You can do something very similar with conditional aggregation:
select id,
sum(case when year = 2017 then points end) as points_2017,
sum(case when year = 2018 then points end) as points_2018,
(sum(case when year = 2018 then points end) -
sum(case when year = 2017 then points end)
) as diff
from t
group by id;
SELECT *,
points - LAG(points) OVER ( PARTITION BY id
ORDER BY year ) delta_to_prev
FROM sourcetable
PS. Needs MySQL 8+.
I am saving the reporting date as date format (ex 2017-04-05) now I want the records only between two years based on reporting date, I am giving the from year and two year as 2016 and 2017
See My table structure
report_id | report_date | user_id | amount
1 | 2015-11-12 | 12 | 5000
2 | 2016-12-01 | 17 | 5000
3 | 2017-01-12 | 21 | 5000
4 | 2017-02-12 | 07 | 5000
The above table is my table structure
Now I want to get the records where report_date between year 2016 and 2017, I will not give complete date only I will give the year.
You can use YEAR() to get just the year of a date, then you can search between years like so:
SELECT *
FROM reports
WHERE YEAR(report_date) BETWEEN 2015 AND 2016;
I want to develop code that will allow a subset within a query. I have three fields "batchid", "month" and "year". Each batch may have several months and more than one year. The final order I need is the highest month year combination.
The following table I hope illustrates this.
Batch Month Year
5 12 2013
1 2014
6 11 2013
3 2014
4 1 2014
2 2014
The required order is
Batch Month Year
5 12 2013
1 2014
4 1 2014
2 2014
6 11 2013
3 2014
You can see each batch is sorted to the latest date in the batch and each batch is ordered to the latest date in the batch.
I have got it as far as the year is concerned but cannot figure out the month.
The first statement determines the lowest and highest dates.
I am new to this forum and for that matter not experienced using VBA and have not beanpole to get the SQL statement into this post so I apologize na hope this may make sense.
SELECT t1.batch, t1.month, t1.year
FROM tmp t1
JOIN
(SELECT batch, max(year*12+month) mord FROM tmp GROUP BY batch ORDER BY mord) t2
ON t2.batch = t1.batch
ORDER BY t2.mord, t1.year, t1.month
yields
+-------+-------+------+
| batch | month | year |
+-------+-------+------+
| 5 | 12 | 2013 |
| 5 | 1 | 2014 |
| 4 | 1 | 2014 |
| 4 | 2 | 2014 |
| 6 | 11 | 2013 |
| 6 | 3 | 2014 |
+-------+-------+------+
Assuming I have a table like the following:
id | assignment | duedate
1 | Math | 2012-01-01
2 | History | 2012-02-02
3 | Science | 2012-01-01
4 | Government | 2012-02-01
5 | Government | 2013-01-13
6 | History | 2013-03-13
Is it possible to make some sql query such that I get a grouping of all the dates by month and year? Is there some possibility that I could get a sorted result of:
duedatemonth | count
January 2012 | 2
Feburary 2012 | 2
January 2013 | 1
March 2013 | 1
I know you can GROUP BY duedate, but that only groups those with the same month, day, and year instead of just month and year.
Would it be then possible to even further group it such that it factors in "assignment" to obtain a resulting table of
id | duedatemonth | count
1 | January 2012 | 2
3 | January 2012 | 2
2 | Feburary 2012 | 2
4 | Feburary 2012 | 2
5 | January 2013 | 1
6 | March 2013 | 1
try this
SELECT DATE_FORMAT(duedate,'%M %Y') duedatemonth, COUNT(*) count
FROM Table1
GROUP BY year(duedate), MONTH(duedate)
DEMO HERE
will output this:
DUEDATEMONTH COUNT
January 2012 2
February 2012 2
January 2013 1
March 2013 1
Use the string functions YEAR and MONTH.
SELECT YEAR(duedate), MONTH(duedate), COUNT(*)
FROM sparkles
GROUP BY YEAR(duedate), MONTH(duedate)
Use MONTHNAME or DATE_FORMAT to get the name of the month.
You can use this query.
SELECT DATE_FORMAT(duedate, '%M %Y') duedatemonth, COUNT(1) `count`
FROM Tbl
GROUP BY DATE_FORMAT(duedate, '%M %Y')