區分我有貨幣分類匯率的名單,我需要以某種方式發佈的關於價值的差異。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