存储过程将数据关闭一列 - sql 2012

时间:2014-02-21 17:27:27

标签: sql stored-procedures sql-server-2012

我在sql 2012中运行此存储过程,当时的日期范围是2014-01-01到2014-02-01。我在2014年1月的所有31天都在滚动.SX正确汇总但我的结果集已经过了一天。它没有返回1-1-2014并且正在返回2-1-2014。正在为每个记录应用一个全时图章,所以我正在做一个。这个逻辑适用于某些SP就好了,而其他的则关闭了。

有人有任何建议吗?

USE [Reports]
GO
/****** Object:  StoredProcedure [dbo].[p_rpt_KPI_Channel_Daily_Actual_January14]    Script Date: 2/21/2014 8:51:04 AM ******/

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[p_rpt_KPI_Channel_Daily_Actual_January14]
    @channel varchar(50) --This designed to use either:  Enterprise Account, House Account, Inside Sales Account, Major Account
                         --                              National Account or Retail Record Type
AS

BEGIN
    SET NOCOUNT ON;
    DECLARE @StartDate Date = Convert(varchar(10), DateAdd(d, -50, getdate()), 110)
    DECLARE @Today Date = CONVERT(VARCHAR, GETDATE()-19, 101) -- Today

--DECLARE @channel varchar(50);

DECLARE @local_channel varchar(50);
SET @local_channel = @channel;
--SET @channel = 'Enterprise Account'

  SELECT   

    c.Account_RecordType

    ,Sum(case when a.timewait between DateAdd(d, -50, CONVERT (date, getdate())) and DateAdd(d, -49, CONVERT (date,getdate())) then (a.convotime)/60 else 0 end) as '01/01/2014'
    ,Sum(case when a.timewait between DateAdd(d, -49, CONVERT (date, getdate())) and DateAdd(d, -48, CONVERT (date,getdate())) then (a.convotime)/60 else 0 end) as '01/02/2014'
    ,Sum(case when a.timewait between DateAdd(d, -48, CONVERT (date, getdate())) and DateAdd(d, -47, CONVERT (date,getdate())) then (a.convotime)/60 else 0 end) as '01/03/2014'
    ,Sum(case when a.timewait between DateAdd(d, -47, CONVERT (date, getdate())) and DateAdd(d, -46, CONVERT (date,getdate())) then (a.convotime)/60 else 0 end) as '01/04/2014'
    ,Sum(case when a.timewait between DateAdd(d, -46, CONVERT (date, getdate())) and DateAdd(d, -45, CONVERT (date,getdate())) then (a.convotime)/60 else 0 end) as '01/05/2014'
    ,Sum(case when a.timewait between DateAdd(d, -45, CONVERT (date, getdate())) and DateAdd(d, -44, CONVERT (date,getdate())) then (a.convotime)/60 else 0 end) as '01/06/2014'
    ,Sum(case when a.timewait between DateAdd(d, -44, CONVERT (date, getdate())) and DateAdd(d, -43, CONVERT (date,getdate())) then (a.convotime)/60 else 0 end) as '01/07/2014'
    ,Sum(case when a.timewait between DateAdd(d, -43, CONVERT (date, getdate())) and DateAdd(d, -42, CONVERT (date,getdate())) then (a.convotime)/60 else 0 end) as '01/08/2014'
    ,Sum(case when a.timewait between DateAdd(d, -42, CONVERT (date, getdate())) and DateAdd(d, -41, CONVERT (date,getdate())) then (a.convotime)/60 else 0 end) as '01/09/2014'
    ,Sum(case when a.timewait between DateAdd(d, -41, CONVERT (date, getdate())) and DateAdd(d, -40, CONVERT (date,getdate())) then (a.convotime)/60 else 0 end) as '01/10/2014'
    ,Sum(case when a.timewait between DateAdd(d, -40, CONVERT (date, getdate())) and DateAdd(d, -39, CONVERT (date,getdate())) then (a.convotime)/60 else 0 end) as '01/11/2014'
    ,Sum(case when a.timewait between DateAdd(d, -39, CONVERT (date, getdate())) and DateAdd(d, -38, CONVERT (date,getdate())) then (a.convotime)/60 else 0 end) as '01/12/2014'
    ,Sum(case when a.timewait between DateAdd(d, -38, CONVERT (date, getdate())) and DateAdd(d, -37, CONVERT (date,getdate())) then (a.convotime)/60 else 0 end) as '01/13/2014'
    ,Sum(case when a.timewait between DateAdd(d, -37, CONVERT (date, getdate())) and DateAdd(d, -36, CONVERT (date,getdate())) then (a.convotime)/60 else 0 end) as '01/14/2014'
    ,Sum(case when a.timewait between DateAdd(d, -36, CONVERT (date, getdate())) and DateAdd(d, -35, CONVERT (date,getdate())) then (a.convotime)/60 else 0 end) as '01/15/2014'
    ,Sum(case when a.timewait between DateAdd(d, -35, CONVERT (date, getdate())) and DateAdd(d, -34, CONVERT (date,getdate())) then (a.convotime)/60 else 0 end) as '01/16/2014'
    ,Sum(case when a.timewait between DateAdd(d, -34, CONVERT (date, getdate())) and DateAdd(d, -33, CONVERT (date,getdate())) then (a.convotime)/60 else 0 end) as '01/17/2014'
    ,Sum(case when a.timewait between DateAdd(d, -33, CONVERT (date, getdate())) and DateAdd(d, -32, CONVERT (date,getdate())) then (a.convotime)/60 else 0 end) as '01/18/2014'
    ,Sum(case when a.timewait between DateAdd(d, -32, CONVERT (date, getdate())) and DateAdd(d, -31, CONVERT (date,getdate())) then (a.convotime)/60 else 0 end) as '01/19/2014'
    ,Sum(case when a.timewait between DateAdd(d, -31, CONVERT (date, getdate())) and DateAdd(d, -30, CONVERT (date,getdate())) then (a.convotime)/60 else 0 end) as '01/20/2014'
    ,Sum(case when a.timewait between DateAdd(d, -30, CONVERT (date, getdate())) and DateAdd(d, -29, CONVERT (date,getdate())) then (a.convotime)/60 else 0 end) as '01/21/2014'
    ,Sum(case when a.timewait between DateAdd(d, -29, CONVERT (date, getdate())) and DateAdd(d, -28, CONVERT (date,getdate())) then (a.convotime)/60 else 0 end) as '01/22/2014'
    ,Sum(case when a.timewait between DateAdd(d, -28, CONVERT (date, getdate())) and DateAdd(d, -27, CONVERT (date,getdate())) then (a.convotime)/60 else 0 end) as '01/23/2014'
    ,Sum(case when a.timewait between DateAdd(d, -27, CONVERT (date, getdate())) and DateAdd(d, -26, CONVERT (date,getdate())) then (a.convotime)/60 else 0 end) as '01/24/2014'
    ,Sum(case when a.timewait between DateAdd(d, -26, CONVERT (date, getdate())) and DateAdd(d, -25, CONVERT (date,getdate())) then (a.convotime)/60 else 0 end) as '01/25/2014'
    ,Sum(case when a.timewait between DateAdd(d, -25, CONVERT (date, getdate())) and DateAdd(d, -24, CONVERT (date,getdate())) then (a.convotime)/60 else 0 end) as '01/26/2014'
    ,Sum(case when a.timewait between DateAdd(d, -24, CONVERT (date, getdate())) and DateAdd(d, -23, CONVERT (date,getdate())) then (a.convotime)/60 else 0 end) as '01/27/2014'
    ,Sum(case when a.timewait between DateAdd(d, -23, CONVERT (date, getdate())) and DateAdd(d, -22, CONVERT (date,getdate())) then (a.convotime)/60 else 0 end) as '01/28/2014'
    ,Sum(case when a.timewait between DateAdd(d, -22, CONVERT (date, getdate())) and DateAdd(d, -21, CONVERT (date,getdate())) then (a.convotime)/60 else 0 end) as '01/29/2014'
    ,Sum(case when a.timewait between DateAdd(d, -21, CONVERT (date, getdate())) and DateAdd(d, -20, CONVERT (date,getdate())) then (a.convotime)/60 else 0 end) as '01/30/2014'
    ,Sum(case when a.timewait between DateAdd(d, -20, CONVERT (date, getdate())) and DateAdd(d, -19, CONVERT (date,getdate())) then (a.convotime)/60 else 0 end) as '01/31/2014'


FROM  SalesForce.dbo.SalesForceContact AS b INNER JOIN
      Dossier_Replication.dbo.vwSF_DATA_Contact c ON b.ContactID = c.CONTACTID__C RIGHT OUTER JOIN
      satVRS.dbo.rptNECACallHistory AS a ON b.UserID = a.UserID_Caller

WHERE (b.Platform = 'HandsonVRS') AND (a.timeWait BETWEEN @StartDate AND @Today) 
                                  AND (a.isReport = '1') 
                                  AND (a.NECA_isReport = '1') 
                                  AND (a.ConvoTime > '0')
                                  AND (c.Account_RecordType = @channel)

GROUP BY 
        c.Account_RecordType
END

1 个答案:

答案 0 :(得分:0)

@Y @StartDate Date = Convert(varchar(10),DateAdd(d,-50,getdate()),11​​0)

评估日期为2014年1月2日,2014年2月21日(当您的问题发布时。)@Today使用您的公式评估到2月2日。我想你只需要调整你的补偿。