区分我有货币分类汇率的名单,我需要以某种方式发布的关于价值的差异。SQL存储过程 - 试图通过日期和if语句
例如:
- GBP USD至|截获日期:23/02/12 |值:5
- GBP USD至|截获日期:22/02/12 |值:3
- GBP USD至|截获日期: 21/02/12 |值:3
我希望发生什么;当查询运行时,它会自动计算出最新的日期,比较这一点,当前一个日期采取,并回传一个值,说如果这已经增加,即如果增加然后“^”,如果减少“v” ,如果相同“< - >”。
我目前的查询可以提取最新的日期,但我仍然需要做一个子查询来取出第二个最新的日期和值,然后发布一个if语句。
难道我吠叫了我的方法正确的树?
这是我的代码。
SELECT
distinct t.source_currency_code, t.target_currency_code,
t.entry_no as entry_no,
'(' + t.source_currency_code + ') ' + s.currency_name as source_currency_name,
'(' + t.target_currency_code + ') ' + x.currency_name as target_currency_name,
t.value_amount as value_amount,
t.uplift_percent as uplift,
t.date_loaded as date_loaded
from texchange_rate t, tcurrency s, tcurrency x
where
s.currency_code = t.source_currency_code and
x.currency_code = t.target_currency_code and
t.date_loaded in
(
SELECT max(date_loaded) from texchange_rate tt
where t.source_currency_code = tt.source_currency_code
and t.target_currency_code = tt.target_currency_code
)
order by source_currency_code, target_currency_code
SELECT
distinct t.source_currency_code, t.target_currency_code,
t.entry_no as entry_no,
'(' + t.source_currency_code + ') ' + s.currency_name as source_currency_name,
'(' + t.target_currency_code + ') ' + x.currency_name as target_currency_name,
t.value_amount as value_amount,
t.uplift_percent as uplift,
t.date_loaded as date_loaded2
from texchange_rate t, tcurrency s, tcurrency x
where
s.currency_code = t.source_currency_code and
x.currency_code = t.target_currency_code and
t.date_loaded in
(
SELECT max(date_loaded) from texchange_rate tt
where t.source_currency_code = tt.source_currency_code
and t.target_currency_code = tt.target_currency_code
)
and
t.value_amount in
(
SELECT value_amount from texchange_rate tt
where DATEDIFF(day, date_loaded, getdate()) < date_loaded
and t.source_currency_code = tt.source_currency_code
and t.target_currency_code = tt.target_currency_code
)
order by source_currency_code, target_currency_code
一些样本数据:
4366 GBP USD 15986 23/01/2012 13:42:02
4337 GBP USD 15600 17/10/2011 12:37:58
4312 GBP USD 15500 22/08/2011 14:00:01
4287 GBP USD 15500 21/08/2011 14:00:01
简单地说,是的。您需要获取最新值,并返回到最新日期!=最新值的日期。或者,你可以递归找到所有的差异对并采取最后一个。从每个表格获取一些样本数据或每个表格的至少一个定义会很有帮助。 – Sorpigal 2012-02-24 11:53:01
嗯没关系。我在我的初始文章中编辑了一些示例数据,因为我无法在答复框中清楚地做到这一点。 – Rexxo 2012-02-24 12:10:28