Solved

SQL 2008 GROUP BY

Posted on 2013-11-05
3
285 Views
Last Modified: 2013-11-05
Hello,
I'm trying to use below code to get some totals per client.
results I'm getting is about 10-15 lines per client.
I need to get only one line per client

SELECT fo.AccountNumber,cl.AccountCode,cl.AccountstartDate, cl.Name,
(Select SUM(fo.SubTotalAmount) where fo.ServiceTypeID in (122,123,124)) AS [OVN],
(Select SUM(fo.SubTotalAmount) where fo.ServiceTypeID NOT in (122,123,124)) AS [Local],
SUM(fo.SubTotalAmount) AS [SubTotalAamount],
SUM(fo.TotalAmount) AS [TotalAamount]
FROM  FinalizedOrders fo JOIN Clients cl ON fo.AccountNumber = cl.AccountNumber
WHERE fo.OrderDate BETWEEN '2013-01-01' AND '2013-12-31'
GROUP BY fo.AccountNumber,cl.AccountCode,cl.AccountstartDate, cl.Name,fo.ServiceTypeID
order BY fo.AccountNumber

Any help is appreciated,
Thanks,
0
Comment
Question by:W.E.B
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 5

Accepted Solution

by:
MohitPandit earned 300 total points
ID: 39623903
Hello,

Can you check with below code?

SELECT
	Table1.Name, [OVN] = SUM(Table1.[OVN]), [Local] = SUM(Table1.[Local]),
	[SubTotalAamount] = SUM(Table1.[SubTotalAamount]), [TotalAamount] = SUM(Table1.[TotalAamount])
FROM
(
	SELECT fo.AccountNumber,cl.AccountCode,cl.AccountstartDate, cl.Name,
	(Select SUM(fo.SubTotalAmount) where fo.ServiceTypeID in (122,123,124)) AS [OVN],
	(Select SUM(fo.SubTotalAmount) where fo.ServiceTypeID NOT in (122,123,124)) AS [Local],
	SUM(fo.SubTotalAmount) AS [SubTotalAamount],
	SUM(fo.TotalAmount) AS [TotalAamount]
	FROM  FinalizedOrders fo JOIN Clients cl ON fo.AccountNumber = cl.AccountNumber
	WHERE fo.OrderDate BETWEEN '2013-01-01' AND '2013-12-31'
	GROUP BY fo.AccountNumber,cl.AccountCode,cl.AccountstartDate, cl.Name,fo.ServiceTypeID
) Table1
GROUP BY Table1.Name
ORDER BY Table1.AccountNumber

Open in new window


Best Regards,
Mohit Pandit
0
 

Author Comment

by:W.E.B
ID: 39623924
Awesome.

I will have a second similar question shortly.

thanks again.
0
 

Author Closing Comment

by:W.E.B
ID: 39623927
Thank you
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question