2015-06-17 99 views
0

我的查詢結果如下 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是空的,如果一些列值變空的,所以如何解決這一問題?

+0

try nz(SUM(balances.balance),0)AND CHECK – Sachu

+0

@Sachu in Sir Sir – Saedawke

回答

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檢查是否總和null.If所以分配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 
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訪問中可用。