SQL Weekly Data Update - sql

We have a database which is updated every Sunday the dataset looks like below;
[AccountID] [AccountName] [AccountContact] [Package] [NextTransactionDate] [ReportDate]
I use this data to create a power BI query which is automated to run every Monday as the data is updated every Sunday afternoon
I only want to use the data that has come in on the latest Sunday and not the data from the previous weeks. i.e. [ReportDate]
The BI report only needs to show the data from let's say 25/10/20 and then on the next cycle 01/11/20 and so on. But this has to be automated using a query so it doesn't need to manually update every week

you can add a where statement base on current db time in your query
WHERE ReportDate >= DATEADD(day,-7, GETDATE())
this should work just fine.

Related

How can I filter data between 2 dates in SQL

I have a report which i want to display the data within 3 days, i.e. when a user runs the report it should display data as of yesterday, today and tomorrow.
Where [tasSched].[CurrReqstDate] Between *yesterday today and tomorrow*
If you are using SQL Server you can use below query to get the data between yesterday and tomorrow.
WHERE [tasSched].[CurrReqstDate] between DATEADD(DAY, -1,GETDATE())
and DATEADD(DAY, +1,GETDATE())

how to automate the date change in a query using transact sql

I work for a company where everyday I modify a query by changing the date of the day before, because the report is always from the previous day.
I want to automate the date change. I have made a table with two columns, one with all dates from this year and another with bits where if 0 is a working day and 1 if is a holiday.
I have successfully automated a little bit by telling if the day before is a working day then subtract 1 from the date (This is what happens everyday). But the problem is, that if is Monday appears as Friday, because Saturday and Sunday are not billable. And let's also say, that if today is Thursday and Wednesday and Tuesday we're holidays, then the report will run on Monday. I will leave you a picture, that shows how the table is made with dates.
Remembering, that if there is no holidays in the middle of the week, always will be subtract one.
The way to do this sort of thing is close to what you have done, but just extend it further. Create a BusinessDate table that has every date, and then every rule you have implemented inside it. You can go so far as to include a column such as ReportDate which will return,for every date, what date the report should be run for.
Do this once, and it will work forever more. You may have to update for future holidays once a year, but better than once a day!
It will also allow you to update things specific for your business, like quarter dates, company holidays, etc.
If you want to know more on the subject, look up topics around creating a date dimension in a data warehouse. Its the same general issue you are facing.
Too complicated for a comment and it involves a lot of guessing.
So everyday, your process starts by first determining if "today" is a work day. So you would do something like:
if exists (select * from <calendar> where date = cast (getdate() as date) and IsWorkday = 1")
begin
<do stuff>
end;
The "do stuff" section would then run a report or your query (or something that isn't very clear) using the most recent work day prior to the current date. You find that date using something like:
declare #targetdate date;
set #targetdate = (select max(date) from <calendar>
where date < cast (getdate() as date)
and IsWorkday = 1);
if #targetdate is not null
<run your query using #targetdate>
That can be condensed into less code but it is easier to understand when the logic is written step-by-step.

SSIS Package to archive data on monthly basis

Can any one let me know how can i create a ssis package which will serve the below mentioned criteria.
1) Extract new member joiner data on monthly basis .
2) Store the data on a separate table call 'Joiner' which has column member_name,join_date,member_class.
3) the job will be scheduled to run 1st day of every month.
For example the package will run on 1st April with a join_date 1st March to 31st Match and dump it to the 'joiner' table. Next month it will run on 1st May with a join_date of 1st April to 30th April.
I know i have to create a store procedure with a join date parameter to pass but my concern is how should i achieve this automation of passing date every month and archive joiners data on month and month basis.
Any help will be much appreciated.
It sounds like your question is about how to get the previous month of data on an automated basis. DATEADD() and EOMONTH() can accomplish your need of grabbing a rolling timeframe each month automatically if you are using SQL Server.
SELECT
getdate(), --current date
EOMONTH(GETDATE()), --last day of the current month
EOMONTH(GETDATE(),-1), --last day of the previous month
DATEADD(DAY, 1, EOMONTH(GETDATE(),-2)) --go back two months and add 1 day
Your query would need to include something like this in the WHERE clause.
WHERE join_date >= DATEADD(DAY, 1, EOMONTH(GETDATE(),-2))
AND join_date < EOMONTH(GETDATE(),-1)

How do I add a column in PowerBI that evaluates the difference of Latest End time and Earliest Start Time from a large dataset?

I am new to PowerBI. I am trying to make a report of the number of days consumed for a test to complete. There is a large fleet of tests that are run across a week and I would like to subtract the Earliest Start Time from the Latest End time, excluding Saturdays and Sundays and then display the result as a new column next to the Latest actual end as shown in the picture below.
Pardon for any errors above. The data was fetched from a SQL Server using a query (if that helps). Thank you.
query in sql server
select
*
--number of days
,DATEDIFF (day,[Earrliest startTime], [Latest actualend]) diffday
from TestTable
--excluding Saturdays and Sundays
where datepart(weekday,[Earrliest startTime]) not in (6,7)
and datepart(weekday,[Latest actualend]) not in (6,7)
SQL Fiddle
Hope it help you :-)
Create a New Table DateTable to be your calendar table.
DateTable = CALENDARAUTO()
Add a weekday column so you can filter out weekends.
Weekday = WEEKDAY(DateTable[Date])
Now you can create a measure that counts the days between your first and last day:
DayCount = COUNTX(DateTable,
IF(DateTable[Date]+1 > MIN(StartEnd[startTime]) &&
DateTable[Date]+1 < MAX(StartEnd[acutalend]) &&
NOT(DateTable[Weekday] IN {1,7}),
1, BLANK()))
The +1 are there to give you end of day rather than beginning of day.

How to get data for just two hour from previous day,

I need to get data from previous day (sql server db) for transferring to other DB (postgress), but since data is huge I want to transfer records for just 2 hr, I mean I will run this job 12 times a day and each time it will transfer rec for 2 hr, record transfered should not be duplicate.
So basically I need a query which I can schedule to run 12 times and which will transfer records for two hours each.
declare #StartHour datetime, #EndHour datetime
set #EndHour = dateadd(hh,datediff(hh,0,GetDate()),0)
set #StartHour = dateadd(hh,-2,#EndHour)
--The above make the query work with start and end on-the-hour
--so it can be run any time within one hour to get the data
--for the two hours ending on the previous hour
select * from whatever where TheDate between #StartHour and #EndHour
If you are timestamping inserts, then it should be simple to run a select to pull out only the preceding two hours worth of records.
SELECT * FROM tblFoo WHERE tblFoo.insertionDate>DATEADD(hour,-2,GETDATE())
(If you want to be exact, then don't use GETDATE but hold the last date that ran in a table or variable somewhere and add two hours to it each time then setting it after running the query)