按一列返回数据的存储过程-sql 2012
我正在sql 2012中运行此存储过程。当时的日期范围是2014-01-01到2014-02-01。我正在对2014年1月的31天进行滚动计算。SP的求和正确,但我的结果集与之相差一天。它不会返回2014年1月1日,而是返回2014年2月1日。正在对每个录制的应用一个全日制戳记,因此我正在进行一次中间测试。此逻辑在某些SP中工作正常,而在其他SP中则处于关闭状态 有人有什么建议吗按一列返回数据的存储过程-sql 2012,sql,stored-procedures,sql-server-2012,Sql,Stored Procedures,Sql Server 2012,我正在sql 2012中运行此存储过程。当时的日期范围是2014-01-01到2014-02-01。我正在对2014年1月的31天进行滚动计算。SP的求和正确,但我的结果集与之相差一天。它不会返回2014年1月1日,而是返回2014年2月1日。正在对每个录制的应用一个全日制戳记,因此我正在进行一次中间测试。此逻辑在某些SP中工作正常,而在其他SP中则处于关闭状态 有人有什么建议吗 USE [Reports] GO /****** Object: StoredProcedure [dbo].[p
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
@Y@StartDate Date=Convert(varchar(10),DateAdd(d,-50,getdate()),110) 2014年2月21日(您的问题发布时)计算至2014年1月2日。@Today使用您的公式计算至2月2日。我想你只需要调整你的偏移量