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.
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 SalesTotal123 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 SalesTotal789 GHI .... .... ... .... ... 650456 DEF .... .... ... .... ... 450123 ABC .... .... ... .... ... 250I 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 = 2014SET @EndYear = 2014SET @StartMonth = 1SET @EndMonth = 3SET @TopN = 10SELECT 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.FAMTSALESWHEN OrdHist.TRANTYPE=2 AND OrdHist.FRETSALES = 0 THEN OrdHist.FAMTSALESELSE OrdHist.FRETSALES * -1END AS [Sale / Return Amount]FROM OESHDT OrdHist INNER JOIN ARCUS Cust ON OrdHist.CUSTOMER = Cust.IDCUSTWHERE OrdHist.YR BETWEEN @StartYear AND @EndYear AND OrdHist.PERIOD BETWEEN @StartMonth AND @EndMonthORDER BY OrdHist.CUSTOMER ASCMS |
|
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.IDCUSTWHERE OrdHist.YR BETWEEN @StartYear AND @EndYear AND OrdHist.PERIOD BETWEEN @StartMonth AND @EndMonthGROUP BY OrdHist.CUSTOMER, Cust.NAMECUST, Cust.TEXTSTRE1, Cust.TEXTSTRE2, Cust.NAMECITY, Cust.CODESTTE, Cust.CODEPSTLORDER BY [Sale / Return Amount] DESC[/code] KH[spoiler]Time is always against us[/spoiler] |
|
|
dominican79
Starting Member
2 Posts |
Posted - 2014-03-23 : 11:07:38
|
Hi Khtan,This works perfect, thank you so very much!!!MS |
|
|
|
|
|
|
|