所以我现在有一组按货币和价值分组的值,它显示了过去 30 天的值,但是我想按日期搜索,最终使用另一个表中的参数。
;WITH cte AS (SELECT ROW_NUMBER() OVER (ORDER BY date_loaded Asc) AS n, value,
date_loaded, cast(round(value * 0.0001 / 100 * 0.5 + (value * 0.0001),4)AS dec(12,4)) as buy_rate,
cast(round(value * 0.0001 / 100 * -0.5 + (value * 0.0001), 4) AS dec(12,4)) as sell_rate
FROM texchange_rate WHERE source_currency_code = @source_currency_code
and target_currency_code = @target_currency_code
and date_loaded between dateadd(day, datediff(day, 0 ,getdate())-30, 0) and getdate())
SELECT t2.value, t2.date_loaded, @source_currency_code as source_currency_code,
@target_currency_code as target_currency_code, t2.buy_rate, t2.sell_rate
FROM cte t2 LEFT JOIN cte t1 ON t2.n = t1.n + 1
WHERE t2.value <> ISNULL(t1.value, -1)
order by date_loaded desc
END
我想定义 dateadd 从单独的表中搜索的天数,这可能吗?例如
从
dateadd(day, datediff(day, 0 ,getdate())-30, 0) and getdate())
到
dateadd(day, datediff(day, 0 ,getdate())@dayparameter, 0) and getdate())
为了让它工作,我尝试声明@dayparameter(类似于这里 - http://msdn.microsoft.com/en-us/library/ms186819%28v=sql.100%29.aspx,尽管这是为了server 2008) 在存储过程的开头,并将其放在 dateadd 中会给出错误
消息 102,级别 15,状态 1,过程 proc_getCurrencyHistory,第 48 行“@dayparameter”附近的语法不正确。
希望这是有道理的。