SSRS Pull data from last year when January - sql

Basically, when the report is run, I'd like to display YTD data; unless, it is January, then, I want to display all of last year.
I've been attempting to put this in my where statement, using case or iif. In both cases, I get the same error: Msg 102, Level 15, State 1... Incorrect syntax near 'between'.
SELECT
name,
owner,
duedate,
submitteddate
FROM Table
WHERE submitteddate IS NOT NULL AND
CASE WHEN MONTH(GETDATE()) = 1
THEN submitteddate between DATEADD(YEAR, DATEDIFF(YEAR, '19000101', GETDATE()) - 1 , '19000101') AND DATEADD(d, -1, DATEADD(YEAR, DATEDIFF(YEAR, '19000101', GETDATE()), '19000101'))))
ELSE submitteddate between DATEADD(yy, DATEDIFF(yy, 0, GETDATE()), 0) AND GETDATE()))
end

It seems like related to usage of the bracelets. please try like below:
SELECT
name,
owner,
duedate,
submitteddate
FROM Table
WHERE submitteddate IS NOT NULL AND
CASE WHEN MONTH(GETDATE()) = 1
THEN submitteddate between DATEADD(YEAR, DATEDIFF(YEAR, '19000101', GETDATE()) - 1 , '19000101') AND DATEADD(d, -1, DATEADD(YEAR, DATEDIFF(YEAR, '19000101', GETDATE()), '19000101'))
ELSE submitteddate between DATEADD(yy, DATEDIFF(yy, 0, GETDATE()), 0) AND GETDATE()
end

....WHERE YEAR(submitteddate) = YEAR(GETDATE())
- CASE WHEN MONTH(GETDATE()) = 1 THEN 1 ELSE 0 END

How about
WHERE
submitteddate >=
IIF(MONTH(GETDATE()) = 1, DATEADD(Year, -1, GETDATE()), '1900-01-01')
AND submitteddate <= GETDATE();
Or by using a variable
DECLARE #FromDate DATEIME = (SELECT IIF(MONTH(GETDATE()) = 1, DATEADD(Year, -1, GETDATE()), '1900-01-01'));
SELECT *
FROM Table
WHERE
submitteddate >=
#FromDate
AND submitteddate <= GETDATE();

Rather than using a CASE expression in the WHERE, which isn't SARGable, you would be far better off using variables:
DECLARE #StartDate DATE, #EndDate Date;
SET #StartDate = CASE DATEPART(MONTH,GETDATE()) WHEN 1 THEN DATEADD(YEAR, DATEDIFF(YEAR, 0,GETDATE())-1,0)
ELSE DATEADD(YEAR, DATEDIFF(YEAR,0,GETDATE()),0)
END;
SET #EndDate = DATEADD(YEAR,DATEDIFF(YEAR, 0,#StartDate)+1,0);
--SELECT #StartDate, #EndDate; --Uncomment to check the values, but would break SSRS, as it only reads the first returned dataset.
SELECT [name],
[owner],
duedate,
submitteddate
FROM [Table]
WHERE submitteddate >= #StartDate
AND submitteddate < #EndDate;
Note that there is no need to check if the value of submitteddate is NULL or not. NULL = {expression} will always result in "unknown", which isn't true and thus won't be returned in a data set. Thus if you don't want NULL values, then the IS NOT NULL clause is pointless in a WHERE like:
WHERE [Column] IS NOT NULL
AND [Column] = 1;
The above would return exactly the same rows as:
WHERE [Column] = 1;

Related

Can a SSRS report contain double date/time parameters?

Is it possible to have two date/time parameters in a report?
I have a simple dataset with the 'where' clause:
where RequestSource in (#reqsource)
and EntryDay between #startdate and #finishdate
and EntryDay between #periodstart and #periodend
In the report there is a calendar date/time parameter (using #startdate and #finishdate) and I wanted to add a second one (two part parameter) (using #periodstart and #periodend).
The dataset for the #periodstart parameter contains:
select DATEADD(qq, DATEDIFF(qq, 0, GETDATE()) - 1, 0) as time, 'last quarter start' as timename
union all
select DATEADD(qq, DATEDIFF(qq, 0, GETDATE()), 0) as time, 'this quarter start' as timename
union all
select DATEADD(yy, DATEDIFF(yy, 0, GETDATE()), 0) as time, 'this year start' as timename
union all
select DATEADD(yy, DATEDIFF(yy, 0, GETDATE()) - 1, 0) as time, 'last year start' as timename
The dataset #periodend parameter contains:
select DATEADD(dd, -1, DATEADD(qq, DATEDIFF(qq, 0, GETDATE()), 0)) as time, 'last quarter end' as timename
union all
select DATEADD (dd, -1, DATEADD(qq, DATEDIFF(qq, 0, GETDATE()) +1, 0)) as time, 'this quarter end' as timename
union all
select DATEADD(yy, DATEDIFF(yy, 0, GETDATE()) + 1, -1) as time, 'this year end' as timename
union all
select DATEADD(yy, DATEDIFF(yy, 0, GETDATE()), -1) as time, 'last year end' as timename
Is it possible to have the #startdate and #finishdate parameters in null and use the #periodstart and #periodend parameters which have pre-defined values?
report parameter view
I'm not really sure what your issue is, you are asking if it's possible to have more than one start date and end date?
Yes it is, you just need to accommodate this in your dataset query.
Unless I'm missing something I would do it something like this.
DECLARE #sDate datetime
DECLARE #eDate datetime
SET #sDate = ISNULL(#startdate, #periodstart)
SET #eDate = ISNULL(#finishdate, #periodend)
SELECT *
FROM myTable
WHERE RequestSource IN (#reqsource)
AND EntryDay BETWEEN #sDate AND #eDate

TSQL only pull data if another date field is the previous business day

1) I'm need only to pull data if the the openddate is equal to the PrevBiz date. I think the where/and statement would be Openddate = PrevBiz, but not sure. It wasn't working for me and could be because the date format isn't matching. Any Suggestions?
DECLARE #TODAY DATE = GETDATE()
DECLARE #PREVFIRST CHAR(8) = CONVERT(CHAR(8), DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) - 1, 0), 112)
DECLARE #PREVLAST CHAR(8) = CONVERT(CHAR(8), DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), -1), 112)
DECLARE #PREVBIZ CHAR(12) = DATEADD(DAY, CASE DATENAME(WEEKDAY, CONVERT(CHAR(12), #TODAY,112))
WHEN 'SUNDAY' THEN -2
WHEN 'MONDAY' THEN -3
ELSE -1 END, DATEDIFF(DAY, 0, CONVERT(CHAR(12), #TODAY, 112)))
SELECT TOP 10
CURRENTDATE =#TODAY,
FIRST_OF_MONTH =#PREVFIRST,
LASTDAY_OFMONTH =#PREVLAST,
PREVBIZ =#PREVBIZ,
DATEADD(DAY, CASE DATENAME(WEEKDAY, CONVERT(DATE, #TODAY,101))
WHEN 'SUNDAY' THEN -2
WHEN 'MONDAY' THEN -3
ELSE -1 END, DATEDIFF(DAY, 0, CONVERT(DATE, #TODAY, 101))) AS PREVIOUSBIZDATE,
OpendDate
FROM [USBI_DW].[USBI].[vw_NameAddressBase]
where IsCurrent = 1
Here's my results:
declare #TODAY datetime = convert(date,GETDATE())
declare #PREVBIZ datetime = DATEADD(DAY, CASE DATENAME(WEEKDAY,#TODAY)
WHEN 'SUNDAY' THEN -2
WHEN 'MONDAY' THEN -3
ELSE -1 END,#TODAY)
declare #iToday int = convert(nvarchar(8),#TODAY, 112)
, #iPrevBiz int = convert(nvarchar(8),#PREVBIZ, 112)
select top 10
CURRENTDATE =#iToday,
PREVBIZ =#iPrevBiz,
OpendDate
from [USBI_DW].[USBI].[vw_NameAddressBase]
where IsCurrent = 1
and OprendDate = #iPrevBiz
hope your view contains int date attribute ( because of DateWarehouse specific)

Sum of Dates between, T-SQL error

I'm trying to check if one of the join_date or date_of_change (date fields) are within the range and count them but I get an error:
sqlserver.jdbc.SQLServerException: The conversion from int to
TIMESTAMP is unsupported.
SUM(CASE WHEN (join_date BETWEEN DATEADD(day, -8, GETDATE()) AND DATEADD(day, -1, GETDATE())) OR (date_of_change BETWEEN DATEADD(day, -8, GETDATE()) AND DATEADD(day, -1, GETDATE())) THEN 1 ELSE 0 END) AS Total
Could someone explain to me what I'm doing wrong.
Original Code:
SELECT DISTINCT mtype, CASE WHEN (join_date BETWEEN DATEADD(day, -8,
GETDATE()) AND DATEADD(day, -1, GETDATE())) OR (date_of_change BETWEEN
DATEADD(day, -8, GETDATE()) AND DATEADD(day, -1, GETDATE())) THEN 1 ELSE 0
END AS Total FROM T0 GROUP BY mype, join_date,date_of_change
As #Alex K has said there could be another statement in the batch causing the problem as there doesn't seem to be any TimeStamp involved in this query.
Answering your last comment about the GROUP BY, you can simplify the query the following way:
SELECT
mtype, COUNT(1) as Total
FROM
T0
WHERE
(join_date BETWEEN DATEADD(day, -8, GETDATE()) AND DATEADD(day, -1, GETDATE()))
OR (date_of_change BETWEEN DATEADD(day, -8, GETDATE()) AND DATEADD(day, -1, GETDATE()))
GROUP BY
mype
But I am afraid that if the error is in a different statement.
I wrote the query again and it worked, must be a typo somewhere

Accumulating data from previous year

I'm having trouble with finding a solution for below SQL-query. My first query should and does the trick of accumulating amount of last year's data.
CASE
WHEN rehuv.VER_DATUM >= dateadd(year, -1, dateadd(month, -datepart(month, #STARTDATE) + 1, #STARTDATE))
AND rehuv.VER_DATUM <= DATEADD(year, -1, #ENDDATE) THEN rehuv.BELOPP ELSE 0
END AS PREVIOUS_YEAR_ACC
But when I try to accumulate data within a specific time interval (I want the same time interval as #startdate -> #enddate but one year backwards). Sql-query:
CASE
WHEN rehuv.VER_DATUM >= DATEADD(year, -1, #STARTDATE)
AND rehuv.VER_DATUM <= DATEADD(year, -1, #ENDDATE) THEN rehuv.BELOPP ELSE 0
END AS PREVIOUS_YEAR_MONTH
Thank you in advance!
Best regards,
Simon
If you want the monthly sum of last year, you can try this:
DECLARE #STARTDATE DATETIME;
DECLARE #ENDDATE DATETIME;
SET #STARTDATE='2015-1-1';
SET #ENDDATE='2015-5-1';
DECLARE #LAST_YEAR DATE;
SET #LAST_YEAR = DATEADD(YEAR, -1, DATEADD(DAY,
DATEPART(DAYOFYEAR, GETDATE())*-1+1, CONVERT(DATE, GETDATE())));
SELECT DATEADD(MONTH, DATEPART(MONTH, VER_DATUM), #LAST_YEAR) MONTH, SUM(CASE
WHEN REHUV.VER_DATUM >= DATEADD(YEAR, -1, #STARTDATE)
AND REHUV.VER_DATUM <= DATEADD(YEAR, -1, #ENDDATE)
THEN REHUV.BELOPP ELSE 0
END ) AS PREVIOUS_YEAR_MONTH
FROM REHUV
GROUP BY DATEPART(MONTH, VER_DATUM)

How to run a query for year to date based on end date

I have a query that runs based on the "end date" picked. Essentially the user picks the end date, and the query will run the report for the entire YTD based on the end date. So for example if I select "12/3/12" it should run the report from 1/1/12 - 12/3/12. This works if you run it ON the day... Today I tried to run it for end date "12/31/12" however I return no results because I think it's trying to get the start date based on today's date? Below is my query:
SELECT Store_Number, COUNT(DISTINCT Customer_Email_Address) AS Customer_email_address, COUNT(DISTINCT Invoice_Number) AS [Total Cars],
#enddate AS End_Date, DATEADD(yy, DATEDIFF(yy, 0, GETDATE()), 0) AS Start_Date
FROM Invoice_Tb
WHERE (Invoice_Date BETWEEN DATEADD(yy, DATEDIFF(yy, 0, GETDATE()), 0) AND CONVERT(Datetime, #enddate, 102))
GROUP BY Store_Number
Replace the GetDate() with the #enddate parameter and it should work:
SELECT
Store_Number,
COUNT(DISTINCT Customer_Email_Address) AS Customer_email_address,
COUNT(DISTINCT Invoice_Number) AS [Total Cars],
#enddate AS End_Date,
DATEADD(yy, DATEDIFF(yy, 0, #enddate), 0) AS Start_Date
FROM Invoice_Tb
WHERE (Invoice_Date BETWEEN DATEADD(yy, DATEDIFF(yy, 0, #enddate), 0)
AND CONVERT(Datetime, #enddate, 102))
GROUP BY Store_Number
If you query:
declare #enddate datetime = '12/31/2012'
select DATEADD(yy, DATEDIFF(yy, 0, #enddate), 0)
It will return 2012-01-01 which is what you want.