SQL Query for Getting Data for Last 3 Months

SQL query for getting data for last 3 months

SELECT * 
FROM TABLE_NAME
WHERE Date_Column >= DATEADD(MONTH, -3, GETDATE())

Mureinik's suggested method will return the same results, but doing it this way your query can benefit from any indexes on Date_Column.

or you can check against last 90 days.

SELECT * 
FROM TABLE_NAME
WHERE Date_Column >= DATEADD(DAY, -90, GETDATE())

How to get data of last 3 months from sql table

You can try like this, Put your appropriate date column in this query.

SELECT * 
FROM TABLE_NAME
WHERE DATEADD(MONTH, -3, GETDATE()) between txtFromDate and txtToDate

you can check against last 90 days.

SELECT * 
FROM TABLE_NAME
WHERE DATEADD(DAY, -90, GETDATE()) between txtFromDate and txtToDate

Filtering data for the last 3 months using DATEADD() in SQL

Thanks to @VvdL for their comment.

PostgreSQL uses a different command. Changed to below and works.

WHERE my_date >= current_date - INTERVAL '3 months'

SQL query to Group last 3 months of data by individual months and counts

SELECT FromAddress,
SUM( CASE WHEN MONTH(SendDate) = 11 AND YEAR(SendDate) = 2015 THEN 1 ELSE 0 END ) AS Nov2015,
SUM( CASE WHEN MONTH(SendDate) = 12 AND YEAR(SendDate) = 2015 THEN 1 ELSE 0 END ) AS Dec2015,
SUM( CASE WHEN MONTH(SendDate) = 1 AND YEAR(SendDate) = 2016 THEN 1 ELSE 0 END ) AS Jan2016
FROM tableName
GROUP BY FromAddress

I have used MONTH() and YEAR() functions in SQL Server to extract month and year from SendDate column. You can use the respective functions provided in the DBMS you are using in above query.

To make it dynamic you can use DATEDIFF() function as below

SELECT FromAddress,
SUM( CASE WHEN DATEDIFF(MONTH,SendDate,GETDATE()) = 3 THEN 1 ELSE 0 END ) ,
SUM( CASE WHEN DATEDIFF(MONTH,SendDate,GETDATE()) = 2 THEN 1 ELSE 0 END ) ,
SUM( CASE WHEN DATEDIFF(MONTH,SendDate,GETDATE()) = 1 THEN 1 ELSE 0 END )
FROM tableName
GROUP BY FromAddress

PostgreSQL: Select only past three months of data

As documented in the manual, there is no datediff() function in Postgres. You just subtract an interval:

select *
from cte_data
where my_timestamp >= current_timestamp - interval '3 month';

SQL query for getting data for the last 6 months grouped by month?

EDIT:
Over the hours I made a few corrections. Most notably I corrected the self join query to reflect my intentions and also added more details to better explain what is going on.

To my knowledge there are two ways about it (which are probably the same under the hood).

Also, please note that these solutions assume you have a month field already in place. If you have a date or timestamp field, you should take one extra preparation step.

[Addendum] To be more precise, I'd say that the ideal would be to have a date/timestamp field that is truncated/flattened to the first day of the month.

As an example,











































monthamount
2021-01-0150
2021-02-0120
2021-03-0110
2021-04-01100
2021-05-0120
2021-06-0140
2021-07-0180
2021-08-0150

SQL Server query to get the data from previous month if the column value is not present in next month

This answers the original version of the question.

Assuming that month is stored as a date with the first first day of the month, then a simple method uses recursive CTEs:

with cte as (
select restaurant, num_items, month,
dateadd(month, -1,
coalesce(lead(month) over (partition by restaurant order by month),
max(month) over ()
)
) as end_month
from t
union all
select restaurant, num_items, dateadd(month, 1, month), end_month
from cte
where month < end_month
)
select *
from cte
order by restaurant, month;

Here is a db<>fiddle.



Related Topics



Leave a reply



Submit