有人可以告诉我如何将这些字段添加到此存储过程

时间:2014-11-10 16:26:37

标签: sql sql-server stored-procedures

我有一个为我们构建的SP,它在我们的数据库中执行表的摘要声明。我想要做的是让SP也根据输入的日期提取去年/月的数据。下面是我正在使用的SQL代码。我想要得到的是总和和体积字段,它是根据输入的日期参数减去1个月的总和。

例如: 如果我将2013年10月1日开始和2013年10月31日结束,我将获得2013-10-01至2013-10-31的总数和数量,并在2个单独的栏中将2013-09-01至1013-的总数和数量09-30

代码

      ( 
  @Start                      DATETIME,
  @End                        DATETIME
)
AS
DECLARE
   @reference int,
   @sSQL VARCHAR(2000)
BEGIN
select Convert(datetime,Cast(edi.Creation_dt as varchar(8)),103) as Date, ia.xref_no_tx, la.ldc_acct_no, la.serv_loc_nm
, a.acct_nm, c.company_last_nm
, Case RG.Rate_cd
    When 'DLS' then 'HEDGE'
    When 'STL' then 'STL'
    WHen 'SPOT BILLING' then 'SPOT'
    WHen 'SL SPOT' then 'STL SPOT'
    Else null
    End as Acct_type
      , Convert(datetime,Cast(ia.start_dt as varchar(8)),103)as Start_dt
      , Convert(datetime,Cast(ia.end_dt as varchar(8)),103) as End_dt
      , edi.trans_sub_ty as Inv_type
      , max( case       when la.class_ty_cd = 'COMM' and th.ppa_in = 'N' and th.trans_sub_ty_cd = 'FEES'  then th.trans_qty
                        when la.class_ty_cd = 'MUNI' and th.ppa_in = 'N' and th.trans_sub_ty_cd = 'EXCS' then th.trans_qty
                        when la.class_ty_cd <> 'COMM' and th.ppa_in = 'N' and th.trans_sub_ty_cd = 'BASE' then th.trans_qty
                        else 0 end) as Volume

      , sum(th.trans_am) as Total

from invoice_advise_relate iar, transaction_history th
      ,invoice_advise ia, ldc_account la, account a, customer c, edi_transaction edi
      , (select max(edi_trans_id) as m_edi_trans, relate_id from edi_transaction where class_nm = 'cInvoiceAdvise' group by relate_id) as edic
      , (Select max(rating_group_id) as m_rate, ldc_acct_id from rating_group group by ldc_acct_Id) as C_Rate
      , rating_group rg
where iar.trans_id = th.trans_id
and th.cancel_in = 'N'
and th.trans_ty_cd not in ('PAY', 'ANC')
and iar.inv_adv_id = ia.inv_adv_id
and ia.ldc_acct_id = la.ldc_acct_id
and la.acct_id = a.acct_id 
and a.cust_id = c.cust_id
and la.ldc_acct_no not like 'E%'
and edi.Creation_dt >= convert(varchar,@Start,112)
and edi.Creation_dt <= convert(varchar,@End,112)
and edi.relate_id = ia.inv_adv_id
and edic.m_edi_trans = edi.edi_trans_id
and edi.response_cd = ''
and rg.rating_group_id = C_Rate.M_Rate
and C_Rate.LDC_Acct_Id = la.ldc_Acct_Id
and edi.trans_sub_ty <> '00'
group by edi.Creation_dt, ia.xref_no_tx, la.ldc_acct_no,la.serv_loc_nm, a.acct_nm, c.company_last_nm, ia.start_dt, ia.end_dt,edi.trans_sub_ty, rg.rate_cd

1 个答案:

答案 0 :(得分:0)

首先声明并初始化上个月的开始日期。

DECLARE @PrevStart datetime 
SELECT @PrevStart = dateadd(month, -1, @Start)

在WHERE子句中,将上一个开始日期替换为开始日期,以便包含上个月的数据以及本月的数据。

and edi.Creation_dt >= convert(varchar,@PrevStart,112)
and edi.Creation_dt <= convert(varchar,@End,112)

然后使用CASE语句逻辑过滤本月的上个月数据。

  , max( case       when la.class_ty_cd = 'COMM' and th.ppa_in = 'N' and th.trans_sub_ty_cd = 'FEES'
                        AND edi.Creation_dt >= convert(varchar,@Start,112) then th.trans_qty 
                    when la.class_ty_cd = 'MUNI' and th.ppa_in = 'N' and th.trans_sub_ty_cd = 'EXCS'
                        AND edi.Creation_dt >= convert(varchar,@Start,112) then th.trans_qty
                    when la.class_ty_cd <> 'COMM' and th.ppa_in = 'N' and th.trans_sub_ty_cd = 'BASE'
                        AND edi.Creation_dt >= convert(varchar,@Start,112) then th.trans_qty
                    else 0 end) as Volume

  , sum(CASE WHEN edi.Creation_dt >= convert(varchar,@Start,112) THEN th.trans_am ELSE 0 END) as Total

  , max( case       when la.class_ty_cd = 'COMM' and th.ppa_in = 'N' and th.trans_sub_ty_cd = 'FEES'
                        AND edi.Creation_dt < convert(varchar,@Start,112) then th.trans_qty 
                    when la.class_ty_cd = 'MUNI' and th.ppa_in = 'N' and th.trans_sub_ty_cd = 'EXCS'
                        AND edi.Creation_dt < convert(varchar,@Start,112) then th.trans_qty
                    when la.class_ty_cd <> 'COMM' and th.ppa_in = 'N' and th.trans_sub_ty_cd = 'BASE'
                        AND edi.Creation_dt < convert(varchar,@Start,112) then th.trans_qty
                    else 0 end) as PrevVolume

  , sum(CASE WHEN edi.Creation_dt < convert(varchar,@Start,112) THEN th.trans_am ELSE 0 END) as PrevTotal