문제

I am trying to create a Office Access program to monitor my depts.

I have a table that contains that how much I owe to someone named tbl_depts.

I have another table that contains my payments named tbl_payments.

here is my syntax:

SELECT DISTINCTROW
    tbl_depts.who,
    tbl_depts.dept,
    Sum(tbl_payments.payment) AS [Paid],
    (tbl_depts.dept - [Paid]) AS remaining
FROM tbl_depts INNER JOIN tbl_payments ON tbl_depts.[ID] = tbl_payments.[dept_ID]
GROUP BY tbl_depts.who;

But there is a problem. If there is only one payment for a dept, query showing empty. But this query works perfect if there is two payment for that dept.

What should I do?

도움이 되었습니까?

해결책

how about using LEFT JOIN, when using INNER JOIN be sure that dept_ID exists on both tables.

SELECT DISTINCTROW
    tbl_depts.who,
    tbl_depts.dept,
    Sum(tbl_payments.payment) AS [Paid],
    (tbl_depts.dept - [Paid]) AS remaining
FROM tbl_depts LEFT JOIN tbl_payments 
     ON tbl_depts.[ID] = tbl_payments.[dept_ID]
GROUP BY tbl_depts.who, tbl_depts.dept, (tbl_depts.dept - [Paid]);

다른 팁

Your query above wont even run - the grouping is wrong. This works:

SELECT tbl_depts.ID, tbl_depts.who, tbl_depts.dept, Sum(tbl_payments.payment) AS paid, [dept]-[payment] AS remaining
FROM tbl_payments INNER JOIN tbl_depts ON tbl_payments.dept_ID = tbl_depts.ID
GROUP BY tbl_depts.ID, tbl_depts.who, tbl_depts.dept, [dept]-[payment];
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top