2つの市場データテーブル間で外部結合を行っています。2つのテーブルの取引量は異なる方法で報告されているため、別々にクエリを実行してから結果を合計する必要があります。
問題は、2番目のクエリは、頻繁には発生しないため、結果が返されない場合がある取引条件に対するものであるということです。したがって、c.volumeには値があり、md.volumeはnullであり、加算の結果としてnullになります。
nullを0として扱うにはどうすればよいですか?
select
c.the_time, c.symbol, c.volume + md.volume
from
-- These are single shares
(select
(time_stamp / 100000) as the_time, symbol,
sum(size) as volume
from
[20160510]
where
price_field = 0
and (size > 0
and tradecond != 0)
group by
(time_stamp / 100000), symbol) as c
full outer join
(select
d.the_time, d.symbol, d.volume as volume
from
-- These are single shares when multiplied by -1
(select
(time_stamp / 100000) as the_time, symbol, sum(size) * -1 as volume
from
[20160510]
where
price_field = 0
and size < 0
group by
(time_stamp / 100000), symbol) as d) as md on md.the_time = c.the_time
and md.symbol = c.symbol
COALESCEの使用を検討する必要があります。ISNULLを使用することもできますが、COALESCEはANSI標準関数であることに注意してください。参照リンクを参照してください。
COALESCEを使用した後のクエリは次のようになります
select
c.the_time, c.symbol, COALESCE(c.volume,0) + COALESCE(md.volume ,0)
from
-- These are single shares
(select
(time_stamp / 100000) as the_time, symbol,
sum(size) as volume
from
[20160510]
where
price_field = 0
and (size > 0
and tradecond != 0)
group by
(time_stamp / 100000), symbol) as c
full outer join
(select
d.the_time, d.symbol, d.volume as volume
from
-- These are single shares when multiplied by -1
(select
(time_stamp / 100000) as the_time, symbol, sum(size) * -1 as volume
from
[20160510]
where
price_field = 0
and size < 0
group by
(time_stamp / 100000), symbol) as d) as md on md.the_time = c.the_time
and md.symbol = c.symbol
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加