私は通貨で分類された為替レートのリストを持っています。例えばSQLストアドプロシージャ - 日付とif文で区別しようとしています
:
- GBP USDへ|キャプチャの日付:23/02/12 |値:5
- GBPからUSD |キャプチャの日付:22/02/12 |値:3
- GBPからUSD |キャプチャの日付: 21/02/12 |値:3
私は何が起こりたいですか。クエリが実行されると、それは取った最新の日付を自動的に計算し、前の日付をとったときにこれを比較して、これが増えたかどうかを示す値をポストバックします。 、同じ場合 "< - >"
私の現在のクエリは、取得した最新の日付を引き出すことができますが、2番目の最新の日付と値を取り出して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
ええ、大丈夫です。最初の投稿でサンプルデータを編集しましたが、返信ボックスで明確に表示できません。 – Rexxo