SQL語法如下
SELECT b.stock_id,b.普通股股本,b.股本合計,b.歸屬於母公司業主之權益合計,(b.權益總計+b.權益總額) as 權益總計2 ,i.本期淨利(淨損),i.營業利益(損失),c.投資活動之淨現金流入(流出),c.營業活動之淨現金流入(流出),p.收盤價,p.date FROM balance_sheet b INNER JOIN price p ON b.stock_id = p.stock_id INNER JOIN income_sheet i on b.stock_id = i.stock_id INNER JOIN cash_flows c on b.stock_id = c.stock_id where p.date = b.date and b.stock_id='1101'
請問如何(b.權益總計+b.權益總額) as 權益總計2
(說明 b.權益總計 和 b.權益總額 有部分資料是NULL )
上面SQL語句相加後 權益總計2的資料都是NULL
謝謝
這MSSQL的解法..
先判斷是否為null
是的話先轉為0
SELECT b.stock_id
,b.普通股股本
,b.股本合計
,b.歸屬於母公司業主之權益合計
,(isNull(b.權益總計,0) + isNull(b.權益總額,0) ) as 權益總計2
,i.本期淨利(淨損)
,i.營業利益(損失)
,c.投資活動之淨現金流入(流出)
,c.營業活動之淨現金流入(流出)
,p.收盤價
,p.date
FROM balance_sheet b
INNER JOIN price p ON b.stock_id = p.stock_id
INNER JOIN income_sheet i on b.stock_id = i.stock_id
INNER JOIN cash_flows c on b.stock_id = c.stock_id
where p.date = b.date
and b.stock_id='1101'
MS SQL :
case when isNumeric(b.權益總計)=1 and isNumeric(b.權益總額)=1
then b.權益總計 + b.權益總額
else null end as 權益總計2
補充Oracle語法
SELECT b.stock_id,
b.普通股股本,
b.股本合計,
b.歸屬於母公司業主之權益合計,
(NVL (b.權益總計, 0) + NVL (b.權益總額, 0)) AS 權益總計2,
i.本期淨利(淨損),
i.營業利益(損失),
c.投資活動之淨現金流入(流出),
c.營業活動之淨現金流入(流出),
p.收盤價,
p.date
FROM balance_sheet b
INNER JOIN price p ON b.stock_id = p.stock_id
INNER JOIN income_sheet i ON b.stock_id = i.stock_id
INNER JOIN cash_flows c ON b.stock_id = c.stock_id
WHERE p.date = b.date AND b.stock_id = '1101'