SQL Group By, or Excel Macro

Can you please help,
I use below script to get the data from SQL. (I need to do some data comparison)
I need to group it By Reference , Invoice Date.
Please see sample attached. (2 tabs, Original Sheet1, Sheet 2 is how I would Like to see it).
Any help is appreciated.

SQL Script or Excel Macro.

SELECT ISNULL(FO.Reference, '') AS [Reference], FO.SubTotalAmount AS [SubTotAmt], FinalizedOrdersInvoices.InvoiceNumber AS [InvoiceNumber],
(Select TransactionDate from AROpenTransactions where AROpenTransactions.TransactionNumber = FinalizedOrdersInvoices.InvoiceNumber) AS [InvoiceDate]
FROM FinalizedOrders FO
INNER JOIN FinalizedOrdersInvoices ON FinalizedOrdersInvoices.OrderNo = FO.OrderNo
Where ((FO.AccountNumber in (9500,9600,9601,9602,9603,10012))) and ((FO.OrderDate >= '2015-01-01')  And (FO.OrderDate <='2015-02-28'))
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:

SELECT	ISNULL(FO.Reference, '') AS [Reference],
	SUM(FO.SubTotalAmount) AS [SubTotAmt],
	OT.TransactionDate  AS [InvoiceDate] 
FROM FinalizedOrders FO
	INNER JOIN FinalizedOrdersInvoices FOI ON FOI.OrderNo = FO.OrderNo 
	LEFT JOIN AROpenTransactions OT ON OT.TransactionNumber = FOI.InvoiceNumber
WHERE	FO.AccountNumber IN (9500,9600,9601,9602,9603,10012) 
	AND FO.OrderDate >= '2015-01-01' 
	AND FO.OrderDate <='2015-02-28'
GROUP BY	ISNULL(FO.Reference, ''),

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Brian CroweDatabase AdministratorCommented:
You're not grouping by Reference Invoice Date in that example, you are grouping by Reference and pivoting the data to aggregate on Invoice Date which is quite a bit different especially since the dates are not predictable so you would need to write some sort of dynamic sql to get the column headers you want.  A group by Reference and Invoice Date would go something like this...

SELECT ISNULL(FO.Reference, '') AS [Reference],
	AROT.TransactionDate AS [InvoiceDate],
	SUM(FO.SubTotalAmount) AS [SubTotAmt],
FROM FinalizedOrders FO
INNER JOIN FinalizedOrdersInvoices AS FOI
	ON FOI.OrderNo = FO.OrderNo
	ON FOI.InvoiceNumber = AROT.TransactionNumber
WHERE FO.AccountNumber in (9500,9600,9601,9602,9603,10012)
	and FO.OrderDate >= '2015-01-01'
	And FO.OrderDate <='2015-02-28'
GROUP BY FO.Reference, FOI.InvoiceNumber, AROT.TransactionDate

Open in new window

W.E.BAuthor Commented:

Stefan script is showing in one column
Reference      SubTotAmt      InvoiceDate
0010      26.70      2015-02-15 00:00:00.000
0010      24.03      2015-02-28 00:00:00.000
0010      31.01      2015-01-15 00:00:00.000
0010      28.60      2015-01-31 00:00:00.000
0014      31.01      2015-01-15 00:00:00.000
0014      28.60      2015-01-31 00:00:00.000
0014      26.70      2015-02-15 00:00:00.000
0014      24.03      2015-02-28 00:00:00.000

Brian script is showing in one column Also

Reference      InvoiceNumber      InvoiceDate      SubTotAmt
0010      548515      2015-01-31 00:00:00.000      28.60
0010      549316      2015-02-15 00:00:00.000      26.70
0010      549831      2015-02-28 00:00:00.000      24.03
0010      548038      2015-01-15 00:00:00.000      31.01
0014      548515      2015-01-31 00:00:00.000      28.60
0014      549316      2015-02-15 00:00:00.000      26.70
0014      548038      2015-01-15 00:00:00.000      31.01
0014      549831      2015-02-28 00:00:00.000      24.03

Acronis True Image 2019 just released!

Create a reliable backup. Make sure you always have dependable copies of your data so you can restore your entire system or individual files.

ste5anSenior DeveloperCommented:
Just query the data and then pivot it in Excel..
Brian CroweDatabase AdministratorCommented:
The reason you're getting it in one column is because your question and your spreadsheet are not aligned as I pointed out in my answer.  You asked for the data to be grouped by reference and invoice date but that is not how your excel sheet is organized.
W.E.BAuthor Commented:
Thank you both,
I guess my question should have been how to pivot what I have in SQL.
Anyway, I got it figured out.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.