我创建了以下查询,从周六和周日开始每隔7天重复一次。该报告将每七天运行一次。但是,在我们的SFTP文件夹(入站)报告中收到文件的那天,我面临的问题应该是缺少Null = 0的条目。使此自动化过程的主要目标是每个星期日都将执行每七天星期日 示例:
SELECT SubmitterID,SubmitterName,convert(varchar(15), DateReceived, 101) DateReceived,sum(ClaimCount) as TotalCount
FROM FalloutClaimReport
WHERE DateReceived BETWEEN '2019-06-01' AND '2019-06-07'
--ORDER BY COUNT(submitterID) DESC;
GROUP BY submitterid, SubmitterName, convert(varchar(15), DateReceived, 101)
DECLARE @StartDate AS DATETIME
DECLARE @EndDate AS DATETIME
DECLARE @CurrentDate AS DATETIME
SET @StartDate = '2019-06-01' --AND '2019-06-10'
SET @StartDate = '2019-06-07'
SET @EndDate = GETDATE()
SET @CurrentDate = @StartDate
答案 0 :(得分:2)
我不确定您在寻找什么,但是我认为一般的方法是您试图获取一周的数据。
让我们从一些查询开始(这些查询假定美国安装为默认日期为星期一。
SELECT
DATEADD(WEEK, -1, CAST(DATEADD(WEEK, DATEDIFF(WEEK, 0, CAST(GETDATE() AS date)), -1) AS date)) AS TheLastSundayOfTheFullWeek
, DATEADD(WEEK, -1, CAST(DATEADD(WEEK, DATEDIFF(WEEK, 0, CAST(GETDATE() AS date)), +5) AS date)) AS TheLastSaturdayOfTheFullWeek
, CAST(DATEADD(WEEK, DATEDIFF(WEEK, 0, CAST(GETDATE() AS date)), -1) AS date) AS SundayOfTheCurrentWeek
, CAST(DATEADD(WEEK, DATEDIFF(WEEK, 0, CAST(GETDATE() AS date)), +5) AS date) AS SaturdayOfTheCurrentWeek;
这些查询生成以下日期
TheLastSundayOfTheFullWeek TheLastSaturdayOfTheFullWeek SundayOfTheCurrentWeek SaturdayOfTheCurrentWeek
2019-06-30 2019-07-06 2019-07-07 2019-07-13
最后一整周的运行时间为6/30至7/06。当前一周将定义为7/7至7/13。
根据您需要的星期定义,选择合适的两列。
在这种情况下,我构建了一个虚拟表,其中包含报告应具有的所有预期日期(或元素)。然后,我用它来驱动到实际数据表的连接。由于我们不知道给定日期会找到任何行,因此我将这些表与LEFT JOIN连接起来
SELECT
FCR.SubmitterID
, FCR.SubmitterName
, CONVERT(varchar(15), ED.DateReceived, 101) AS DateReceived
, SUM(FCR.ClaimCount) AS TotalCount
FROM
(
-- This logic builds out a list of all the dates that must exist on the report
-- I used the logic for TheLastSundayOfTheFullWeek
SELECT
DATEADD(DAY, D.DayOffset, DATEADD(WEEK, -1, CAST(DATEADD(WEEK, DATEDIFF(WEEK, 0, CAST(GETDATE() AS date)), -1) AS date))) AS DateReceived
FROM
(
-- Generate a series of 7 numbers from 0 t 6
SELECT TOP 7
-1 + ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS rn
FROM
sys.all_columns AS AC
) D(DayOffset)
) AS ED
LEFT OUTER JOIN
dbo.FalloutClaimReport AS FCR
ON FCR.DateReceived = ED.DateReceived
GROUP BY
CONVERT(varchar(15), ED.DateReceived, 101)
, FCR.SubmitterID
, FCR.SubmitterName;
生成的结果集如下
我们在30号或5号没有数据,但查询中仍然有记录。如果您需要在其中使用默认值,请使用ISNULL / COALESCE调用包装该列。
DBFiddle version提供测试沙箱
答案 1 :(得分:0)
奥黛丽,
我会提出两种可能的解决方案。假设您指的是SRS,SSRS。...
1)我将您的SSIS作业设置为每7天通过SQL代理运行一次。它将调用存储过程(SP),然后运行该存储过程并将其写入表中……当调用该SP时,它将运行:
SELECT
SubmitterID,
SubmitterName,
convert(varchar(15), DateReceived, 101) DateReceived,
sum(ClaimCount) as TotalCount
FROM FalloutClaimReport
WHERE cast(DateReceived as date) BETWEEN dateadd(d,-7,cast(getdate() as date)) AND dateadd(d,-1,cast(getdate() as date))
GROUP BY
submitterid,
SubmitterName,
convert(varchar(15), DateReceived, 101)
2)如果您决定采用SSRS路由,则应进行报告订阅,该报告订阅会自动发送给您需要的用户,该用户调用上面的存储过程,并将需要的信息发送给需要它的任何人。 (上面的代码应该足以假设它正在选择所需的内容)