Access Database Total Field不计算空记录?

时间:2015-06-17 10:05:35

标签: sql database ms-access-2007 ms-access-2010

我的查询结果如下 enter image description here

查询:

SELECT reservations.customerid, 
 (SELECT SUM(balances.balance) FROM balances 
  WHERE balances.customer_id = reservations.customerid) 
                               AS Preveious_balance ,
 (SELECT SUM(services.Amount_due) 
   FROM services 
   WHERE services.customer_id = reservations.customerid 
     AND services.status=0) AS Service_due ,  
(SELECT SUM(foods.Amount_due) 
    FROM foods WHERE foods.customer_id = reservations.customerid 
    AND foods.status=0) AS Food_due,
((due_nights.Due_nights - reservations.billed_nights) * rooms.rate) 
  as Accomendation,
(Preveious_balance + Service_due  +  Food_due + Accomendation) 
    AS Total_due 

FROM reservations, due_nights, rooms 
  WHERE reservations.customerid = due_nights.customerid 
 AND reservations.roomno = rooms.roomno

如果其他一些列值为空则Total_due为空,那么如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

SELECT reservations.customerid, 
     (SELECT SUM (balances.balance)
         FROM balances WHERE balances.customer_id = reservations.customerid)
           AS Preveious_balance ,
     (SELECT SUM(services.Amount_due)
            FROM services WHERE services.customer_id = reservations.customerid 
             AND services.status=0) AS Service_due ,  
    (SELECT SUM(foods.Amount_due)
            FROM foods WHERE foods.customer_id = reservations.customerid 
           AND foods.status=0) AS Food_due,
    ((due_nights.Due_nights - reservations.billed_nights) * rooms.rate)
          as Accomendation,
    (NZ(Preveious_balance,0) + NZ(Service_due,0)  +  NZ(Food_due,0) + NZ(Accomendation,0)) AS Total_due 
  FROM reservations, due_nights, rooms 
WHERE reservations.customerid = due_nights.customerid 
AND reservations.roomno = rooms.roomno

您可以NZ function of Access检查总和是否为空。如果是,则分配0.它将解决您的问题

如果不是访问

SELECT reservations.customerid, 
 (SELECT IIf(Sum(balances.balance) Is Null, 0, Sum(balances.balance))
     FROM balances WHERE balances.customer_id = reservations.customerid)
       AS Preveious_balance ,
 (SELECT IIf(Sum(services.Amount_due) Is Null, 0, Sum(services.Amount_due))
        FROM services WHERE services.customer_id = reservations.customerid 
         AND services.status=0) AS Service_due ,  

 (SELECT IIf(Sum(foods.Amount_due) Is Null, 0, Sum(foods.Amount_due))
        FROM foods WHERE foods.customer_id = reservations.customerid 
       AND foods.status=0) AS Food_due,
((due_nights.Due_nights - reservations.billed_nights) * rooms.rate)
      as Accomendation,
(Preveious_balance + Service_due  +  Food_due + Accomendation) AS Total_due 
   FROM reservations, due_nights, rooms 
 WHERE reservations.customerid = due_nights.customerid 
 AND reservations.roomno = rooms.roomno

答案 1 :(得分:0)

SELECT reservations.customerid, 
 (SELECT IIf(Sum(balances.balance) Is Null, 0, Sum(balances.balance))
     FROM balances WHERE balances.customer_id = reservations.customerid)
       AS Preveious_balance ,
 (SELECT IIf(Sum(services.Amount_due) Is Null, 0, Sum(services.Amount_due))
        FROM services WHERE services.customer_id = reservations.customerid 
         AND services.status=0) AS Service_due ,  

 (SELECT IIf(Sum(foods.Amount_due) Is Null, 0, Sum(foods.Amount_due))
        FROM foods WHERE foods.customer_id = reservations.customerid 
       AND foods.status=0) AS Food_due,
((due_nights.Due_nights - reservations.billed_nights) * rooms.rate)
      as Accomendation,
(Preveious_balance + Service_due  +  Food_due + Accomendation) AS Total_due 
   FROM reservations, due_nights, rooms 
 WHERE reservations.customerid = due_nights.customerid 
 AND reservations.roomno = rooms.roomno

如果您使用ADO.NET或其他编程来检查空值,这是正确的,因为Nz仅在MS访问中可用。