Solved

SQL 2008 GROUP BY

Posted on 2013-11-05
3
283 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
  • 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
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.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

820 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