isnull
, i.e.:
...sum(isnull(amount,0))...
other useful related functions here are nullif
and coalesce
题
I am writing one query in that I am doing sum of the two column. some time it is happening that "amount" column is null so It returns null to me.
But I want if my amount column is null then I want it to be zero.
Note :-- I cannot add default constraint to the table in which amount column is present Bcoz first time when I calculate the sum data is not present in that table.
so I want to add default constraint to that column dynamically
How to achieve that.
Here's My Query.
select
sum((ism.selling_price * som.quantity) + (((tm.tax_amount*(ism.selling_price * som.quantity))/100))) as Amnt,
(sum((ism.selling_price * som.quantity) + (((tm.tax_amount*(ism.selling_price * som.quantity))/100))) - sum(amount)) as total,
sm.sell_order_no,
r.retailer_id,
r.business_name
from RS_Sell_Order_Master as SM
left outer join RS_Sell_Order_Mapping as SOM on SOM.sell_order_no = sm.sell_order_no
left outer join RS_Receipt_Master as RM on rm.sales_order = sm.sell_order_no
left outer join RS_Relationship_Master as R on r.retailer_id = sm.retailer_id
left outer join RS_Inventory_Master as Im on im.product_id = som.product_id
left outer join RS_Inventory_Selling_Master as Ism on ism.product_code = im.product_code
left outer join RS_Sells_Invoice_Info_Master as SIIM on siim.selling_product_id = ism.selling_product_id
left outer join RS_Tax_Master as tm on tm.tax_id = siim.tax_id
where r.retailer_id = 24
group by sm.sell_order_no,r.retailer_id,r.business_name
having (sum((ism.selling_price * som.quantity) + (((tm.tax_amount*(ism.selling_price * som.quantity))/100))) - sum(amount)) > 0
解决方案
isnull
, i.e.:
...sum(isnull(amount,0))...
other useful related functions here are nullif
and coalesce
其他提示
This will give you 0 if is null then.
ISNULL(SUM(COLUMN),0)