SQL Server - 前一天的数据检索在哪里

时间:2014-10-21 21:30:43

标签: sql-server sql-server-2012 sql-server-2012-express

我正在从一个视图中检索仪表板的数据,我需要一些帮助我的查询来检索以下条件满足的昨天的数据:

当'星期一'检索星期五的数据时。 当'星期二'从星期六到星期一检索数据时, 当“其他日子”刚刚检索前一天时,

但还有另一个条件: 排除“前一天”数据WHERE“DESP ...”为空。(因为这会使百分比偏斜)

这是我到目前为止所做的:

SELECT CORD_DocumentCode
,OpenDate
,datedue
,DESP_PostedDate
,COUNT(CORD_DocumentCode) AS Order_Count
,SUM(CASE WHEN InFullAndOneTime = 2 THEN 1 ELSE 0 END) AS Difot_count
,SUM(CASE WHEN InFullAndOneTime = 2 THEN 1 ELSE 0 END) * 1.0 /COUNT(CORD_DocumentCode) AS DIFOT
FROM DIFOTIS_View
WHERE (OpenDate >= CASE WHEN DATENAME(dw, CONVERT(CHAR(8), GETDATE(), 112)) LIKE 'Monday' THEN CONVERT(CHAR(8), DATEADD(dd, - 2, GETDATE()), 112) 
                         WHEN DATENAME(dw, CONVERT(CHAR(8), GETDATE(), 112)) LIKE 'Tuesday' THEN CONVERT(CHAR(8), DATEADD(dd, - 3, GETDATE()), 112) ELSE CONVERT(CHAR(8), 
                         DATEADD(dd, - 1, GETDATE()), 112) END) --AND) (OpenDate < CONVERT(CHAR(8), GETDATE(), 112)) 


Group by OpenDate,CORD_DocumentCode, datedue, DESP_PostedDate
order by OpenDate

这将显示昨天的数据,因为今天是'星期三',结果是 15.4%(或4/26),而我希望它显示80%(或4/5)

我尝试过添加

((Datediff(day,Opendate,getdate())=1) and DESP_PostedDate is not null)

但它只显示21Oct的数据。

你有什么建议我该做什么? 请查看以下星期五的数据供您阅读。

感谢所有帮助。感谢

埃里克

CORD_DocumentCode      OpenDate     datedue     DESP_PostedDate Order_Count Difot_count DIFOT
CASW92195               2014-10-19  2014-10-19  2014-10-20  1   1   1.000000000000
CASW92196               2014-10-19  2014-10-19  2014-10-20  1   1   1.000000000000
CASW92197               2014-10-19  2014-10-19  2014-10-20  1   1   1.000000000000
CASW92198               2014-10-19  2014-10-19  2014-10-20  1   1   1.000000000000
CASW92199               2014-10-19  2014-10-19  2014-10-20  1   1   1.000000000000
CASW92200               2014-10-19  2014-10-19  NULL        1   0   0.000000000000
CASW92201               2014-10-20  2014-10-20  2014-10-20  1   1   1.000000000000
CASW92202               2014-10-20  2014-10-20  2014-10-20  1   1   1.000000000000
CASW92203               2014-10-20  2014-10-20  2014-10-21  1   1   1.000000000000
CASW92204               2014-10-20  2014-10-20  2014-10-21  1   1   1.000000000000
CASW92205               2014-10-20  2014-10-20  2014-10-21  1   1   1.000000000000
CASW92206               2014-10-20  2014-10-20  2014-10-21  1   1   1.000000000000
CORD37188               2014-10-20  2014-10-20  NULL        1   0   0.000000000000
CORD37189               2014-10-20  2014-10-20  2014-10-20  1   0   0.000000000000
CORD37190               2014-10-20  2014-10-20  2014-10-20  1   0   0.000000000000
CORD37191               2014-10-20  2014-10-20  2014-10-21  1   0   0.000000000000
CORD37192               2014-10-20  2014-10-20  2014-10-20  1   0   0.000000000000
CORD37193               2014-10-20  2014-10-20  2014-10-20  1   0   0.000000000000
CORD37195               2014-10-20  2014-10-20  2014-10-20  1   0   0.000000000000
CORD37196               2014-10-20  2014-10-20  2014-10-20  1   0   0.000000000000
CORD37197               2014-10-20  2014-10-20  2014-10-21  1   0   0.000000000000
CORD37198               2014-10-20  2014-10-20  2014-10-21  1   0   0.000000000000
CORD37199               2014-10-20  2014-10-20  2014-10-20  1   0   0.000000000000
CORD37200               2014-10-20  2014-10-20  NULL        1   0   0.000000000000
CORD37211               2014-10-20  2014-10-20  2014-10-21  1   1   1.000000000000
CORD37216               2014-10-20  2014-10-20  2014-10-21  1   1   1.000000000000
CASW92207               2014-10-21  2014-10-21  2014-10-21  1   1   1.000000000000
CASW92208               2014-10-21  2014-10-21  2014-10-21  1   1   1.000000000000
CASW92209               2014-10-21  2014-10-21  2014-10-21  1   1   1.000000000000
CASW92210               2014-10-21  2014-10-21  NULL        1   0   0.000000000000
CASW92211               2014-10-21  2014-10-21  NULL        1   0   0.000000000000
CASW92212               2014-10-21  2014-10-21  NULL        1   0   0.000000000000
CASW92213               2014-10-21  2014-10-21  NULL        1   0   0.000000000000
CORD37218               2014-10-21  2014-10-21  2014-10-21  1   1   1.000000000000
CORD37220               2014-10-21  2014-10-20  2014-10-21  1   0   0.000000000000
CORD37221               2014-10-21  2014-10-21  NULL        1   0   0.000000000000
CORD37222               2014-10-21  2014-10-21  NULL        1   0   0.000000000000
CORD37225               2014-10-21  2014-10-21  NULL        1   0   0.000000000000
CORD37227               2014-10-21  2014-10-21  NULL        1   0   0.000000000000
CORD37228               2014-10-21  2014-10-21  NULL        1   0   0.000000000000
CORD37229               2014-10-21  2014-10-21  NULL        1   0   0.000000000000
CORD37230               2014-10-21  2014-10-21  NULL        1   0   0.000000000000
CORD37231               2014-10-21  2014-10-21  NULL        1   0   0.000000000000
CORD37232               2014-10-21  2014-10-21  NULL        1   0   0.000000000000
CORD37233               2014-10-21  2014-10-21  NULL        1   0   0.000000000000
CORD37234               2014-10-21  2014-10-21  NULL        1   0   0.000000000000
CORD37235               2014-10-21  2014-10-21  NULL        1   0   0.000000000000
CORD37236               2014-10-21  2014-10-21  NULL        1   0   0.000000000000
CORD37237               2014-10-21  2014-10-21  NULL        1   0   0.000000000000
CORD37238               2014-10-21  2014-10-21  NULL        1   0   0.000000000000
CORD37239               2014-10-21  2014-10-21  NULL        1   0   0.000000000000
CORD37241               2014-10-21  2014-10-21  NULL        1   0   0.000000000000

1 个答案:

答案 0 :(得分:0)

我花了一段时间,但是在Shiv的帮助下解决了这个问题。来自另一个论坛。 我在这里发布答案以供将来参考。

/*Main Query */
declare @ReportDate datetime = getdate()


SELECT    COUNT(CORD_DocumentCode) as [count], SUM(CASE WHEN InFullAndOneTime = 2 THEN 1 ELSE 0 END) AS Difot_count, 
                         SUM(CASE WHEN InFullAndOneTime = 2 THEN 1 ELSE 0 END) * 1.0 / COUNT(CORD_DocumentCode) AS DIFOT
FROM            DIFOTIS_View
WHERE  cast(datepart(dw,OpenDate) as varchar) in       
(SELECT * FROM dbo.CSVToTable(Case
when datepart(dw,@ReportDate) =7 and DESP_PostedDate is NOT null then '6' 
when datepart(dw,@ReportDate) =6 and DESP_PostedDate is NOT null then '5'  
when datepart(dw,@ReportDate) =5 and DESP_PostedDate is NOT null then '4'  
when datepart(dw,@ReportDate) =4 and DESP_PostedDate is NOT null then '3'  
when datepart(dw,@ReportDate) =2 and DESP_PostedDate is NOT null then '6'  
when datepart(dw,@ReportDate) =3 and DESP_PostedDate is NOT null then '7,1,2'
when datepart(dw,@ReportDate) =1 and DESP_PostedDate is NOT null then '7' Else NULL END)) and opendate>=DATEADD(day, -7,@ReportDate)

/* Function Creation*/ 
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[CSVToTable] (@InStr VARCHAR(MAX))
RETURNS @TempTab TABLE
   (id int not null)
AS
BEGIN
    ;-- Ensure input ends with comma
    SET @InStr = REPLACE(@InStr + ',', ',,', ',')
    DECLARE @SP INT
DECLARE @VALUE VARCHAR(1000)
WHILE PATINDEX('%,%', @INSTR ) <> 0 
BEGIN
   SELECT  @SP = PATINDEX('%,%',@INSTR)
   SELECT  @VALUE = LEFT(@INSTR , @SP - 1)
   SELECT  @INSTR = STUFF(@INSTR, 1, @SP, '')
   INSERT INTO @TempTab(id) VALUES (@VALUE)
END
    RETURN
END
GO

干杯 ħ