1

これは私のコードです:

select row_number() over (order by cte3.update desc) as row_num,

            (select cte3.update from last_two_items 
             where idOrder = 1 and 
             cte3.toy_id = toy_id
            ) as update,

            cte3.price_previous as price_previous,
            cte3.price_current as price_current,
            cte3.id as id,
            cte3.toy_id as toy_id

    from last_two_items as cte3
    where idOrder = 2

last_two_items という一時テーブルがあります。この一時テーブルには、古い価格、新しい価格、更新日、idOrder などのいくつかの列があります。

次のようになります。

old_price   new_price    date of update             idOrder         toy_id
0           0.16         2013-08-06 10:03:41.700    1               123
0.16        0.08         2013-08-06 10:02:28.850    2               123

クエリの結果として、idOrder が 2 のレコードから古い価格と新しい価格を取得したいのですが、idOrder = 1 のレコードから更新日を取得します。おもちゃは両方のレコードで同じです。

ただし、私が持っているコード (この質問の最初のコード) では、idOrder = 2 のレコードしか取得できません。

これを行う方法?

4

2 に答える 2

0

機能しているにもかかわらず、他のアプローチを使用することをお勧めします。

select lti2.old_price, lti2.new_price, lti1.date_of_update
from last_two_items lti1
left join last_two_items lti2
where lti1.idOrder = 1 and lti2.idOrder = 2 

サブセレクトなしのかなり簡単なこと。

于 2013-08-12T09:32:59.807 に答える