2

I have two tables Projects and Invoices. The database is MS Access ( mdb )

Project table has fields

  1. ProjectID
  2. ProjectName
  3. ProjectStatus

Invoices table has the fields

  1. InvoiceID
  2. ProjectID ( foreign key )
  3. InvoiceType ( Paid and Recieved )
  4. InvoiceChannel ( Coding, Designing and Consulting )
  5. InvoiceAmount

The Projects table is in a one-to-many relation with Invoices table with Project ID as foreign key.

I want a table created from querying the Invoices table like this

Grouping problem in MS Access

How can I achieve this using an SQL query?
Or do I have to do it using server side coding ( I use C# with ASP.NET )

0

1 Answer 1

2

Your posted table schema doesn't have a field that contains the amount to sum. I will refer to it as Amount.

First, create a crosstab query, and save it in the database (I've called it ct):

TRANSFORM Sum(Amount) AS SumOfAmount
SELECT ProjectID, InvoiceType
FROM Invoices
GROUP BY ProjectID, InvoiceType
PIVOT InvoiceChannel In ("Coding","Designing","Consulting");

This will give you the ProjectID and the InvoiceType as the first two columns, with one additional column each for "Coding","Designing" and "Consulting".

To get the total, you need another query:

SELECT ct.*, Coding + Designing + Consulting AS Total
FROM ct;

Bear in mind that if there are no records for a particular ProjectID/InvoiceType/InvoiceChannel, the query will return NULL for that combination, which will result in the Total being NULL. If this query will be running under Access (say via Access.Application) you can use the Nz function, which works like the ?? operator in C#:

SELECT ct.*, Nz(Coding,0) + Nz(Designing,0) + Nz(Consulting,0) AS Total
FROM ct;

If you are accessing data from an .mdb or .accdb via ADO.NET using the OleDbProvider, you probably won't be able to use Nz, and you'll have to use something more complex:

SELECT ct.*, 
    Iif(IsNull(Coding), 0, Coding) +
    Iif(IsNull(Designing), 0, Designing) + 
    Iif(IsNull(Consulting), 0, Consulting) AS Total
FROM ct;
Sign up to request clarification or add additional context in comments.

4 Comments

sorry. i have updated question. plus i dont need cross tab query. i am just concerned about querying the single table Invoices. I will try your method and let you know. thanks...
How will you get the data from your Invoices table in the form you want without a crosstab query?
lets just say i have all the project ids from which i want the invoices count. both tables are in different mdbs
Hi ZevSpitz. Nice answer, but you do not need another query to get a total in a crosstab, you can additionally sum the value field as a row heading. For example: TRANSFORM Sum(Table1.ANumber) AS SumOfANumber SELECT Table1.AText2, Sum(Table1.ANumber) AS Total FROM Table1 GROUP BY Table1.AText2 PIVOT Table1.AText;

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.