从SQL Server

时间:2017-01-24 20:23:57

标签: sql sql-server sql-server-2008 tsql ssms

我正在尝试比较上一年和当前年度参数的数据。我创建了三个名为#currentyear#previous_year#comparison_over_previous_year的临时表。

#currentyear数据正从存储过程中提取,我也希望从存储过程中提取上一年的数据。在尝试在执行存储过程时传递DATEADD函数时,T-SQL不喜欢它。现在,我在那里传递了静态数据。

我想从#currentyear临时表和#previous_year临时表中减去Money字段,但我很难过。

到目前为止我的代码如下:

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

所以我有两个问题:

  • 一个是DATEADD函数无法通过EXEC语句传递

  • 两个是我一直试图从[Direct Ceded Written Premium]#currentyear表中减去#previous_year列的值。看起来我需要按承运人分组并乞求。去年可能没有每个月的所有运营商。例如,2015年januray月可能没有palomar作为承运人,但2016年1月将。所以他们不必被减去。如果两年都存在palomar,那么它将被减去。希望有道理。

2016 DATA 2016 DATA

当我将其导出到excel时,我想在SSRS中输出结果:

Output

2 个答案:

答案 0 :(得分:0)

Dateadd函数不能用作参数。你可以声明一个像这样的变量

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'

答案 1 :(得分:0)

为什么不在代码中使用静态变量并将其设置在您需要的位置?

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

然而,您尝试完成的减法仅依赖于每个运营商的每个日期的1行。您应该在插入数据之前更改您的过程以聚合数据,但是在这里我将其放入另一个临时表中,因此您不必更改过程。

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