SQL Group By, or Excel Macro

Hello,
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'))
   
Thanks
Sample.xls
W.E.BAsked:
Who is Participating?
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:
E.g.

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, ''),
	OT.TransactionDate; 

Open in new window

0

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],
	FOI.InvoiceNumber,
	AROT.TransactionDate AS [InvoiceDate],
	SUM(FO.SubTotalAmount) AS [SubTotAmt],
FROM FinalizedOrders FO
INNER JOIN FinalizedOrdersInvoices AS FOI
	ON FOI.OrderNo = FO.OrderNo
INNER JOIN AROpenTransactions AS AROT
	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

0
W.E.BAuthor Commented:
Hello,

Stefan script is showing in one column
Example:
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
Example

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

Thanks
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

ste5anSenior DeveloperCommented:
Just query the data and then pivot it in Excel..
0
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.
0
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.

thanks,
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.