MS Access SQL Query Data Side By Side instead of Stacked

mrmmickle1

Well-known Member
Joined
May 11, 2012
Messages
2,461
My Current Query Produces Data like this:



Access 2013
ABC
1Invoice NumberInvoice TotalGL_Account
2CINV4413$2,200.00Supply: 20-05-509-5480
3CINV4514$1,750.00Supply: 20-05-509-5480
4CINV4574$2,400.00Supply: 20-05-509-5480
5CINV4773$1,500.00Supply: 20-05-509-5480
6CINV4844$2,800.00Supply: 20-05-509-5480
7CINV4947$3,900.00Supply: 20-05-509-5480
8CINV4413$3,000.00Freight: 20-05-509-5600
9CINV4514$49,000.00Freight: 20-05-509-5600
10CINV4574$36,700.00Freight: 20-05-509-5600
11CINV4773$38,100.00Freight: 20-05-509-5600
12CINV4844$37,850.00Freight: 20-05-509-5600
13CINV4947$39,700.00Freight: 20-05-509-5600
Sheet1


Current Query:

Code:
'Both GL Accounts Together in Same Query
SELECT tblInvoiceReport.[Invoice Number], Sum(tblInvoiceReport.[Total Amount]) AS [Invoice Total], "Supply: 20-05-509-5480" AS GL_Account
FROM tblInvoiceReport
WHERE (((tblInvoiceReport.Item)<>"Processing Fee" And (tblInvoiceReport.Item) Not Like "Carrier*"))
GROUP BY tblInvoiceReport.[Invoice Number], "Supply";
UNION ALL
SELECT tblInvoiceReport.[Invoice Number],  SUM(tblInvoiceReport.[Total Amount]) AS [Invoice Total], "Freight: 20-05-509-5600" AS GL_Account
FROM tblInvoiceReport
WHERE tblInvoiceReport.Item LIKE "Carrier*" Or   tblInvoiceReport.Item = "Processing Fee"
GROUP BY tblInvoiceReport.[Invoice Number];

I would like for the query to produce values that look like this:


Access 2013
ABC
1Invoice NumberSupply: 20-05-509-5480Freight: 20-05-509-5600
2CINV4413$2,200.00$3,000.00
3CINV4514$1,750.00$49,000.00
4CINV4574$2,400.00$36,700.00
5CINV4773$1,500.00$38,100.00
6CINV4844$2,800.00$37,850.00
7CINV4947$3,900.00$39,700.00
Sheet1



Is there an easy way to change my query to do this?
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Let's say that your query above in named "Query1". Create a new query based off of that one with this code:
Code:
SELECT 
    Query1.[Invoice Number], 
    Sum(IIf([GL_Account]="Supply: 20-05-509-5480",[Invoice Total],0)) AS [Supply: 20-05-509-5480], 
    Sum(IIf([GL_Account]="Freight: 20-05-509-5600",[Invoice Total],0)) AS [Freight: 20-05-509-5600]
FROM Query1
GROUP BY Query1.[Invoice Number];
 
Upvote 0
You can also do this more dynamically with a Cross-Tab Query. That code would look like this:
Code:
TRANSFORM Sum(Query1.[Invoice Total]) AS [SumOfInvoice Total]
SELECT Query1.[Invoice Number]
FROM Query1
GROUP BY Query1.[Invoice Number]
ORDER BY Query1.[GL_Account] DESC 
PIVOT Query1.[GL_Account];
 
Upvote 0
Joe4,

Thanks, I ended up merging the two together! Works like a charm!! Really appreciate the help :)

Code:
SELECT Query1.[Invoice Number], Sum(IIf([GL_Account]="Supply: 20-05-509-5480",[Invoice Total],0)) AS [Supply: 20-05-509-5480], Sum(IIf([GL_Account]="Freight: 20-05-509-5600",[Invoice Total],0)) AS [Freight: 20-05-509-5600]
FROM (SELECT tblInvoiceReport.[Invoice Number], Sum(tblInvoiceReport.[Total Amount]) AS [Invoice Total], "Supply: 20-05-509-5480" AS GL_Account
FROM tblInvoiceReport
WHERE (((tblInvoiceReport.Item)<>"Processing Fee" And (tblInvoiceReport.Item) Not Like "Carrier*"))
GROUP BY tblInvoiceReport.[Invoice Number], "Supply"
UNION ALL
SELECT tblInvoiceReport.[Invoice Number],  SUM(tblInvoiceReport.[Total Amount]) AS [Invoice Total], "Freight: 20-05-509-5600" AS GL_Account
FROM tblInvoiceReport
WHERE tblInvoiceReport.Item LIKE "Carrier*" Or   tblInvoiceReport.Item = "Processing Fee"
GROUP BY tblInvoiceReport.[Invoice Number])  AS Query1
GROUP BY Query1.[Invoice Number];
 
Upvote 0
You're welcome!

Note my last post above, which shows another more dynamic way of doing it (so you aren't hard-coding those column headers in case they may ever be other values).
 
Upvote 0
I'm fiddling around with the CrossTab Query Now
If you use the Crosstab Query Wizard, it will walk you through it (it is pretty straightforward). Just be sure to choose the SUM function for the mathematical function to apply to the Invoice Total field. I t will add an extra Total column in there, but if you then switch your query to Design View, you can easily remove that.
 
Upvote 0
Thanks for the two methods. I'm fiddling around with the CrossTab Query Now.
Replace Sheet1 with your table name
Code:
TRANSFORM Min(Sheet1.[Invoice Total]) AS [MinOfInvoice Total]
SELECT Sheet1.[Invoice Number]
FROM Sheet1
GROUP BY Sheet1.[Invoice Number]
PIVOT Sheet1.GL_Account;
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,516
Messages
6,125,286
Members
449,218
Latest member
Excel Master

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top