2016-03-30 12 views
0

どうすればいいですか?迷っています。2行+グループ化を比較する

╔════════╦══════════╦══════════╗ 
║ ITEM ║ PRICE ║ DATE  ║ 
╠════════╬══════════╬══════════╣ 
║ Dollar ║  60 ║ 1.3.2016 ║ 
║ Dollar ║  50 ║ 2.3.2016 ║ 
║ Bound ║  100 ║ 1.3.2016 ║ 
║ Bound ║  110 ║ 2.3.2016 ║ 
║ Euro ║  600 ║ 1.3.2016 ║ 
║ Euro ║  580 ║ 3.3.2016 ║ 
╚════════╩══════════╩══════════╝ 

出力は、前の行

╔════════╦══════════╦════════════════════════╗ 
║ ITEM ║ PRICE ║ DATE  ║ Differnece ║ 
╠════════╬══════════╬════════════════════════╣ 
║ Dollar ║  50 ║ 2.3.2016 ║ -10  ║ 
║ Bound ║  110 ║ 2.3.2016 ║ 10  ║ 
║ Euro ║  580 ║ 3.3.2016 ║ -20  ║ 
╚════════╩══════════╩════════════════════════╝ 
+0

実際の質問はどのようなものですから、最後の価格との価格差で、各タイプから一つの項目を表示する必要がありますか? –

+0

申し訳ありません、あなたは今それを見ることができます – gaidi

+0

これまでに何を試しましたか?答えが「私が失われた」と考える以外に何もない場合は、プログラマを雇ってそれを行う必要があります。 –

答えて

0
select name as 'Item', 
     price as 'Price', 
     (select max(date) from table where name like '%dollar%') as 'Date', 
     (select top 1 price from table where name like '%dollar%') - (select top 1 price from table where id< (select max(id) from table where name like '%dollar%')and name like '%dollar%' order by id desc) as 'difference' 
     from table where name like '%dollar%' 
union all --now just repeat the code above and switch the like as you want 
select name as 'Item', 
     price as 'Price', 
     (select max(date) from table where name like '%bound%') as 'Date', 
     (select top 1 price from table where name like '%bound%') - (select top 1 price from table where id< (select max(id) from table where name like '%bound%')and name like '%bound%' order by id desc) as 'difference' 
     from table where name like '%bound%' 
+0

これは遅いクエリxDです。何か速いプログラマを雇用したい場合は、 – Lucas

+0

が動作しませんでした。 – gaidi