I have two tables Projects and Invoices. The database is MS Access ( mdb )
Project table has fields
- ProjectID
- ProjectName
- ProjectStatus
Invoices table has the fields
- InvoiceID
- ProjectID ( foreign key )
- InvoiceType ( Paid and Recieved )
- InvoiceChannel ( Coding, Designing and Consulting )
- 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

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 )
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):This will give you the
ProjectIDand theInvoiceTypeas the first two columns, with one additional column each for “Coding”,”Designing” and “Consulting”.To get the total, you need another query:
Bear in mind that if there are no records for a particular
ProjectID/InvoiceType/InvoiceChannel, the query will returnNULLfor that combination, which will result in theTotalbeingNULL. If this query will be running under Access (say viaAccess.Application) you can use theNzfunction, which works like the??operator in C#:If you are accessing data from an
.mdbor.accdbvia ADO.NET using the OleDbProvider, you probably won’t be able to useNz, and you’ll have to use something more complex: