I have a table wherein I want to get the total prices of a column in specific dates. The dates are strings (eg: January 2018). What I have tried is to convert the string to date.
SELECT
SUM(price_amount) as 'total_paid',
STR_TO_DATE(CONCAT('01 ',price_month), "%M %d %Y") as dates
FROM user_prices
WHERE price_type = 'cash' AND price_month >= DATE('January 01 2018')
GROUP BY price_month
ORDER BY price_month DESC;
The result of this is null. I have to get only the sum of each existing month in the table that dates are equal or greater January 01 2018. And also group them by month.
email price_amount price_month price_type date_added
1#gmail.com 200 April 2017 cash ---
19#gmail.com 400 December 2017 cash ---
12#gmail.com 100 January 2018 cash ---
123#gmail.com 230 January 2018 cash ---
1234#gmail.com 250 January 2018 credit ---
321#gmail.com 200 April 2018 cash ---
32#gmail.com 120 March 2018 cash ---
So the example above should show the expected result below:
price_month total_paid
March 2018 120
April 2018 200
January 2018 330
If the price_month column is actually literal text as you have shown us, then you'll need to use STR_TO_DATE to make this query work. Note that STR_TO_DATE requires at least year, month, and day information in order to generate a date. So, in the query below, I arbitrarily build each month year data as occurring on the first of the month.
SELECT
price_month,
SUM(price_amount) AS total_paid
FROM user_prices
WHERE
STR_TO_DATE(CONCAT('01 ', price_month), '%d %M %Y') >= '2018-01-01' AND
price_type = 'cash'
GROUP BY
price_month,
STR_TO_DATE(CONCAT('01 ', price_month), '%d %M %Y')
ORDER BY
STR_TO_DATE(CONCAT('01 ', price_month), '%d %M %Y');
Demo
Moving forward, please don't store your dates as text like this. In general, you may assume that a puppy gets run over every time you have to use STR_TO_DATE in a MySQL query.
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)
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)
Experts ,
What is best way to get last 13 months name inducing current month. In the table I have records for every month from 2014.
Note : date format is - 2016-01-15 , each month has multiple records in table.
something like below
SELECT MONTHNAME(start_date) as month_name
FROM purchase_order_entry_reports
WHERE start_date (needs to select last 13 months )
Result i'm looking for
November,
December,
January,
February,
March,
April,
May,
June,
July,
August,
September,
October,
November
SELECT distinct YEAR(start_date), MONTHNAME(start_date) as month_name
FROM purchase_order_entry_reports
WHERE start_date BETWEEN now() - interval 13 month
AND now()
I have a table with a column named timestamp
i want to be able to order by month(timestamp) and year(timestamp) and group the months and years together
so for example, if i had the following timestamps:
2014-01-01
2014-02-01
2014-05-01
2015-01-01
i want to show in this order
MONTH YEAR
1 2015
5 2014
2 2014
1 2014
You can pick month and year from timestamp with MONTH and YEAR:
GROUP BY MONTH(field_with_ts) , YEAR(field_with_ts)
and the same thing with the ORDER BY clause.
Try this:
substring(date, 8 , 2) as Month, substring(date, 1, 3) as Year
See, if that works.
Sorry to bother you again, i have difficulties in formulating my query . i have two columns with a date values. (see sample below)
Posting Date col B
Feb 02,2013 feb 01, 2013
Feb 02, 2013
feb 15, 2013
mar 03,2013 mar 01, 2013
april 12, 2013 april 12, 2013
if my parameter is a range of date based on col B (ex. where colB between 02/01/2013 and 02/28/2013).
I want to show all value in Posting date which is part of the date range i had filtered. say, having month of Feb and 2013 as year
results:
Posting Date col B
Feb 02,2013 feb 01, 2013
Feb 02, 2013
feb 15, 2013
Is this what you're looking for? Is your field type of your Posting_Date column a Date? If so, then this should work:
SELECT Posting_Date, ColB
FROM YourTable
WHERE Posting_Date >= '2013-02-01'
AND Posting_Date < '2013-03-01'
I prefer using >= and < rather than BETWEEN.
If your Posting_Date field is stored as a varchar, then use STR_TO_DATE:
SELECT Posting_Date, ColB
FROM YourTable
WHERE STR_TO_DATE(Posting_Date , '%M %d,%Y') >= '2013-02-01'
AND STR_TO_DATE(Posting_Date , '%M %d,%Y') < '2013-03-01'
SQL Fiddle Demo