2017-01-08 6 views
2

私は行に1つの複雑な計算プロセスを持っています 私の条件がtrueの場合は の2つのフィールドの乗算(ヴァル2) そして私はこの問題は、次に偽に多分等しい任意condotion これは私のコード行間の複雑な計算プロセスのいずれかがnullになる可能性があります

あるがnullに等しい結果である 100 /合計値の割合((VAL1 + VAL2)* 10%)を算出します
SELECT DISTINCT 
     r.paymentDate as "Payment Date" , 
     r.recivedID as "Recived ID" , 
     f.farmerName as "Farmer Name" , 
     ra.tips as "percentage ",  


      (((SELECT SUM(od.price * od.kilo) FROM orderDetailsTBL od WHERE od.recivedID = r. recivedID AND r.status = @STATUS AND od.calcType = 'k') + 
      (SELECT SUM(od.quant * od.price) FROM orderDetailsTBL od WHERE od.recivedID = r. recivedID AND r.status = @STATUS AND od.calcType = 'n') ) * ra.tips)/100 
       as [amount] 
FROM recivedTBL r 
JOIN farmerTBL f ON f.farmerID = r.farmerID 
JOIN recivedAccTBL ra ON ra.recivedID = r.recivedID 
JOIN orderDetailsTBL od ON od.recivedID = r.recivedID 
WHERE (r.paymentDate BETWEEN @D1 AND @D2) AND r.status = @STATUS 

任意のサブクエリ条件がfalseに等しい場合、このクエリは値を返しません for exampel:

​​

falseが返された値に等しい。このサブクエリは

答えて

3

nullになります場合は、これが何をしたいですか?

SELECT r.paymentDate as "Payment Date", r.recivedID as "Recived ID", 
     f.farmerName as "Farmer Name", ra.tips as "percentage",  
     SUM((CASE WHEN od.calcType = 'k' THEN od.price * od.kilo 
       WHEN od.calcType = 'n' THEN od.quant * od.price 
      END) * ra.tips)/100 as [amount] 
FROM recivedTBL r JOIN 
    farmerTBL f 
    ON f.farmerID = r.farmerID JOIN 
    recivedAccTBL ra 
    ON ra.recivedID = r.recivedID JOIN 
    orderDetailsTBL od 
    ON od.recivedID = r.recivedID 
WHERE r.paymentDate BETWEEN @D1 AND @D2 AND r.status = @STATUS 
GROUP BY r.paymentDate, r.recivedID, f.farmerName, ra.tips; 
+0

はい、それは、あなたに感謝 –

0

がで「量」のためにあなたの全体の計算をラップ:あなたの計算がNULLを返した場合

COALESCE(<your-calc>, 0) as [amount] 

ゼロを返すこと

関連する問題