Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 Aggregate/Group Query with CASE

Author  Topic 

dominican79
Starting Member

2 Posts

Posted - 2014-03-22 : 12:17:56
Hello Everyone,

Thank you for reading my post. I think I'm half way from where I need to be with this query below. My end goal is to be able to list the Top (N) customers based on sales totals. So far I can get a list of all customers with their information for each order, pretty straight forward. This is what my query currently produce (I shortened the field names from the query, just to make it easier):

CustNo CustName, Addr1, Addr2 City State Zip SalesTotal
123 ABC .... .... ... .... ... 100
123 ABC .... .... ... .... ... 150
456 DEF .... .... ... .... ... 200
456 DEF .... .... ... .... ... 250
789 GHI .... .... ... .... ... 300
789 GHI .... .... ... .... ... 350

However, this is what I want to produce: grouping the results by customer and aggregate the sales amounts, sorted by highest to lowest like this:


CustNo CustName, Addr1, Addr2 City State Zip SalesTotal
789 GHI .... .... ... .... ... 650
456 DEF .... .... ... .... ... 450
123 ABC .... .... ... .... ... 250

I havne't been able to figure this out to save my life, but I'm sure it's possible. I look forward to hearing back from you, thank you soooo much.

My query below:


DECLARE @StartYear CHAR(4);
DECLARE @EndYear CHAR(4);
DECLARE @StartMonth SMALLINT;
DECLARE @EndMonth SMALLINT;
DECLARE @TopN SMALLINT;

SET @StartYear = 2014
SET @EndYear = 2014
SET @StartMonth = 1
SET @EndMonth = 3
SET @TopN = 10

SELECT OrdHist.CUSTOMER AS [Customer No.], Cust.NAMECUST AS [Customer Name], Cust.TEXTSTRE1 AS [Address1], Cust.TEXTSTRE2 AS [Address2],
Cust.NAMECITY AS City, Cust.CODESTTE AS [State], Cust.CODEPSTL AS Zip,
CASE
WHEN OrdHist.TRANTYPE=1 THEN OrdHist.FAMTSALES
WHEN OrdHist.TRANTYPE=2 AND OrdHist.FRETSALES = 0 THEN OrdHist.FAMTSALES
ELSE OrdHist.FRETSALES * -1
END AS [Sale / Return Amount]
FROM OESHDT OrdHist INNER JOIN ARCUS Cust ON OrdHist.CUSTOMER = Cust.IDCUST
WHERE OrdHist.YR BETWEEN @StartYear AND @EndYear AND OrdHist.PERIOD BETWEEN @StartMonth AND @EndMonth
ORDER BY OrdHist.CUSTOMER ASC


MS

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2014-03-22 : 21:06:55
[code]SELECT
OrdHist.CUSTOMER AS [Customer No.],
Cust.NAMECUST AS [Customer Name],
Cust.TEXTSTRE1 AS [Address1],
Cust.TEXTSTRE2 AS [Address2],
Cust.NAMECITY AS City,
Cust.CODESTTE AS [State],
Cust.CODEPSTL AS Zip,
SUM(CASE
WHEN OrdHist.TRANTYPE=1 THEN OrdHist.FAMTSALES
WHEN OrdHist.TRANTYPE=2 AND OrdHist.FRETSALES = 0 THEN OrdHist.FAMTSALES
ELSE OrdHist.FRETSALES * -1
END) AS [Sale / Return Amount]
FROM
OESHDT OrdHist
INNER JOIN ARCUS Cust ON OrdHist.CUSTOMER = Cust.IDCUST
WHERE
OrdHist.YR BETWEEN @StartYear AND @EndYear
AND OrdHist.PERIOD BETWEEN @StartMonth AND @EndMonth
GROUP BY
OrdHist.CUSTOMER,
Cust.NAMECUST,
Cust.TEXTSTRE1,
Cust.TEXTSTRE2,
Cust.NAMECITY,
Cust.CODESTTE,
Cust.CODEPSTL
ORDER BY [Sale / Return Amount] DESC
[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

dominican79
Starting Member

2 Posts

Posted - 2014-03-23 : 11:07:38
Hi Khtan,

This works perfect, thank you so very much!!!

MS
Go to Top of Page
   

- Advertisement -