求和函数在SQL中添加零和NULLS如何避免

时间:2018-06-04 03:45:17

标签: sql oracle sum

我试图在我的最终结果中只返回三列,这看起来像下面的结果A,但我得到了结果B.我知道这与我的NULLS和0有关但不确定如何排除它们?如果我使用where子句,我需要在我的所有三个标志中都有一个WHERE子句,而不知道该怎么做

结果A

R   DIA             SUD_FLAG_PER    ED_3_FLAG_PER
1   11.11111111     5.555555556      2.777777778

结果B - 这就是我目前正在取得的成就

R   DIA             SUD_FLAG_PER    ED_3_FLAG_PER
1   11.11111111         0   
2   0               5.555555556 
3   0                    0  
4   0                    0          2.777777778

CODE

select


SUM(Diabetes_FLAG)*100/( SELECT percentt
                               from members) as DIA

,SUM(SUD_FLAG)*100/( SELECT percentt
                               from members) as SUD_FLAG_per 

,SUM(ED_3_FLAG)*100/(SELECT percentt
                               from members) as ED_3_FLAG_per                             


From prefinal

Group By Diabetes_FLAG ,SUD_FLAG ,ED_3_FLAG  

这是Prefinal Table的结果。抱怨格式

MED_ID  ED_3_FLAG   SUD_FLAG    DIABETES_FLAG
99017471E       1    0              0
97483445D       0   0
93816600D   1   0   0
97696242G       0   0
95277731G       0   0
95235519A       0   0
90977691G       0   1
93793821A       0   0
96133532A       0   0
94378176C       0   0
94180014F       0   0
93391445F       0   0
98706680C       0   0
96478120E       0   0
92247933C       0   0
98591445F       0   0
98583717D       0   0
97258639C       0   0
90870338A       0   0
93695941A       0   0
91464685C       1   0
95994257F       0   0
94373524E       0   0
91373284C       0   0
97499504C       0   0
91677431D       0   0
99886113D       0   1
92964373F       0   0
90206268E       0   0
93208186A       0   0
92374509A       0   1
95879269E       0   0
92866204A       0   1
93741183C       0   0
90507292A       0   0
92867013C       0   0

members table results 

    PERCENTT
1   36

1 个答案:

答案 0 :(得分:3)

尝试加入表格:

SELECT SUM(Diabetes_FLAG * 100/percentt) as DIA,
       SUM(SUD_FLAG * 100 / percentt) as SUD_FLAG_per,
       SUM(ED_3_FLAG * 100 / percentt) as ED_3_FLAG_per
  FROM prefinal CROSS JOIN members;

SQL Fiddle Demo