Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL to get previous year record

Tags:

sql

mysql

I want a MySQL query to fetch previous year records. I already wrote a query to fetch current year records but I want previous year record also. There is a column called "date_created" based upon this date I have to fetch the status of the meterial.

SELECT material_status, COUNT(*) c
FROM purchase_order
WHERE YEAR(date_created) = YEAR(CURDATE()) AND material_status='open'; 
like image 334
Ranjan Avatar asked Jul 23 '16 05:07

Ranjan


People also ask

How do I get the previous record in MySQL?

You can use UNION to get the previous and next record in MySQL. Insert some records in the table using insert command. Display all records from the table using select statement.

How do I get this year data in MySQL?

Use the YEAR() function to retrieve the year value from a date/datetime/timestamp column in MySQL. This function takes only one argument – a date or date and time. This can be the name of a date/datetime/timestamp column or an expression returning one of those data types.

How do I get last 7 days record in SQL?

Here's the SQL query to get records from last 7 days in MySQL. In the above query we select those records where order_date falls after a past interval of 7 days. We use system function now() to get the latest datetime value, and INTERVAL clause to calculate a date 7 days in the past.

How do I get last 6 months records in SQL?

Instead of approximating the "current" date by selecting the MAX(date) the code could reference CAST(GETDATE() as DATE) to access the system datetime and cast it as type DATE. where [date] > dateadd(month, -6, cast(getdate() as date));


1 Answers

to get last year data

SELECT material_status, COUNT(*) c
FROM purchase_order
WHERE YEAR(date_created) = YEAR(DATE_SUB(CURDATE(), INTERVAL 1 YEAR)) AND material_status='open'; 
like image 130
mohan111 Avatar answered Oct 09 '22 19:10

mohan111