Last day of current quarter date parameter SSRS - reporting-services

I'd like some of my SSRS reports to default to show the dates of the current financial quarter when they run.
I'm in the UK so Quarter 1 = 01-APR-2018 to 30-JUN-2018 etc
I have two date parameters (SSRS). For the first parameter I've used this expression:
=DateAdd(DateInterval.Quarter, DateDiff(DateInterval.Quarter, CDate("1/1/1900"), Today()), CDate("1/1/1900"))
When I run the report, it defaults to 01-APR-2018, which is what I want.
However, I've been unable to work out an expression to bring through the final day of the current quarter.
Any ideas of how to achieve this would be appreciated.

SSRS Expression
Here's the expression that will work in your report.
Last Day of Current Quarter
= DATEADD(
DateInterval.Day
, -1
, DATEADD(
DateInterval.Quarter
, DATEPART(DateInterval.Quarter, DateTime.Now )
, DATESERIAL(YEAR(Now), 1, 1)
)
)
T-SQL Examples
Here are the formulas in SQL, if you want to drive the defaults from a query. This way you can create a view on the database and use that instead of having to do the expressions in your report. If you don't have permissions to create the view, you can create a Shared Dataset / .rsd file on the report server using just the select statement.
First Day of Current Quarter
SELECT [first_quarter_day] = DATEADD(qq, DATEDIFF(qq, 0, GETDATE()), 0)
Last Day of Current Quarter
SELECT [last_quarter_day] = DATEADD(dd, -1, DATEADD(qq, 1 ,DATEADD(qq, DATEDIFF(qq, 0, GETDATE()), 0)))
View Example
CREATE VIEW [dbo].[date_defaults]
AS
SELECT
[First_Day_of_Current_Month] = DATEADD(mm, DATEDIFF(mm,0, GETDATE()), 0)
, [Last_Day_of_Current_Month] = DATEADD(dd, -1, DATEADD(mm, 1, DATEADD(mm, DATEDIFF(mm, 0, GETDATE()), 0)))
, [First_Day_of_Next_Month] = DATEADD(mm, 1,DATEADD(mm, DATEDIFF(mm, 0, GETDATE()), 0))
, [Last_Day_of_Previous_Month] = DATEADD(dd, -1,DATEADD(mm, DATEDIFF(mm, 0, GETDATE()), 0))
, [First_Day_of_Current_Quarter] = DATEADD(qq, DATEDIFF(qq, 0, GETDATE()), 0)
, [First_Day_of_Previous_Quarter] = DATEADD(qq, DATEDIFF(qq, 0, GETDATE())-1, 0)
, [Last_Day_of_Current_Quarter] = DATEADD(dd, -1, DATEADD(qq, 1, DATEADD(qq, DATEDIFF(qq, 0, GETDATE()), 0)))
, [Last_Day_of_Previous_Quarter] = DATEADD(dd, -1, DATEADD(qq, DATEDIFF(qq, 0, GETDATE()), 0))
, [First_Day_of_Current_Year] = DATEADD(yy, DATEDIFF(yy,0, GETDATE()), 0)
, [First_Day_of_Previous_Year] = DATEADD(yy, DATEDIFF(yy, 0,DATEADD(yy, -1, GETDATE())), 0)
, [Last_Day_of_Previous_Year] = DATEADD(dd, -1,DATEADD(yy, DATEDIFF(yy, 0, GETDATE()), 0))
, [This_Day_Last_Year] = DATEADD(yy, -1, GETDATE())
GO
Shared Dataset XML example
Note: You'll have to update the DataSourceReference with a datasource on the report server.
<?xml version="1.0" encoding="utf-8"?>
<SharedDataSet xmlns="http://schemas.microsoft.com/sqlserver/reporting/2010/01/shareddatasetdefinition" xmlns:rd="http://schemas.microsoft.com/SQLServer/reporting/reportdesigner">
<DataSet Name="date_defaults">
<Query>
<DataSourceReference>Your_DataSource_Here</DataSourceReference>
<CommandText> SELECT
[First_Day_of_Current_Month] = DATEADD(mm, DATEDIFF(mm,0, GETDATE()), 0)
, [Last_Day_of_Current_Month] = DATEADD(dd, -1, DATEADD(mm, 1, DATEADD(mm, DATEDIFF(mm, 0, GETDATE()), 0)))
, [First_Day_of_Next_Month] = DATEADD(mm, 1,DATEADD(mm, DATEDIFF(mm, 0, GETDATE()), 0))
, [Last_Day_of_Previous_Month] = DATEADD(dd, -1,DATEADD(mm, DATEDIFF(mm, 0, GETDATE()), 0))
, [First_Day_of_Current_Quarter] = DATEADD(qq, DATEDIFF(qq, 0, GETDATE()), 0)
, [First_Day_of_Previous_Quarter] = DATEADD(qq, DATEDIFF(qq, 0, GETDATE())-1, 0)
, [Last_Day_of_Current_Quarter] = DATEADD(dd, -1, DATEADD(qq, 1, DATEADD(qq, DATEDIFF(qq, 0, GETDATE()), 0)))
, [Last_Day_of_Previous_Quarter] = DATEADD(dd, -1, DATEADD(qq, DATEDIFF(qq, 0, GETDATE()), 0))
, [First_Day_of_Current_Year] = DATEADD(yy, DATEDIFF(yy,0, GETDATE()), 0)
, [First_Day_of_Previous_Year] = DATEADD(yy, DATEDIFF(yy, 0,DATEADD(yy, -1, GETDATE())), 0)
, [Last_Day_of_Previous_Year] = DATEADD(dd, -1,DATEADD(yy, DATEDIFF(yy, 0, GETDATE()), 0))
, [This_Day_Last_Year] = DATEADD(yy, -1, GETDATE())</CommandText>
</Query>
<Fields>
<Field Name="First_Day_of_Current_Month">
<DataField>First_Day_of_Current_Month</DataField>
<rd:TypeName>System.DateTime</rd:TypeName>
</Field>
<Field Name="Last_Day_of_Current_Month">
<DataField>Last_Day_of_Current_Month</DataField>
<rd:TypeName>System.DateTime</rd:TypeName>
</Field>
<Field Name="First_Day_of_Next_Month">
<DataField>First_Day_of_Next_Month</DataField>
<rd:TypeName>System.DateTime</rd:TypeName>
</Field>
<Field Name="Last_Day_of_Previous_Month">
<DataField>Last_Day_of_Previous_Month</DataField>
<rd:TypeName>System.DateTime</rd:TypeName>
</Field>
<Field Name="First_Day_of_Current_Quarter">
<DataField>First_Day_of_Current_Quarter</DataField>
<rd:TypeName>System.DateTime</rd:TypeName>
</Field>
<Field Name="First_Day_of_Previous_Quarter">
<DataField>First_Day_of_Previous_Quarter</DataField>
<rd:TypeName>System.DateTime</rd:TypeName>
</Field>
<Field Name="Last_Day_of_Current_Quarter">
<DataField>Last_Day_of_Current_Quarter</DataField>
<rd:TypeName>System.DateTime</rd:TypeName>
</Field>
<Field Name="Last_Day_of_Previous_Quarter">
<DataField>Last_Day_of_Previous_Quarter</DataField>
<rd:TypeName>System.DateTime</rd:TypeName>
</Field>
<Field Name="First_Day_of_Current_Year">
<DataField>First_Day_of_Current_Year</DataField>
<rd:TypeName>System.DateTime</rd:TypeName>
</Field>
<Field Name="First_Day_of_Previous_Year">
<DataField>First_Day_of_Previous_Year</DataField>
<rd:TypeName>System.DateTime</rd:TypeName>
</Field>
<Field Name="Last_Day_of_Previous_Year">
<DataField>Last_Day_of_Previous_Year</DataField>
<rd:TypeName>System.DateTime</rd:TypeName>
</Field>
<Field Name="This_Day_Last_Year">
<DataField>This_Day_Last_Year</DataField>
<rd:TypeName>System.DateTime</rd:TypeName>
</Field>
</Fields>
</DataSet>
</SharedDataSet>

Related

Getting Error while using Division in SSRS

I'm getting error when I use this formula in SSRS 2017:
Operator '/' is not defined for types 'integer' and 'System.TimeSpan'
Operator '*' is not defined for types 'System.TimeSpan' and 'System.TimeSpan'
=IIF(
100 / (DateAdd("d", -(Day(Today)), Today) -
DateSerial(Year(DateAdd("m", -1, Now())), 1, 1)
)
* ((Today - DateSerial(Year(DateAdd("m", -1, Now())), 1, 1)) / 100) > 1,
1,
100 / (DateAdd("d",-(Day(Today)), Today) -
DateSerial(Year(DateAdd("m", -1, Now())), 1, 1)
)
* ((Today - DateSerial(Year(DateAdd("m", -1, Now())), 1, 1)) / 100)
)
Use DateDiff() instead of -:
=IIF(
100 / DateDiff("d", DateAdd("d", -Day(Today), Today),
DateSerial(Year(DateAdd("m", -1, Now())), 1, 1)
)
* (DateDiff("d", Today, DateSerial(Year(DateAdd("m", -1, Now())), 1, 1)) / 100) > 1,
1,
100 / DateDiff("d", DateAdd("d",-(Day(Today)), Today),
DateSerial(Year(DateAdd("m", -1, Now())), 1, 1)
)
* DateDiff("d", Today, DateSerial(Year(DateAdd("m", -1, Now())), 1, 1)) / 100
)

How to sum up total sales for all months prior to current month

I am trying to get total sales for all previous months prior to current month.
So if i pull a report from 01.12.2017 to 31.12.2017, the query should pull january to november sales.
I have this query which seems to do it:
SELECT
sum(case when month(create_date) =
month(DATEADD(m, -12, GETDATE()))
and year(create_date) = year(DATEADD(m, -12, GETDATE()))
then Forecast_Revenue else 0 end) as [Sales_12_mo_ago],
sum(case when month(create_date) =
month(DATEADD(m, -11, GETDATE()))
and year(create_date) = year(DATEADD(m, -11, GETDATE()))
then forecast_revenue else 0 end) as [Sales_11_mo_ago],
sum(case when month(create_date) =
month(DATEADD(m, -10, GETDATE()))
and year(create_date) = year(DATEADD(m, -10, GETDATE()))
then forecast_revenue else 0 end) as [Sales_10_mo_ago],
sum(case when month(create_date) =
month(DATEADD(m, -9, GETDATE()))
and year(create_date) = year(DATEADD(m, -9, GETDATE()))
then forecast_revenue else 0 end) as [Sales_9_mo_ago],
sum(case when month(create_date) =
month(DATEADD(m, -8, GETDATE()))
and year(create_date) = year(DATEADD(m, -8, GETDATE()))
then forecast_revenue else 0 end) as [Sales_8_mo_ago],
sum(case when month(create_date) =
month(DATEADD(m, -7, GETDATE()))
and year(create_date) = year(DATEADD(m, -7, GETDATE()))
then forecast_revenue else 0 end) as [Sales_7_mo_ago],
sum(case when month(create_date) =
month(DATEADD(m, -6, GETDATE()))
and year(create_date) = year(DATEADD(m, -6, GETDATE()))
then forecast_revenue else 0 end) as [Sales_6_mo_ago],
sum(case when month(create_date) =
month(DATEADD(m, -5, GETDATE()))
and year(create_date) = year(DATEADD(m, -5, GETDATE()))
then forecast_revenue else 0 end) as [Sales_5_mo_ago],
sum(case when month(create_date) =
month(DATEADD(m, -4, GETDATE()))
and year(create_date) = year(DATEADD(m, -4, GETDATE()))
then forecast_revenue else 0 end) as [Sales_4_mo_ago],
sum(case when month(create_date) =
month(DATEADD(m, -3, GETDATE()))
and year(create_date) = year(DATEADD(m, -3, GETDATE()))
then forecast_revenue else 0 end) as [Sales_3_mo_ago],
sum(case when month(create_date) =
month(DATEADD(m, -2, GETDATE()))
and year(create_date) = year(DATEADD(m, -2, GETDATE()))
then forecast_revenue else 0 end) as [Sales_2_mo_ago],
sum(case when month(create_date) =
month(DATEADD(m, -1, GETDATE()))
and YEAR(create_date) = year(DATEADD(m, -1, GETDATE()))
then forecast_revenue else 0 end) as [Sales_1_mo_ago]
FROM
AMGR_Opportunity
where Objective in ('New Opportunity, New Customer', 'New Opportunity, Existing Customer', 'Repeat Order, Existing Customer', 'Winback Customer')
and Creator_Id in ('TELE1','CRM','TELE2', 'YONELA', 'TELE3', 'Int1')
and status in (1,2)
I however need a overall total for all months prior to current month.
How can i achieve this? I would also like to know if there isnt an easy way to run this query? I can get previous month by doing this query below
SELECT month(create_date) as month_name, sum(Forecast_Revenue) as sum_of_month
FROM dbo.AMGR_Opportunity_Tbl
WHERE Month(Convert(date,Create_date))= Month(DateAdd(month, -1, convert(date,getDate())))
AND Year(Create_Date)=YEAR(getDate()) AND status IN (1,2)
and Objective in ('New Opportunity, New Customer', 'New Opportunity, Existing Customer', 'Repeat Order, Existing Customer', 'Winback Customer')
and Creator_Id in ('TELE1','CRM','TELE2', 'YONELA', 'TELE3', 'Int1')
GROUP BY month(create_date);
Is there a way that i can retrieve all previous 11 month sales using the second query option above?
it seems i figured it out as below:
SELECT SUM(Forecast_Revenue) as Sum_of_month
FROM AMGR_Opportunity
WHERE create_date < DATEADD(mm, DATEDIFF(mm, 0, '20171101'), 0)
AND create_date >= DATEADD(mm, DATEDIFF(mm, 0, '20171101') -12, 0) and Status IN (1,2)
and Creator_Id in ('TELE1','CRM','TELE2', 'YONELA', 'TELE3', 'Int1')
and Objective in ('New Opportunity, New Customer', 'New Opportunity, Existing Customer', 'Repeat Order, Existing Customer', 'Winback Customer')
group by month(create_date)
i changed the -1 to -12 to get all the months and rewrote the query.
Use this
WITH CTE
AS
(
SELECT
DateDif = 'Sales_'+CAST(DATEDIFF(M,create_date,EOMONTH(DATEADD(M,-1,GETDATE()))+1) AS VARCHAR(10))+'_mo_ago',
Forecast_Revenue,
FROM AMGR_Opportunity
WHERE Objective in ('New Opportunity, New Customer', 'New Opportunity, Existing Customer', 'Repeat Order, Existing Customer', 'Winback Customer')
AND Creator_Id in ('TELE1','CRM','TELE2', 'YONELA', 'TELE3', 'Int1')
AND status in (1,2)
)
SELECT
*
FROM CTE
PIVOT
(
SUM(forecast_revenue)
FOR
DateDif IN
(
[Sales_1_mo_ago],
[Sales_2_mo_ago],
[Sales_3_mo_ago],
[Sales_4_mo_ago],
[Sales_5_mo_ago],
[Sales_6_mo_ago],
[Sales_7_mo_ago],
[Sales_8_mo_ago],
[Sales_9_mo_ago],
[Sales_10_mo_ago],
[Sales_11_mo_ago],
[Sales_12_mo_ago]
)
)Pvt

SQL Server SELECT statement compare time in db with current time

This is probably a noob question, but i am a little stuck.
I have this long SELECT query :
SELECT
dbo.Dagplanning.GeldigOp ,
dbo.Trips_Bus.Route ,
dbo.Trips_Bus.TripStart AS BeginUur ,
dbo.Places.Naam AS BeginPlaats ,
dbo.Trips_Bus.TripEnd AS EindUur ,
dbo.Trips_Bus.Duty AS Dienst ,
dbo.Trips_Bus.Block ,
dbo.Personeel.Personeelsnummer ,
dbo.Personeel.Naam ,
dbo.Personeel.VoorNaam ,
dbo.Trips_Bus.OpDay ,
dbo.Trips_Bus.PeriodeID
FROM
dbo.Dagplanning ,
dbo.Personeel ,
dbo.Trips_Bus ,
dbo.Places
WHERE
dbo.Trips_Bus.TripStart >= DATEADD(minute, DATEDIFF(minute, -5, GETDATE()), 0)
AND dbo.Trips_Bus.TripStart <= DATEADD(minute, DATEDIFF(minute, 300, GETDATE()), 0)
AND dbo.Personeel.Personeelsnummer = dbo.Dagplanning.Personeelsnummer
AND dbo.Trips_Bus.Duty = ( dbo.Dagplanning.Rol + '+' + REPLICATE('0', 3 - LEN(dbo.Dagplanning.Dienst)) + dbo.Dagplanning.Dienst )
AND dbo.Trips_bus.Opday LIKE '%" + opdag + "%'
AND dbo.Trips_Bus.PeriodeID = '" + periodeid + "'
AND dbo.Dagplanning.GeldigOp = DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0)
AND dbo.Trips_Bus.TripFrom = dbo.Places.Place
ORDER BY
dbo.Trips_Bus.TripStart ASC
It's actually this part that I have a problem with:
WHERE
dbo.Trips_Bus.TripStart >= DATEADD(minute, DATEDIFF(minute, -5, GETDATE()), 0)
AND dbo.Trips_Bus.TripStart <= DATEADD(minute, DATEDIFF(minute, 300, GETDATE()), 0)
This returns nothing, no error but also no data. De data in the table is stored like this example : 21:50:00
Any idea what I am doing wrong? Please be gentle ;-)
Oh, I am completely new at this, so please keep that in mind!

Creating a calendar in sql

We use a lookup table in a database for grouping weeks, months and years. Sadly the chap who created it has long since gone and the calendar runs out at the end of the year! So I really need to find a way of adding to it! Its set up with the following columns:
I'm not very good in sql(MS2008) to be honest and ive stuggeled with it, we cant set anything new up as everything is built around this!
you can do smth like this, but I don't get why your calendar starts from March?
declare #Date_Start date, #Date_End date
select #Date_Start = '20130101'
select #Date_End = '20131231'
;with
CTE_Dates as (
select #Date_Start as [Date]
union all
select dateadd(dd, 1, [Date])
from CTE_Dates
where [Date] < #Date_End
),
CTE_Calendar as (
select
[Date],
datename(dw, [Date]) as [Day],
datepart(ww, [Date]) as [Week],
datepart(mm, [Date]) as [MonthID],
dateadd(mm, datediff(mm, 0, getdate()), 0) as [Month],
datepart(yy, [Date]) as [YearID],
dateadd(yy, datediff(yy, 0, getdate()), 0) as [Year],
datepart(qq, [Date]) as [QuarterID],
dateadd(qq, datediff(qq, 0, getdate()), 0) as [Quarter]
from CTE_Dates
)
select
row_number() over (order by [Date]) + #Start_ID - 1 as ID,
*
from CTE_Calendar
option (maxrecursion 0)
SQL FIDDLE EXAMPLE

Stored Procedure Throws Error, but running as a query does not

I am aggregating data for reporting, and have a stored procedure that I am trying to insert into a temporary table. If I just run EXEC <stored_proc_name> it works just fine. However, when I try and run the following code:
Create table #TempResults2
(
slscode varchar(3),
intakes_this_month int,
intakes_this_year int,
intakes_last_month int,
intakes_two_months int,
intakes_three_months int,
intakes_four_months int,
intakes_five_months int,
intakes_six_months int,
ships_this_month int,
ships_last_month int,
ships_two_months int,
ships_three_months int,
ships_four_months int,
ships_five_months int,
ships_six_months int,
ships_this_year int
PRIMARY KEY CLUSTERED (SLSCODE)
)
INSERT INTO #TempResults2
EXEC crm.dbo.sp_sales_info
SELECT * FROM #TempResults2
DROP TABLE #TempResults2
I get the following error:
Msg 8114, Level 16, State 1, Procedure sp_sales_info, Line 36
Error converting data type varchar to int.
Warning: Null value is eliminated by an aggregate or other SET operation.
Here is my stored procedure, and I know the problem is somewhere in the first section (where I get all the shipping information) because I can comment that out and just select the intake stuff and the temp table thing works:
UPDATE here is the full stored proc
ALTER PROCEDURE [dbo].[sp_sales_info]
-- Add the parameters for the stored procedure here
AS
BEGIN
DECLARE
#today date, #this_month date, #last_month_start date, #last_month_end date,
#two_months_start date, #two_months_end date, #three_months_start date, #three_months_end date,
#four_months_start date, #four_months_end date, #five_months_start date, #five_months_end date,
#six_months_start date, #six_months_end date, #this_year_start date, #startdate date, #enddate date;
SET #today = GETDATE();
SET #this_month = DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0);
SET #last_month_start = DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) + -1, 0);
SET #last_month_end = DATEADD(MILLISECOND, -3, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) + -1 + 1, 0));
SET #two_months_start = DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) + -2, 0);
SET #two_months_end = DATEADD(MILLISECOND, -3, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) + -2 + 1, 0));
SET #three_months_start = DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) + -3, 0);
SET #three_months_end = DATEADD(MILLISECOND, -3, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) + -3 + 1, 0));
SET #four_months_start = DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) + -4, 0);
SET #four_months_end = DATEADD(MILLISECOND, -3, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) + -4 + 1, 0));
SET #five_months_start = DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) + -5, 0);
SET #five_months_end = DATEADD(MILLISECOND, -3, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) + -5 + 1, 0));
SET #six_months_start = DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) + -6, 0);
SET #six_months_end = DATEADD(MILLISECOND, -3, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) + -6 + 1, 0));
SET #this_year_start = CAST(DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()), 0) as DATE);
SET #startdate = DATEADD(YEAR, -1, GETDATE());
SET #enddate = #today;
SET NOCOUNT ON;
-- Insert statements for procedure here
SELECT
a.*,
d.intakes_this_month,
d.intakes_last_month,
d.intakes_two_months,
d.intakes_three_months,
d.intakes_four_months,
d.intakes_five_months,
d.intakes_six_months,
d.intakes_this_year
FROM
(
SELECT
sum(CASE WHEN a.SHIPDATE >= #this_month THEN 1 ELSE 0 END) AS ships_this_month,
sum(CASE WHEN a.SHIPDATE BETWEEN #last_month_start AND #last_month_end THEN 1 ELSE 0 END) AS ships_last_month,
sum(CASE WHEN a.SHIPDATE BETWEEN #two_months_start AND #two_months_end THEN 1 ELSE 0 END) AS ships_two_months,
sum(CASE WHEN a.SHIPDATE BETWEEN #three_months_start AND #three_months_end THEN 1 ELSE 0 END) AS ships_three_months,
sum(CASE WHEN a.SHIPDATE BETWEEN #four_months_start AND #four_months_end THEN 1 ELSE 0 END) AS ships_four_months,
sum(CASE WHEN a.SHIPDATE BETWEEN #five_months_start AND #five_months_end THEN 1 ELSE 0 END) AS ships_five_months,
sum(CASE WHEN a.SHIPDATE BETWEEN #six_months_start AND #six_months_end THEN 1 ELSE 0 END) AS ships_six_months,
sum(CASE WHEN a.SHIPDATE >= #this_year_start THEN 1 ELSE 0 END) AS ships_this_year,
b.slcode
FROM
(
SELECT
A.ACCOUNT AS CODE,
MIN(CAST(A.BILLDATETIME AS DATE)) AS SHIPDATE
FROM PACWARE.ADS.ARODME A
LEFT OUTER JOIN PACWARE.ADS.PTDME B ON A.PTCODE=B.CODE_
LEFT OUTER JOIN event.dbo.newdate() D ON A.ACCOUNT=D.ACCOUNT
LEFT OUTER JOIN event.dbo.newdate_extras() D2 ON A.ACCOUNT=D2.ACCOUNT
WHERE A.BILLDATETIME>=#startdate
AND A.BILLDATETIME<=#enddate
AND (
(D.NEWDATE>=#startdate AND D.NEWDATE<=#enddate) OR
(D2.NEWDATE IS NOT NULL AND D2.NEWDATE>=#startdate AND D2.NEWDATE<=#enddate) OR
B.MEDICAREID='L7900'
)
AND B.MEDICAREID IN ('A4253','L7900','A9276')
AND A.CATEGORY<>'ID'
Group by
A.ACCOUNT,
B.MEDICAREID,
A.CATEGORY
) a
JOIN event.dbo.patient_dg() b on a.CODE = b.code
GROUP BY b.slcode
) a
JOIN (
SELECT
sum(CASE WHEN a.regdate >= #this_month THEN 1 ELSE 0 END) AS intakes_this_month,
sum(CASE WHEN a.regdate BETWEEN #last_month_start AND #last_month_end THEN 1 ELSE 0 END) AS intakes_last_month,
sum(CASE WHEN a.regdate BETWEEN #two_months_start AND #two_months_end THEN 1 ELSE 0 END) AS intakes_two_months,
sum(CASE WHEN a.regdate BETWEEN #three_months_start AND #three_months_end THEN 1 ELSE 0 END) AS intakes_three_months,
sum(CASE WHEN a.regdate BETWEEN #four_months_start AND #four_months_end THEN 1 ELSE 0 END) AS intakes_four_months,
sum(CASE WHEN a.regdate BETWEEN #five_months_start AND #five_months_end THEN 1 ELSE 0 END) AS intakes_five_months,
sum(CASE WHEN a.regdate BETWEEN #six_months_start AND #six_months_end THEN 1 ELSE 0 END) AS intakes_six_months,
sum(CASE WHEN a.regdate >= #this_year_start THEN 1 ELSE 0 END) AS intakes_this_year,
a.slscode
FROM
event.dbo.patient_dg_lite() a
GROUP BY a.slscode
) d on a.slcode = d.slscode
JOIN event.dbo.employee_slscode b on a.slcode = b.slscode
JOIN event.dbo.employee c on b.employee_id = c.id
END
Your columns aren't in the same ordinal positions slscode is defined first in the CREATE TABLE.
Create table #TempResults2
(
slscode varchar(3),
/*Rest of table*/
ships_this_year int
)
But your SELECT in the stored procedure has it as the last column
SELECT
/* Loads of CASE statements*/
b.slcode
FROM /* ... */
So you are trying to insert the slscode varchar column into the integer ships_this_year column