Solved

Group by clause not returning expected results

Posted on 2008-10-28
3
202 Views
Last Modified: 2010-04-21
Hi Guys:
I am trying to aggregate results based on employee, product, office & revenue. When I use this statement


SELECT office,  sum(revenue)
FROM [Fiscal 2008] AS f
WHERE f.Date=808
and office = 660
and fa = 224
and [product code] = 1
group by office, revenue

I get these results.office      Expr1001
660      55
660      85.11
660      94.04
660      117.77
660      269.36

 I would expect to see 660 621.28

When I run this:

SELECT  sum(revenue)
FROM [Fiscal 2008] AS f
WHERE f.Date=808
and office = 660
and fa = 224
and [product code] = 1

I do see this
621.28

But as soon as I start adding in group by fields:

SELECT trim(f.empid), trim(f.Office), trim(f.[Product Code]), sum(revenue)
FROM [Fiscal 2008] AS f
WHERE f.Date=808
and office = 660
and fa = 224
and [product code] = 1
GROUP BY  trim(f.empid), trim(f.Office), trim(f.[Product Code]), revenue

I get this.
Expr1000      Expr1001      Expr1002      Expr1003
083MR      660      1      55
083MR      660      1      85.11
083MR      660      1      94.04
083MR      660      1      117.77
083MR      660      1      269.36

I want to see one line for this emp/office/product and see the rolled up revenue {I trimmed the fields thinking maybe the DB had some spaces in those values}.

Any ideas?
0
Comment
Question by:StacyD
  • 2
3 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 125 total points
ID: 22826106
first query: if you want to SUM() by a field, do not GROUP BY  that field:
SELECT office,  sum(revenue)
FROM [Fiscal 2008] AS f
WHERE f.Date=808
and office = 660
and fa = 224
and [product code] = 1
group by office

Open in new window

0
 

Author Comment

by:StacyD
ID: 22830876
I'm such a dope!
SELECT trim(f.empid), trim(f.Office), trim(f.[Product Code]), sum(revenue)
FROM [Fiscal 2008] AS f
WHERE f.Date=808
and office = 660
and fa = 224
and [product code] = 1
GROUP BY  trim(f.empid), trim(f.Office), trim(f.[Product Code])


Thank you - works fine!!!!!
0
 

Author Closing Comment

by:StacyD
ID: 31510936
I must have been asleep at the switch!
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Alter an update query which rounds 7 33
Common Records between Sub Queries 4 25
SQL Syntax 5 34
SQL Server - Find Parent/Child relationship tree between numeric range. 7 41
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

776 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