Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 在存储过程中重新排列日期筛选器_Sql_Sql Server_Logic - Fatal编程技术网

Sql 在存储过程中重新排列日期筛选器

Sql 在存储过程中重新排列日期筛选器,sql,sql-server,logic,Sql,Sql Server,Logic,我有一个存储过程,其中我根据提供的日期列选择了两列。结果应以当前财政年度为基础 从1月5日开始 但是,如何为将设置为的自更新日期过滤器创建逻辑 是否自动开始财政年度而不每年更新存储过程 SELECT SUM([RetailPrice]) AS Retail, SUM([CostPrice]) AS Cost, SUM([MarketPrice]) AS Market FROM PriceDetail WHERE [UserID] = '0029'

我有一个存储过程,其中我根据提供的日期列选择了两列。结果应以当前财政年度为基础 从1月5日开始

但是,如何为将设置为的自更新日期过滤器创建逻辑 是否自动开始财政年度而不每年更新存储过程

SELECT
    SUM([RetailPrice]) AS Retail,
    SUM([CostPrice]) AS Cost,
    SUM([MarketPrice]) AS Market
FROM 
    PriceDetail
WHERE
    [UserID] = '0029'
    AND [Date] >= CONVERT(DATETIME, '2019-01-05')

如果您在一年的前五天从未跑步:

select SUM([RetailPrice]) AS Retail,
       SUM([CostPrice]) AS Cost,
       SUM([MarketPrice]) AS Market
from PriceDetail
where [UserID] = 0029 and
      [Date] >= datefromparts(year(getdate()), 01, 05);
where [UserID] = 0029 and
      [Date] >= datefromparts(year(dateadd(day, -5, getdate())), 01, 05);
如果您需要它在一年的前六天工作:

select SUM([RetailPrice]) AS Retail,
       SUM([CostPrice]) AS Cost,
       SUM([MarketPrice]) AS Market
from PriceDetail
where [UserID] = 0029 and
      [Date] >= datefromparts(year(getdate()), 01, 05);
where [UserID] = 0029 and
      [Date] >= datefromparts(year(dateadd(day, -5, getdate())), 01, 05);

[UserID]=0029
是否能像您预期的那样工作
0029
将被隐式解析为
29
,而
UserID
将被隐式转换为
int
,这意味着如果您具有类似
'0029'
的值,
'29'
'029'
都将被视为具有相同的值。很抱歉,这是一个错误
DATEFROMPARTS
是SQL Server 2012中的一项新功能-因此,如果OP在此版本或更新版本上,此代码将正常工作;如果他使用的是较旧的版本,考虑到财政年度从1月5日开始,我想知道OP是否真的想要
=
;否则他们总是错过一年中的第一天。这当然是OP的逻辑,但值得注意。@marc_s希望OP使用的SQL Server版本不会完全(或即将)失去支持。:)@拉奴:是的-希望如此-但你会感到惊讶(或震惊?)有多少人仍然在2008/2008R2,甚至2005甚至2000(!!)……我既不惊讶也不震惊,@marc_s,但我想说我很担心。