I'm new to MySQL and need your help to figure out a query so I can calculate the average of each quarter.
I have a table called USretail92_21 that looks like this (from 1992 to 2021):
Date
Sales
1992-01-01
701.0
1992-02-01
658.0
1992-03-01
731.0
1992-04-01
816.0
1992-05-01
856.0
1992-06-01
853.0
1992-07-01
101.0
1992-08-01
558.0
1992-09-01
431.0
Consider the date format 1992-01-01 means Jan. 1992. Now I run the below query to get the quarter and month:
select year(date) as Year,monthname(date)as Month, quarter(date) as Quarter, sales from USretail92_21 where kind="Men's clothing stores" order by 1
and that gives me this view:
Year
Month
Quarter
Sales
1992
January
1
701.0
1992
February
1
658.0
1992
March
1
731.0
1992
April
2
816.0
1992
May
2
856.0
1992
June
2
853.0
Now my question to you is how can I get the average sales per quarter and have an output that looks like this:
Quarter
Year
AverageSales
1
1992
696 (average for Jan/Feb/March)
2
1992
841
eventually, I want to have a graph with Python to see sales as Y and "Q1_92 to Q4_21" as X axis
You need to use GROUP BY to calculate aggregates like sums and averages.
Working from your example:
WITH SalesPerMonth AS (
select year(date) as Year,
monthname(date)as Month,
quarter(date) as Quarter,
sales from USretail92_21
where kind="Men's clothing stores"
)
SELECT Quarter, Year, AVG(Sales) AS AverageSales
FROM SalesPerMonth
GROUP BY Quarter, Year
Or alternatively do it all at once:
select year(date) as Year,
quarter(date) as Quarter,
AVG(sales) AverageSales
from USretail92_21
where kind="Men's clothing stores"
group by year(date),
quarter(date)
Related
i have some records on mysql db, i tried to group by month, data is correct but time of March month is showing as "2022-03-22", i required as same as other months like 2022-03-01.
time month_name inc_number
2022-01-04 19:58:09 January 39393
2022-02-08 17:36:33 February 90203
2022-03-22 13:40:48 March 82923
2022-04-01 00:14:33 April 23333
2022-05-01 00:31:58 May 33322
2022-06-06 17:21:29 June 33244
2022-07-01 04:19:20 July 90283
2022-08-01 00:07:04 August 8428
2022-09-01 09:40:15 September 10097
2022-10-01 00:30:19 October 6421
2021-12-01 07:12:30 December 8521
the query im using is below
SELECT
created_on as 'time',
MONTHNAME(created_on) AS 'month_name',
count(distinct id_number) AS "inc_number"
FROM test_reports
WHERE
MONTH(created_on)
GROUP BY MONTH(created_on)
ORDER BY MONTH(created_on)
Please suggest the way to get all time should be first date of each month.
If you use GROUP BY and do not specify a column, MySQL will just use one of the created_on values from the 'array'.
Instead you should define the expected output of created_on and add it to your GROUP BY
You could use something like DATE_FORMAT(created_on, '%Y-%m-01') to always display the first day of that month
working, thanks #verhie
SELECT
created_on as 'time',
MONTHNAME(created_on) AS 'month_name',
count(distinct id_number) AS "inc_number"
FROM test_reports
WHERE
MONTH(created_on)
GROUP BY DATE_FORMAT(created_on, '%Y-%m-01')
ORDER BY MONTH(created_on)
How can we calculate the month on month cumulative retention rate in SQL
Bill_Date Customer_id
2021-01-01 1
2021-01-23 2
2021-01-29 3
2021-02-17 1
2021-02-19 2
2021-03-01 3
Retention Rate= (Total Number of unique Customers in present Month)/(Total Number of Customers in previous Months)
Expected Output
January : 100%
February : 66.7%
March : 25%
February =(Unique customers in feb)/((Unique customers in jan)
March=(Unique customers in march)/((Unique customers in jan)+(Unique
customers in feb)
Consider a window function for cumulative sum of unique customer per year/month:
WITH sub AS (
SELECT YEAR(c.Bill_Date) AS bill_year,
MONTH(c.Bill_Date) AS bill_month,
COUNT(DISTINCT c.customer_id) AS unq_customers
FROM customer_bills c
GROUP BY YEAR(c.Bill_Date),
MONTH(c.Bill_Date)
)
SELECT bill_year,
bill_month,
unq_customers,
IFNULL(
unq_customers /
(SUM(unq_customers) OVER(ORDER BY bill_month) -
unq_customers),
1
) * 100 AS retention_rate
FROM sub
Online Demo
I have two queries that give respectively the number of working unit bought, and the number of working unit consumed by a client.
I am working on a SQL Server 2014
The WUBought query returns something like this example :
Customer Year Month UnitBought
Cust1 2015 6 50
Cust2 2014 7 100
Cust1 2013 10 30
Cust3 2015 2 40
The other query returns the number that were consumed by a client :
Customer Year Month UnitConsumed
Cust1 2015 2 6
Cust1 2015 5 20
Cust2 2015 3 8
Cust1 2015 4 3
Cust3 2015 2 10
What I am basically trying to do, is a sum of what has been bought for every month, minus what has been consumed. Here is an example of what I want as a result for the first six months for Cust1 :
Customer Year Month Remaining
Cust1 2015 1 30
Cust1 2015 2 24
Cust2 2015 3 24
Cust1 2015 4 21
Cust3 2015 5 1
Cust3 2015 6 51
The query that returns the WU bought with a UNION ALL from a table that lists every month, to get each month even if there is no value :
SELECT Customer, [Year], [Month], SUM(UOBought) AS UORest
FROM WU_Bought
GROUP BY [Customer], [PurchaseDate]
UNION ALL
SELECT '' AS Customer, [Year], [Month], '' AS UORest
FROM Months
GROUP BY [Year], [Month]
Here is the query that sums every bought unit every month, with the same union statement :
SELECT Customer, [Year], [Month], SUM(TotalConsumed) * -1 AS UORest
FROM WUConsumed
GROUP BY Customer, Year, Month
UNION ALL
SELECT '' AS Customer, [Year], [Month], '' AS UORest
FROM EveryMonths
GROUP BY Year, Month
Right now I think I must adjust the first one, forcing it to keep the previous sum, but I am not sure how I can do that.
Does this work for you?
SELECT b.customer_id, b.year, b.month, SUM(b.units_bought) AS units_bought, ISNULL(c.units_consumed,0) AS units_consumed, SUM(b.units_bought) - ISNULL(c.units_consumed,0) AS units_remaining
FROM Bought b
LEFT JOIN Consumed c
ON b.customer_id = c.customer_id AND b.year = c.year AND b.month = c.month
GROUP BY b.customer_id, b.year, b.month
Ok, I got it working.
What I did was really "simple", using a SQL Server feature, available since 2012 :
ROWS UNBOUNDED PRECEDING
Here is a pretty clear article about this feature.
I created an other view grouping the results from the queries about consumed and bought units with a UNION ALL clause, called "WU_Closing_View", then used the ROWS UNBOUNDED PRECEDING within it :
SELECT Customer, Year, Month, SUM(Closing) OVER(PARTITION BY Customer ORDER BY Year, Month ROWS UNBOUNDED PRECEDING) AS Closing
FROM WU_Closing_View
GROUP BY Customer, Year, Month, Closing
UNION ALL
SELECT '' AS Customer, Year, Month, '' AS Sum_bought
FROM Months
GROUP BY Year, Month
ORDER BY Customer, Year, Month
Note that I used PARTITION BY, in order to sum by client. Because I wanted to show every month in a SSRS matrix, I added a "UNION ALL" pointing to a table that has every year and month on it for an empty client, from 2010 to 2017. But it is optional if you don't need the evolution for every month.
There may be an easier way, but that's the one I found so far.
I have the following database table
"id","date_occurred","country","town","quantity"
"1","2012-06-01","England","Andover","82"
"2","2012-06-01","England","Colchester","569"
"3","2012-06-01","England","Farnham","1"
"4","2012-06-01","England","England","4"
"5","2012-06-01","England","America","13"
"6","2012-06-01","America","England","114"
"7","2012-06-02","England","Andover","4"
"8","2012-06-02","England","Colchester","207"
"9","2012-06-02","America","England","14"
"10","2012-06-03","England","Andover","3"
"11","2012-06-03","England","Colchester","72"
"12","2012-06-03","England","America","1"
"13","2012-06-03","America","England","15"
"14","2012-07-04","England","Andover","1"
"15","2012-07-04","England","Colchester","309"
"16","2012-07-04","England","America","4"
"17","2012-07-04","America","England","11"
"18","2012-08-05","England","Andover","2"
"19","2012-08-05","England","Colchester","319"
"20","2012-08-05","England","Farnham","1"
"21","2012-08-05","England","America","4"
"22","2012-08-05","America","England","25"
"23","2012-08-06","England","Andover","93"
"24","2013-06-06","England","Colchester","542"
"25","2013-06-06","England","Farnham","1"
"26","2013-06-06","England","England","4"
"27","2013-06-06","England","America","7"
"28","2013-06-06","America","England","115"
I would like to produce the following output from a query
Total sales per day per country for a given month
2012-06-01 England 669
2012-06-01 America 114
2012-06-02 England 211
Total sales per day per town for a given month
2012-06-01 Andover 82
2012-06-02 Andover 4
I have been trying various queries with group by, sum, and count, but can't get the correct output.
any simple solutions or guidance welcome. Thanks in advance
Try this:
Total sales per day per country for a given month:
SELECT date_occurred, country, SUM(quantity)
FROM tableA
WHERE YEAR(date_occurred) = 2013 AND MONTH(date_occurred) = 6
GROUP BY date_occurred, country
Total sales per day per town for a given month:
SELECT date_occurred, town, SUM(quantity)
FROM tableA
WHERE YEAR(date_occurred) = 2013 AND MONTH(date_occurred) = 6
GROUP BY date_occurred, town
I have the following 2 tables
id km date
1 22.1 2012-04-15
2 52.1 2012-04-14
3 72.1 2012-03-15
4 54.1 2012-03-14
and
id lt date
1 16.4 2012-04-03
2 22.6 2012-04-29
3 45.9 2012-03-2
4 13.1 2012-03-31
From this tables I need to get a rate by month, I mean, the number of km divided by number of lt in a month. I know I can get the sum of km by month using aggregate functions in a query and in other query the sum of lt, something like this.
SELECT SUM(km) FROM kilometers GROUP BY MONTH(date)
SELECT SUM(lt) FROM gasoline GROUP BY MONTH(date)
Then I could manually divide km/lt for each grouped month. So the question is, can I do that on a simple query? Do I have to change the structure of my database?
Any help would be appreciated, thanks.
You should be able to just join the table data together by the month/year. You would just need to handle the situation where lt is 0 or null for a month. Note that you will also want to group by YEAR so it handles multi-year data appropriately:
SELECT k.year, k.month, k.km / g.lt
FROM (SELECT YEAR(date) as year, MONTH(date) as month, SUM(km) as km
FROM kilometers GROUP BY YEAR(date), MONTH(date)) k
JOIN (SELECT YEAR(date) as year, MONTH(date) as month, SUM(lt) as lt
FROM gasoline GROUP BY YEAR(date), MONTH(date)) g
ON k.month = g.month AND k.year = g.year
Sample output:
YEAR MONTH K.KM / G.LT
---- ----- --------------
2012 3 2.138982929974
2012 4 1.90256407322
Demo: http://www.sqlfiddle.com/#!2/c2942/6