Solved

Group by clause not returning expected results

Posted on 2008-10-28
3
207 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
[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 143

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

More Than Just A Video Library

Train for your certification. Learn the latest DevOps tools. Grow your skillset to do better work.

At Linux Academy, we release new training modules every week so you'll always be up to date on the latest tech.

Question has a verified solution.

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

Introduction Hopefully the following mnemonic and, ultimately, the acronym it represents is common place to all those reading: Please Excuse My Dear Aunt Sally (PEMDAS). Briefly, though, PEMDAS is used to signify the order of operations (http://en.…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

696 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