简体   繁体   中英

Subtracting two columns from two different temp tables in SQL Server

I am trying to compare data from the previous year and the current year parameter. I created three temp tables called #currentyear , #previous_year , and #comparison_over_previous_year .

#currentyear data is pulling from a stored procedure and I want to pull the previous year data from the stored procedure as well. When trying to pass a DATEADD function while executing a stored procedure, T-SQL doesn't like it. For now, I passed a static data in there.

I want to subtract the Money field from #currentyear temp table and the #previous_year temp table but I am having a tough time.

My code so far is below:

IF OBJECT_ID('tempdb..#currentyear') IS NOT NULL 
    DROP TABLE #currentyear

IF OBJECT_ID('tempdb..#previousyear') IS NOT NULL 
    DROP TABLE #previousyear

IF OBJECT_ID('tempdb..#comparison_over_previous_year') IS NOT NULL 
    DROP TABLE #comparison_over_previous_year

CREATE TABLE #currentyear
(
     [Date] DATE,
     [Carrier] VARCHAR(100),
     [Direct Ceded Written Premium] MONEY,
     [begofmonth] DATE 
)

CREATE TABLE #previousyear
(
     [Date] DATE,
     [Carrier] VARCHAR(100),
     [Direct Ceded Written Premium] MONEY,
     [begofmonth] DATE 
)

CREATE TABLE #comparison_over_previous_year
(
     [Date] DATE,
     [Carrier] VARCHAR(100),
     [Direct Ceded Written Premium] MONEY,
     [begofmonth] DATE 
)

INSERT INTO #currentyear
    EXECUTE [dbo].[rpt_written_premium_extract_CURRENT_YEAR_RPT-4518] 
                  @StartDate = '20160101', @EndDate = '20161130', 
                  @ResEQCarrierCd = 'Palomar'

INSERT INTO #previousyear
    EXECUTE [dbo].[rpt_written_premium_extract_CURRENT_YEAR_RPT-4518] 
           --DATEADD(YEAR,-1,@StartDate) = '20160101'
           --DATEADD(YEAR,-1,@EndDate) = '20161130'
           @StartDate = '20150101', **--Using this static value from now as the script above is not playing nice**
           @EndDate = '20151130', @ResEQCarrierCd = 'Palomar'

INSERT INTO #comparison_over_previous_year
    SELECT 
        cy.[Date], cy.[Carrier],
        cy.[Direct Ceded Written Premium] - py.[Direct Ceded Written Premium],
        cy.[begofmonth]
    FROM
        #currentyear cy
    JOIN 
        #previousyear py ON py.[Carrier] = cy.[Carrier]

----Selects the data in order to dump into SSRS
SELECT *
FROM #comparison_over_previous_year

SO I have two problems here:

  • one is that the DATEADD function cannot be passed through EXEC statement

  • and two is that I have been trying to subtract the value of [Direct Ceded Written Premium] columns from #currentyear and the #previous_year tables. Looks like I would need to group by carrier and begofmonth. Last year may not have all carriers for each month. For example, the month of januray 2015 may not have palomar as a carrier but january 2016 would. So they would not have to be subtracted. if palomar exists on both years, then it would be subtracted. Hope that makes sense.

2016年数据 2016年数据

Here is the outcome that I want to output in SSRS when I export it to excel:

产量

Dateadd function cannot be used as parameter. you can declare a variable like this

Declare @ProcStartdate datetime = DATEADD(YEAR,-1,@startdate)
Declare @ProcEnddate datetime = DATEADD(YEAR,-1,@Enddate)

INSERT INTO #previousyear
EXECUTE [dbo].[rpt_written_premium_extract_CURRENT_YEAR_RPT-4518] 

@ProcStartdate 
,@ProcEnddate 
,@ResEQCarrierCd = 'Palomar'

Why not just use a static variable in your code and set it where you need it?

IF OBJECT_ID('tempdb..#currentyear') IS NOT NULL 
    DROP TABLE #currentyear

IF OBJECT_ID('tempdb..#previousyear') IS NOT NULL 
    DROP TABLE #previousyear

IF OBJECT_ID('tempdb..#comparison_over_previous_year') IS NOT NULL 
    DROP TABLE #comparison_over_previous_year

CREATE TABLE #currentyear
(
     [Date] DATE,
     [Carrier] VARCHAR(100),
     [Direct Ceded Written Premium] MONEY,
     [begofmonth] DATE 
)

CREATE TABLE #previousyear
(
     [Date] DATE,
     [Carrier] VARCHAR(100),
     [Direct Ceded Written Premium] MONEY,
     [begofmonth] DATE 
)

CREATE TABLE #comparison_over_previous_year
(
     [Date] DATE,
     [Carrier] VARCHAR(100),
     [Direct Ceded Written Premium] MONEY,
     [begofmonth] DATE 
)

DECLARE @start datetime, @end datetime

--variables to hold dates in procs
set @start = '20160101'
set @end = '20161130'

INSERT INTO #currentyear
    EXECUTE [dbo].[rpt_written_premium_extract_CURRENT_YEAR_RPT-4518] 
                  @StartDate = @start, @EndDate = @end, 
                  @ResEQCarrierCd = 'Palomar'

--reset them to -1 year of themselves
set @start = DATEADD(YEAR,-1,@start)
set @end = DATEADD(YEAR,-1,@end)

INSERT INTO #previousyear
    EXECUTE [dbo].[rpt_written_premium_extract_CURRENT_YEAR_RPT-4518] 

           @StartDate = @start,
           @EndDate = @end

INSERT INTO #comparison_over_previous_year
    SELECT 
        cy.[Date], cy.[Carrier],
        cy.[Direct Ceded Written Premium] - py.[Direct Ceded Written Premium],
        cy.[begofmonth]
    FROM
        #currentyear cy
    JOIN 
        #previousyear py ON py.[Carrier] = cy.[Carrier]

----Selects the data in order to dump into SSRS
SELECT *
FROM #comparison_over_previous_y

However , your subtraction that you are trying to accomplish relies on only 1 row for each date for each carrier. You should alter your procedure to aggregate the data before you insert it, but here I am doing it into another temp table so you don't have to change the proc.

IF OBJECT_ID('tempdb..#currentyear') IS NOT NULL 
    DROP TABLE #currentyear

IF OBJECT_ID('tempdb..#previousyear') IS NOT NULL 
    DROP TABLE #previousyear

IF OBJECT_ID('tempdb..#currentyearAggregate') IS NOT NULL 
DROP TABLE #currentyearAggregate

IF OBJECT_ID('tempdb..#previousyearAggregate') IS NOT NULL 
DROP TABLE #previousyearAggregate

IF OBJECT_ID('tempdb..#comparison_over_previous_year') IS NOT NULL 
    DROP TABLE #comparison_over_previous_year

CREATE TABLE #currentyear
(
        [Date] DATE,
        [Carrier] VARCHAR(100),
        [Direct Ceded Written Premium] MONEY,
        [begofmonth] DATE 
)

--New Temp Table to hold Aggregated Data
CREATE TABLE #currentyearAggregate
(
        [Date] DATE,
        [Carrier] VARCHAR(100),
        [Direct Ceded Written Premium] MONEY,
        [begofmonth] DATE 
)

CREATE TABLE #previousyear
(
        [Date] DATE,
        [Carrier] VARCHAR(100),
        [Direct Ceded Written Premium] MONEY,
        [begofmonth] DATE 
)

--New Temp Table to hold Aggregated Data
CREATE TABLE #previousyearAggregate
(
        [Date] DATE,
        [Carrier] VARCHAR(100),
        [Direct Ceded Written Premium] MONEY,
        [begofmonth] DATE 
)

CREATE TABLE #comparison_over_previous_year
(
        [Date] DATE,
        [Carrier] VARCHAR(100),
        [Direct Ceded Written Premium] MONEY,
        [begofmonth] DATE 
)

DECLARE @start datetime, @end datetime

--variables to hold dates in procs
set @start = '20160101'
set @end = '20161130'

INSERT INTO #currentyear
    EXECUTE [dbo].[rpt_written_premium_extract_CURRENT_YEAR_RPT-4518] 
                    @StartDate = @start, @EndDate = @end, 
                    @ResEQCarrierCd = 'Palomar'

--reset them to -1 year of themselves
set @start = DATEADD(YEAR,-1,@start)
set @end = DATEADD(YEAR,-1,@end)

INSERT INTO #previousyear
    EXECUTE [dbo].[rpt_written_premium_extract_CURRENT_YEAR_RPT-4518] 
            @StartDate = @start,
            @EndDate = @end

--Sum up the premiums for each day
INSERT INTO #previousyearAggregate
    SELECT
        [Date],
        [Carrier],
        SUM([Direct Ceded Written Premium]) as [Direct Ceded Written Premium],
        [begofmonth]
    FROM 
        #previousyear
    GROUP BY
        [Date],
        [Carrier],
        [begofmonth]

--Sum up the premiums for each day
INSERT INTO #currentyearAggregate
    SELECT
        [Date],
        [Carrier],
        SUM([Direct Ceded Written Premium]) as [Direct Ceded Written Premium],
        [begofmonth]
    FROM 
        #currentyear
    GROUP BY
        [Date],
        [Carrier],
        [begofmonth]

INSERT INTO #comparison_over_previous_year
    SELECT 
        cy.[Date], 
        cy.[Carrier],
        cy.[Direct Ceded Written Premium] - py.[Direct Ceded Written Premium],
        cy.[begofmonth]
    FROM
        #currentyearAggregate cy
    JOIN 
        #previousyearAggregate py ON 
        py.[Carrier] = cy.[Carrier] 
        and py.[Date] = cy.[Date]              --Added this join condition

--Selects the data in order to dump into SSRS
SELECT *
FROM #comparison_over_previous_year

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM