【发布时间】:2020-11-19 00:21:58
【问题描述】:
我有一个存储过程,其中将数字的数量作为参数。我用这样的 where 子句进行查询
select salesrepid, month(salesdate), year(salesdate), salespercentage
from SalesRecords
where salesdate >= DATEADD(month, -@NumberOfMonths, getdate())
例如,如果 @NumberOFmonths 已通过 = 3 并且基于今天的日期,
它应该在我的结果集中带来 9 月 9 日、10 月 10 日和 11 月 11 日。我的查询带来了它,但请求是我需要为那些一个月内没有值的销售代表返回 null,
例如:
salerepid month year salespercentage
232 9 2020 80%
232 10 2020 null
232 11 2020 90%
我怎样才能做到这一点?现在查询只带回两条记录并且不带 10 月份的数据,因为那里没有值,但我希望它返回 10 月份的空值。
【问题讨论】:
标签: sql sql-server datetime where-clause recursive-query