this is my first question here. Hopefully I´m clear enough what I´m searching for.
My problem is following:
On this analysis I want to get from the last 7 weeks, the summarized prices of each week. Its working with out any problems, but now I would like to add the weeks number of each week as alias.
In my tests I was using for example something like this:
DECLARE #week7 varchar(10)
SET #week7 = DATEPART(wk, GetDate())
One of my problems is, that I´m not allowed to work with "EXEC".
This is just an example of my analysis:
SELECT DISTINCT(
SELECT SUM(Price)
FROM tblBookingdata
WHERE(Datum BETWEEN DATEADD(wk, -7, DATEADD(DAY, 1 - DATEPART(WEEKDAY, GETDATE()), DATEDIFF(dd, 0, GETDATE()))) AND DATEADD(wk, -6, DATEADD(DAY, 1 - DATEPART(WEEKDAY, GETDATE()), DATEDIFF(dd, 0, GETDATE()))))) AS '7 weeks ago', (
SELECT SUM(Price)
FROM tblBookingdata
WHERE(Datum BETWEEN DATEADD(wk, -6, DATEADD(DAY, 1 - DATEPART(WEEKDAY, GETDATE()), DATEDIFF(dd, 0, GETDATE()))) AND DATEADD(wk, -5, DATEADD(DAY, 1 - DATEPART(WEEKDAY, GETDATE()), DATEDIFF(dd, 0, GETDATE()))))) AS '6 weeks ago'
I would like the column name to show the week number from each sub select. That the output would be for example for this week: 40 (as column name) and 900 as price summary.
So I tried to work here with DECLARE and assign #week7 for example with the current week number. But here I got stuck, due it seems like I need to work here with EXEC.
Is this only possible with "EXEC" or are there any other solutions to solve this? I was looking in the www, but currently I´m stucking a bit. Thankful for every help! :)
I think the DateDiff function is your friend here. Are you using SQL Server? This won't display a row for the week if there are zero records in that week, but this should be close to what you want.
select WeeksAgo, sum(Price) as Price from (
select
Price
,Datediff(wk, Datum, getDate()) as WeeksAgo
,Datum --not used
from
tblBookingdata
)DataByWeek
where WeeksAgo between 0 and 7 --should this be 0-6?
group by WeeksAgo
I think you're looking for something like this. The prior 7 weeks are calculated from GETDATE based on a numbers table with 1, 2, 3, ... 7. Then the booking Prices are summarized by week where the Datum is within the prior 7 weeks. This will display NULL in price_sum if there were no sales that week.
drop table if exists #tblBookingdata;
go
create table #tblBookingdata(
Datum date not null,
Price int not null);
go
;with
weeks_cte(wk) as (
select datepart(wk, dateadd(wk, w*-1, getdate()))
from (values (1),(2),(3),(4),(5),(6),(7)) v(w)),
bookings_cte(wk, price_sum) as (
select datepart(wk, Datum), sum(Price)
from #tblBookingdata
where Datum>dateadd(wk, -7, getdate())
group by datepart(wk, Datum))
select *
from weeks_cte wc
left join bookings_cte b on wc.wk=b.wk;
Related
I have created the following query to reoccur every seven days starting including Saturday and Sunday. This report will be run every seven days. However, the problem I am facing on the days where there were files received in our SFTP folder (inbound) report should have an entry for the missing Null= 0. The primary goal to make this an automated process that will be executed every Sunday through Sunday every seven days
Example:
SELECT SubmitterID,SubmitterName,convert(varchar(15), DateReceived, 101) DateReceived,sum(ClaimCount) as TotalCount
FROM FalloutClaimReport
WHERE DateReceived BETWEEN '2019-06-01' AND '2019-06-07'
--ORDER BY COUNT(submitterID) DESC;
GROUP BY submitterid, SubmitterName, convert(varchar(15), DateReceived, 101)
DECLARE #StartDate AS DATETIME
DECLARE #EndDate AS DATETIME
DECLARE #CurrentDate AS DATETIME
SET #StartDate = '2019-06-01' --AND '2019-06-10'
SET #StartDate = '2019-06-07'
SET #EndDate = GETDATE()
SET #CurrentDate = #StartDate
I'm a little uncertain what you're looking for but I think the general approach is you're trying to get a week's worth of data.
Date calculations
Let's start with some queries (and these presume a US install as the default day is Monday.
SELECT
DATEADD(WEEK, -1, CAST(DATEADD(WEEK, DATEDIFF(WEEK, 0, CAST(GETDATE() AS date)), -1) AS date)) AS TheLastSundayOfTheFullWeek
, DATEADD(WEEK, -1, CAST(DATEADD(WEEK, DATEDIFF(WEEK, 0, CAST(GETDATE() AS date)), +5) AS date)) AS TheLastSaturdayOfTheFullWeek
, CAST(DATEADD(WEEK, DATEDIFF(WEEK, 0, CAST(GETDATE() AS date)), -1) AS date) AS SundayOfTheCurrentWeek
, CAST(DATEADD(WEEK, DATEDIFF(WEEK, 0, CAST(GETDATE() AS date)), +5) AS date) AS SaturdayOfTheCurrentWeek;
These queries generate the following dates
TheLastSundayOfTheFullWeek TheLastSaturdayOfTheFullWeek SundayOfTheCurrentWeek SaturdayOfTheCurrentWeek
2019-06-30 2019-07-06 2019-07-07 2019-07-13
The last full week would run 6/30 to 7/06. The current week would be defined as 7/7 to 7/13.
Depending on which week definition you need, choose the appropriate pair of columns.
Dealing with the unknowns
In situations like this, I build out a virtual table with all expected dates (or elements) my report should have. I then use that to drive a connection to the actual data table. Since we don't know that we'll find any rows for a given date, I connect the tables with a LEFT JOIN
SELECT
FCR.SubmitterID
, FCR.SubmitterName
, CONVERT(varchar(15), ED.DateReceived, 101) AS DateReceived
, SUM(FCR.ClaimCount) AS TotalCount
FROM
(
-- This logic builds out a list of all the dates that must exist on the report
-- I used the logic for TheLastSundayOfTheFullWeek
SELECT
DATEADD(DAY, D.DayOffset, DATEADD(WEEK, -1, CAST(DATEADD(WEEK, DATEDIFF(WEEK, 0, CAST(GETDATE() AS date)), -1) AS date))) AS DateReceived
FROM
(
-- Generate a series of 7 numbers from 0 t 6
SELECT TOP 7
-1 + ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS rn
FROM
sys.all_columns AS AC
) D(DayOffset)
) AS ED
LEFT OUTER JOIN
dbo.FalloutClaimReport AS FCR
ON FCR.DateReceived = ED.DateReceived
GROUP BY
CONVERT(varchar(15), ED.DateReceived, 101)
, FCR.SubmitterID
, FCR.SubmitterName;
That generates a result set like
We didn't have data on the the 30th or the 5th but there are still records on the query. If you need default values in there, wrap the column with ISNULL/COALESCE calls.
DBFiddle version to provide a testing sandbox
Audrey,
I would suggest two possible solutions. Assuming by SRS you meant, SSRS....
1) I would set your SSIS job to run through a SQL Agent every 7 days. It would call a stored procedure (SP) that then ran and wrote into a table... when that SP would be called it would run:
SELECT
SubmitterID,
SubmitterName,
convert(varchar(15), DateReceived, 101) DateReceived,
sum(ClaimCount) as TotalCount
FROM FalloutClaimReport
WHERE cast(DateReceived as date) BETWEEN dateadd(d,-7,cast(getdate() as date)) AND dateadd(d,-1,cast(getdate() as date))
GROUP BY
submitterid,
SubmitterName,
convert(varchar(15), DateReceived, 101)
2) If you decide to go the SSRS route, you should make a report subscription that sends automatically to the users you need that calls the stored procedure above and sends what you need to whoever needs it. (The code above should be enough for that assuming it's selecting what you need)
SQL query to get the 15 of the month for the following year.
Today
select getdate() = 2017-08-23 17:05:24.143
Looking for: 2018-8-15 00:00:00
I know how to get a year from today:
select dateadd(year,1,datediff(day,0,getdate()))
I know how to get the beginning of the month:
SELECT DATEADD(month, DATEDIFF(month, 0, GETDATE()), 0)
However I am having trouble combining the two.
You can use datefromparts for SQL Server versions 2012 and above.
select datefromparts(year(getdate())+1,month(getdate()),15)
Truncate the current day to the start of the month with the code you have, but add 12 months (so, a year), and add 14 days.
select dateadd(day,14,dateadd(month, 12+datediff(month, 0, getdate()), 0))
SELECT DATEADD(month, DATEDIFF(month, 0, DATEADD(year, 1, DATEDIFF(day, 0, GETDATE())), 0)
Vamsi Prabhala's answer deserves to be accepted.
But, you should consider creating a Calendar Table because it greatly simplifies working with dates in general.
Here is a pretty simply query that yields the results that you want:
select * from Calendar C where C.year = datepart(year, getdate()) + 1
and C.day_of_month = 15 and C.month = datepart(m, getdate())
Rextester Demo
I am trying the below query, but it's output comes last 2 weeks data, but I need only last before week data only.
select * from tablename where createddate>=DATEADD(WEEK,-2, GETDATE()) ;
Just add another condition to your WHERE clause to restrict to earlier than the week before last:
SELECT * FROM tablename
WHERE createddate >= DATEADD(WEEK,-2, GETDATE()) AND
createddate < DATEADD(WEEK,-1, GETDATE())
From one your comments I found that the week you are talking about starts from Friday, so you need to add up those gap days into your condition
SELECT * FROM tablename
WHERE createddate >= DATEADD(ww, DATEDIFF(ww, 4 ,DATEADD(WEEK, -1, GETDATE())), 4)
AND createddate < DATEADD(ww, DATEDIFF(ww, 4 ,DATEADD(WEEK, 0, GETDATE())), 4)
this code is currect answer for my quation,
select * from dbo_OrdersCompleteView1 where S2_DateTimeOrederLines between DATEADD(WEEK,-2, GETDATE()) and DATEADD(WEEK,-1,GETDATE());
This seems to have been answered several times for the past 30 days. But seemingly not what I need.
If, for example, today is July 10th, 2012. I'm looking to pull all of June's data. I will need to run this query several days after the start of each month
There is certainly better ways to do this, but one way would be this:
DECLARE #Date DATETIME
SET #Date = '20120710'
SELECT *
FROM YourTable
WHERE YourDateColumn >= CONVERT(VARCHAR(6),DATEADD(MONTH,-1,#Date),112)+'01'
AND YourDateColumn < CONVERT(VARCHAR(6),#Date,112)+'01')
-- first day of previous month
select DateAdd(Month, DateDiff(Month, 0, GetDate())-1,0)
-- last day of previous month
Select DateAdd(day,-1,DateAdd(Month,1,DateAdd(Month,
DateDiff(Month, 0,GETDATE())-1,0)))
Please replace GETDATE() with your date column
Ah, in that case you need something like:
SELECT *
FROM
reporting_table_name_goes_here
WHERE
DATEPART(month, YourDateColumn) = DATEPART(month, DATEADD(month, -1, getdate()))
AND DATEPART(year, YourDateColumn) = DATEPART(year, DATEADD(month, -1, getdate()))
Check out MSDN for details.
I want to select all the records from [Orders] that have a [Submissiondate] less than 7 days.
I'm completely stumped. This is the query I'm executing:
SELECT * FROM [Orders] WHERE ([SubmissionDate] < #SubmissionDate)
Doesn't work.
If you mean you want rows with SubmissionDate between #SubmissionDate and #SubmissionDate - 7 days, then this is how I would implement that in Transact-SQL:
WHERE [SubmissionDate] BETWEEN DATEADD(DAY, -7, #SubmissionDate)
AND #SubmissionDate
Note that BETWEEN implies >= and <=. If you need strict inequalities, make it something like this:
WHERE [SubmissionDate] > DATEADD(DAY, -7, #SubmissionDate)
AND [SubmissionDate] < #SubmissionDate
Assuming that the sql parameter #SubmissionDate is the date (and time) now. You could use the following query that will return those [Orders] submitted within the last 7 days:
SELECT * FROM [Orders] WHERE ([SubmissionDate] >= DATEADD(DD, -7, DATEADD(dd, 0, DATEDIFF(dd, 0, #SubmissionDate))))
Two important remarks to this solution:
Time 'part' is being removed from #SubmissionDate.
As there is no 'Date To' restriction, do includes the [Orders] submitted
'today' (until the time the query is being executed).
The following code is just to get the date 'part' only of a date-time (extracted from this other SO thread).
DATEADD(dd, 0, DATEDIFF(dd, 0, #SubmissionDate))
Try this
SELECT * FROM [Orders] WHERE [submission_date] < NOW() - INTERVAL 7
DAY;
You can also try this DATEDIFF
SELECT * FROM [Orders] WHERE datediff(d,SubmissionDate,GETDATE()) > 7
where GetDate() is today's date and the d is difference in days
select datediff(d,'2012/06/23',GETDATE())
should give you 7 because it's 7 days ago