SSRS报告的月末数据

时间:2018-06-20 19:37:35

标签: python sql date reporting-services sql-server-2016

我正在尝试构建一个SSRS报告,该报告从共享数据源调用数据。我需要建立一个包含几个字段的表:

贷款编号,借款人名称,今天余额,余额月末

我创建了两个冗余的数据源;一个用于报告运行的日期(今天是平衡),另一个用于从过去的日期获取数据(平衡月结束)。

在Python中,我可以使用以下逻辑基于elif设置变量以获取前一个月末(不能是假期或周末):

import datetime

today = datetime.date(2018, 6, 19)
monthend = datetime.date(2018, 12, 14)

if today >= datetime.date(2018, 6, 1) and today < datetime.date(2018, 6, 30):
    monthend = datetime.date(2018, 5, 31)
    print(monthend)
elif today >= datetime.date(2018, 7, 1) and today < datetime.date(2018, 7, 31):
    monthend = datetime.date(2018, 6, 29)
    print(monthend)
elif today >= datetime.date(2018, 8, 1) and today < datetime.date(2018, 8, 31):
    monthend = datetime.date(2018, 7, 31)
    print(monthend)
elif today >= datetime.date(2018, 9, 1) and today < datetime.date(2018, 9, 30):
    monthend = datetime.date(2018, 8, 31)
    print(monthend)
elif today >= datetime.date(2018, 10, 1) and today < datetime.date(2018,
                           10, 31):
    monthend = datetime.date(2018, 9, 28)
    print(monthend)
elif today >= datetime.date(2018, 11, 1) and today < datetime.date(2018,
                           11, 30):
    monthend = datetime.date(2018, 10, 31)
    print(monthend)
elif today >= datetime.date(2018, 12, 1) and today < datetime.date(2018,
                           12, 31):
    monthend = datetime.date(2018, 11, 30)
    print(monthend)
elif today >= datetime.date(2019, 1, 1) and today < datetime.date(2019,
                           1, 31):
    monthend = datetime.date(2018, 12, 31)
    print(monthend)

我该如何在报告端或数据库端编写此逻辑以获取上月结束日期参数?

谢谢

1 个答案:

答案 0 :(得分:0)

这也将起作用:

DECLARE @today DATETIME = CONVERT(CHAR(8), GETDATE(), 112) 
SELECT DATEADD(DAY, - DAY(@Today), @Today) [Last Day of Last Month]