SQL: Selecting current month last year - sql

I currently select data from the current month LAST year. This works fine with
SELECT cast(CREATED as date) AS DATE, COUNT([ID]) AS BOOKINGS, SUM([TOTCOST]) AS Sales
from BOOKING
WHERE
CREATED >= DATEADD(month, datediff(month, 0, getdate())-12, 0)
AND
CREATED < DATEADD(month, datediff(month, 0, getdate())-11, 0)
GROUP BY cast(CREATED as date)
ORDER BY cast(CREATED as date)
However, rather than return 1st to 31st July, I would like to return 2nd July 2014 to 1st August 2014 inclusive. So effectivly +1 day on that current month.
Is this possible?

Yes, simply add 1 to the result of your calculation.
SELECT cast(CREATED as date) AS DATE, COUNT([ID]) AS BOOKINGS, SUM([TOTCOST]) AS Sales
from BOOKING
WHERE
CREATED >= (DATEADD(month, datediff(month, 0, getdate())-12, 0) + 1)
AND
CREATED < (DATEADD(month, datediff(month, 0, getdate())-11, 0) + 1)
GROUP BY cast(CREATED as date)
ORDER BY cast(CREATED as date)

Related

Get records of last 2 months (current year), and last month(last year)

I need to get records of last 2 months and last month(last year) based on my table field paidDate, using SQL server 2016.
Suppose, I run the query on Feb 1st/2nd, 2020. I need the monthly data from December 2019, January 2020, as well as January 2019.
What's the SQL query for this? Is it possible to club all of these scenario into one?
Then for the previous 2 months the paidDate would be :
A) Higher or equal than the first day of 2 months ago
B) Lower than the first day of the current month.
Similar for the month of a year ago.
So try something like this:
SELECT *
FROM YourTable
WHERE
(
paidDate >= DATEADD(month, -2, DATEADD(month, DATEDIFF(month, 0, GetDate()), 0))
AND paidDate < DATEADD(month, DATEDIFF(month, 0, GetDate()), 0)
)
OR
(
paidDate >= DATEADD(month, -13, DATEADD(month, DATEDIFF(month, 0, GetDate()), 0))
AND paidDate < DATEADD(year, -1, DATEADD(month, DATEDIFF(month, 0, GetDate()), 0))
)
LukStorm has the better answer in terms of performance (and I've upvoted it). But if you want complete months and don't care about indexing, then I would suggest datediff():
where datediff(month, paiddate, getdate()) in (1, 2, 13)
This gets the complete months that are 1 month, 2 months, and 13 months in the past.
You can try the logic as below-
SELECT *
FROM your_table
WHERE
(
YEAR(paidDate) = YEAR(DATEADD(MM,-1, getdate()))
AND
MONTH(paidDate) = MONTH(DATEADD(MM,-1, getdate()))
)
OR
(
YEAR(paidDate) = YEAR(DATEADD(MM,-2, getdate()))
AND
MONTH(paidDate) = MONTH(DATEADD(MM,-2, getdate()))
)
OR
(
YEAR(paidDate) = YEAR(DATEADD(MM,-13, getdate()))
AND
MONTH(paidDate) = MONTH(DATEADD(MM,-13, getdate()))
)

Last Day of Each Month Calculation

I have a table X which holds data for each day for a brand. The data for each day is cumulative i.e. sales data for 3 will have data for 1, 2 and 3. Thus data for the last day of each month will be the sales for that month for that brand and company. I want to get the sum of all the sales for that brand for the last 3 months excluding the current month on the last day of each month.
i.e for March: I want sales from 31st Jan 2019 + 28th Feb 2019 + 31st Dec 2018 for each brand and company.
How can I achieve this?
if you are using MSSQL you can use EOMONTH function, example is as under
DECLARE #date VARCHAR(255) = '2/24/2019';
SELECT EOMONTH ( #date ) AS Result;
for MySQL you can use LAST_DAY function
SELECT LAST_DAY('2019-02-24');
Let's say name of your column representing the sales date is "sales_date", then the following predicate will give you the days you're interested in:
sales_date in (
dateadd(day, -1, dateadd(month, datediff(month, 0, getdate()) - 2, 0)),
dateadd(day, -1, dateadd(month, datediff(month, 0, getdate()) - 1, 0)),
dateadd(day, -1, dateadd(month, datediff(month, 0, getdate()), 0))
)

sql get data where date is greater than current month

i want to retrieve records where a date field is set to future months
does this look correct
Select * from table1 WHERE
datesetto >MONTH(dateadd(dd, -1, GetDate())))
select * from tablename where month(columndate)>month(getdate()) and
year(columndate)>=year(getdate())
SELECT * FROM table1
WHERE datesetto >= DATEADD(month, DATEDIFF(month, 0, getdate())+1, 0)
Explanation:
DATEDIFF(month, 0, getdate()) calculates how many months have passed since 1900-01-01.
DATEADD(month, DATEDIFF(month, 0, getdate()), 0) returns the beginning of this month.
DATEADD(month, DATEDIFF(month, 0, getdate())+1, 0) returns the beginning of next month.
Try this:
Select * from table1 WHERE
((DATEPART(MONTH,datesetto) > DATEPART(MONTH,GETDATE())
AND DATEPART(YEAR,datesetto) = DATEPART(YEAR,GETDATE()))
OR (DATEPART(YEAR,datesetto) > DATEPART(YEAR,GETDATE())))
DATEPART(Month,GETDATE()) will give the month of the current date and then you can compare it with the datesetto
Update: The above query will give data for any months greater than the current month and any month greater in the year than the current year.

First date of current Fiscal Year

I am trying to get the First Date of the current Fiscal year. In my case the Fiscal year starts in Oct.
Example: I need the 10/01/2015
I would generally use the below query to the get the first day of the current year but how do I change it to get the first date of fiscal year?
select
convert(varchar(12), (
DateAdd(month, (
Month(getdate()) - 1) * -1,
DateAdd(Day, (
Day(getdate()) - 1) * -1,
getdate()))),
103) as StartYear
You can apply following logic: Subtract 9 months from the current date, find Jan 1st of this year and add 9 months again:
DATEADD(MONTH, 9, DATEADD(YEAR, DATEDIFF(YEAR, 0, DATEADD(MONTH, -9, GETDATE())), 0))
declare #fiscal date = '2015-10-01'
; with dates
as
(
select date = '2015-09-30' union all
select date = '2015-10-01' union all
select date = '2016-09-30' union all
select date = '2016-10-01'
)
select *,
fiscal = case when date < dateadd(year, year(date) - year(#fiscal), #fiscal)
then dateadd(year, year(date) - year(#fiscal) - 1, #fiscal)
else dateadd(year, year(date) - year(#fiscal), #fiscal)
end
from dates
/* result
date fiscal
---------- ----------
2015-09-30 2014-10-01
2015-10-01 2015-10-01
2016-09-30 2015-10-01
2016-10-01 2016-10-01
*/
At first Subtract 9 months from current date to get the previous year(your Fasical year).
Then take the year difference with 0 to your date.
Now add the year difference with 0 to get the starting of a month.
Finally you need to add the 9 months to get the starting month of a Fasical Year.
As year example the require format "10/01/2015". So get that you can use convert function with 101.
select convert(varchar(12), DATEADD(MONTH, 9, DATEADD(YEAR, DATEDIFF(YEAR, 0, DATEADD(MONTH, -9, GETDATE())), 0)), 101) as StartYear

Does SQL automatically calculate month to month start and end dates?

I am trying to get total amount of sales sold in 1 month. For example, from 4/1/15 - 4/30/15.
I am getting a span of 3/30/15 - 4/30/15.
Here is my SQL:
SELECT Customer.custno
, Customer.enteredDate AS 'Date Entered'
, COUNT(BasicPolInfo.polid) AS 'Number of Policies'
, SUM(COUNT(BasicPolInfo.polid)) OVER (ORDER BY Customer.custno ROWS
BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) AS TotalAmount
FROM Customer
INNER JOIN BasicPolInfo ON Customer.custid = BasicPolInfo.custid
WHERE BasicPolInfo.polid IS NOT NULL
AND Customer.firstname IS NOT NULL
AND Customer.enteredDate BETWEEN DATEADD(MONTH, -1, GETDATE()) AND
DATEADD(MONTH, 0, GETDATE())
GROUP BY Customer.custno
, Customer.firstname
, Customer.lastname
, Customer.entereddate
ORDER BY Customer.enteredDate ASC
The results I am getting are from the dates 2015-04-30 to 2015-03-30. I am trying to get 2015-04-30 to 2015-04-01.
And for the next month to also be from 2015-05-31 to 2015-05-01
Any help would be appreciated! Thank you!
Since you are on 2012 you can use EOMONTH()
WHERE Customer.enteredDate >= DATEADD(DAY, 1, EOMONTH(GETDATE(), -1))
AND < DATEADD(DAY, 1, EOMONTH(GETDATE()))
or you could match the month and year only.
where month(Customer.enteredDate) = month(getDate())
and year(Customer.enteredDate) = year(getDate())
Customer.enteredDate >= DATEADD(MONTH, DATEDIFF(MONTH, 0, current_timestamp), 0)
AND Customer.enteredDate < DATEADD(MONTH, DATEDIFF(MONTH, 0, current_timestamp) + 1, 0)