0

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  
3
  • I have reverted your edit; please do not change the entire nature of a question, as that renders any answers and comments incorrect. If you have a second question: ask a second question Commented May 14, 2014 at 8:10
  • but can you please tell me how to use sub query for this please Commented May 14, 2014 at 8:12
  • That is not a clear question; to use a sub-query, you simply write a sub-query Commented May 14, 2014 at 8:24

2 Answers 2

1

isnull, i.e.:

...sum(isnull(amount,0))...

other useful related functions here are nullif and coalesce

Sign up to request clarification or add additional context in comments.

3 Comments

yes, you can read about it at in this post on MSDN: msdn.microsoft.com/en-us/library/ms190349.aspx
thnk you very much for your valuable time Mr. Mark. I appricaite your efforts. It works
hey can you help me for the same query but for sub query error
1

This will give you 0 if is null then.

ISNULL(SUM(COLUMN),0)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.