SQL Query that rund monthly on a fixed day range - sql

I have an SQL query I need to run once a month.
The data set the query produces always has to be from the 11th of the month before to the 10th of the current month.
I now manualy run the query in the fews days after the 11th day of the month manually adjusting the date range in my where statement:
for example...
Where Column A is greater than 10/10/2015 and less than 12/11/15
I was hoping there would be a statement I could add to my query to automatically find the 11th day of the last month and the 10th of the current month. This way I could schedule the query and automatically email the results.

You should be able to use the following within your query: -
CONVERT(date,FORMAT(GETDATE(),'yyyy-MM')+'-10')
(for the 10th of this month)
and
CONVERT(date,FORMAT(DATEADD(m,-1,GETDATE()),'yyyy-MM')+'-11')
(for the 11th of last month).

Try to look out the MONTH() function in your working DBMS. In MySQL and MSSQL it returns a number (1 been january) corresponding to the current month that your system is (you may check if it's date is updated).
With this function you can subtract 1 to get the last month, having to do some logic when the current one is January, hence 1. Since now you should get 12 (december) intead of 0 (an error).
Cheers, mate!

Related

SQL if last day of pervious month has value change the beginning date of next month to start from that day

Need help with this sql code, I would like to roll back start day of new month if last day of pervious month has values.
Thanks
I would suggest nesting EOMONTH() in an IF statement.
Not sure what table(s) you're working with, but general layout would be:
IF(MAX(date_column) = EOMONTH(MAX(date_column), MAX(date_column), new_date)
Reference for EOMONTH():
https://www.sqlservertutorial.net/sql-server-date-functions/sql-server-eomonth-function/

How do I summarise month to date forecast for current month in Power Pivot?

I have a table in a data model that has forecast figures for the next 3 months. What I want to do is to show what the forecast number for the current month to date is.
When I use the DATESMTD function like this:
=CALCULATE(SUM(InternetSales_USD[SalesAmount_USD]),DATESMTD(DateTime[DateKey]))
I get the last month of my data summarised as a total. I assume that is because the DATESMTD function takes the last date in the column and that is 3 months away.
How do I make sure I get this current month MTD total rather then the end of the calendar? The formula should be clever enough to realise I am in May and want the May MTD not the August MTD.
Any ideas?
The way to do this is to do this:
Forecast_Transaction_MTD:=CALCULATE(sum('ATO Online'[2017 Transaction Forecast]), DATESINPERIOD('ATO Online'[Current Year],TODAY(),-day(TODAY()),day))
the last -day(TODAY()) gets the day number for the current day and subtract it from today's date. So, today is the 25 May. the -day(TODAY())),day)) extracts the day (25) and subtracts it from the current date to get me to the 1 May.
The rest of the formula just adds the total for the dates.

SQL Selecting A Range Of Data According To Sysdate

So I have this problem where I am trying to select a range of data for this sql that will run monthly. Basically I have this field Date_OF_Entry that records dates as 01-Jan-15
Now I will be running this script twice a month at the 16th to capture all the data from the 15th back to the 1st. I will also then run the script at the 1st of the next month to capture all data from the end of the month to the 16th.
Any help is appreciated.
What I am doing now.
Where DATE_OF_ENTRY > sysdate-16
ROUND(date,'MM') Rounds down to the 15th, and up from the 16th.
So WHERE ROUND(DATE_OF_ENTRY,'MM') = ROUND(sysdate-1,'MM') will - on the 16th - round sydate and the 1-15th down to the start of the month, and, on the 1st will round everything from the 16th up.
Be carefull though as on the 1st if any new records have been added where date_of_entry = the 1st, they would get bumped down into last month's end of month group.
So, WHERE ROUND(DATE_OF_ENTRY,'MM') = ROUND(sysdate-1,'MM') AND date_of_entry < trunc(sysdate) should clear that up.

how to summarize sql result, week and month wise in single query

I want to summarize sql query result week wise and month wise at the same time in a grid view. Is this even possible or I am just dreaming?
Requirement:
Show last one month data and next two month's data week wise in the grid.
Example-
If the current month is September then I want to show data from 1st August to 31st October categorized in weeks.
Show the data after the next month of current month in month wise view in the same grid.
Example-
data for month November and December will be shown categorized in month not in weeks.
grid or result should look something like below -
Please suggest something to achieve this
If you want a maintainable solution, use two independent queries, one for weekly aggregation other for monthly. Depending on the input run corresponding query.
I think you must work with group by or group by ... cube
SELECT x , y from <tabel> GROUP BY date_feld( to_char( 'MM' ))
but i don't now your DBMS so i can't give you a exact example for the date handling.

Access query (SQL) to return records sorted (grouped by) WEEKS

Greetings SQL gurus,
I don't know if you can help me, but I will try. I have several large databases grouped by year (each year in a different database). I want to be able to compare values from a particular week from one year to the next. For example, "show me week 17 of 2008 vs. week 17 of 2002."
I have the following definition of weeks that ideally I would use:
Only 52 weeks each year and 7 days a week (that only takes 364 days),
The first day of the first week starts from January 2nd - which means we do not use January 1st data, and
In leap year, the first day of the first week ALSO starts from the January 2nd plus we skip Feb. 29.
Any ideas?
Thanks in advance.
Best to avoid creating a table because then you have to update and maintain it to get your queries to work.
DatePart('ww',[myDate]) will give you the week number. You may run into some issues though deciding which week belongs to which year - for example if Jan 1 2003 is on Wednesday does the week belong as week 52 in 2002 or week 1 in 2003? Your accounting department will have a day of the week that is your end of week (usually Sat). I usually just pick the year that has the most days in it. DatePart will always count the first week as 1 and in the case of the example above the last week as 53. You may not care that much either way. You can create queries for each year
SELECT DatePart('ww',[myDate]) as WeekNumber,myYearTable.* as WeekNumber
FROM myYearTable
and then join the queries to get your data. You'll loose a couple days at the end of the year if one table has 52 weeks and one has 53 (most will show as 53). Or you can do it by your weekending day - this always gives you Saturday which would push a late week into the following year.
(7-Weekday([myDate]))+[myDate]
then
DatePart('ww',(7-Weekday([myDate]))+[myDate])
Hope that helps
To get the week number
'to get the week number in the year
select datepart( week, datefield)
'to get the week number in the month
select (datepart(dd,datefield) -1 ) / 7 + 1
You don't need to complicate things thinking about leap years, etc. Just compare weeks mon to sun
SInce you havea a specifc defintion of when the week starts that is differnt that the standard used by the db, I think a weeks table is the solution to your problem. For each year create a table that defines the dates contained in each week and the week number. Then by joining to that table as well as the relevant other tables, you can ask for just the data for week 17.
Table structure
Date Week
20090102 1
20090103 1
etc.
I needed to create a query that shows BOTH year AND week numbers, like 2014-52. The year shows correct when you use the Datepart() formula to convert week 53 to week 52 in the previous year, but shows the wrong year for the week that was week 1 previously that should be week 52 now. It show that week as 2015-52 instead of 2014-52.
Furthermore, it sorts the data wrong if you only use only the week number, eg:
2014-1,2014-11,2014-2
To overcome this I created the following query to insert a 0 and also to check for days in week 1 that should still fall under week 52.
ActualWeek: IIf(DatePart("ww",[SomeDate],1,3)=52 And DatePart("ww",[SomeDate])=1, DatePart("yyyy",[SomeDate],1,3)-1,DatePart("yyyy",[SomeDate],1,3)) & "-" & IIf(DatePart("ww",[SomeDate],1,3)<10,"0" & DatePart("ww",[SomeDate],1,3),DatePart("ww",[SomeDate],1,3))