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 2005 Forums
 Transact-SQL (2005)
 Problems with GROUP BY

Author  Topic 

vadmcse
Starting Member

10 Posts

Posted - 2009-04-08 : 11:52:54
Hi,

I have a table with this fields and content:

Total_____discount______ZIP
8000________12_________08010
4500________12_________08010
3600________6__________08025
1200________12_________08025


Now i have this query:

SELECT (SUM (Total) + (SUM (Total)* ((Discount)/100))) AS TotAmt, ZIP
FROM table
WHERE ZIP BETWEEN '08000' AND '08999'
GROUP BY Discount, ZIP

The result is:

12500_____08010
3600______08025
1200______08025

I want to get this result:

12500______08010
4800_______08025

If i take the "Discount" off of the GROUP BY clause, i get an error.

Any sugestion?

Thanks

DonAtWork
Master Smack Fu Yak Hacker

2167 Posts

Posted - 2009-04-08 : 12:05:43
[code]
declare @yak table( total int, discount int, zip varchar(8))

insert into @yak (total, discount, zip)
select 8000,12,'08010' union all select
4500,12,'08010' union all select
3600,6,'08025' union all select
1200,12,'08025'

SELECT (SUM(Total) + (SUM ((Total)* ((Discount)/100)))) AS TotAmt, ZIP
FROM @yak
WHERE ZIP BETWEEN '08000' AND '08999'
GROUP BY ZIP

[/code]

[Signature]For fast help, follow this link:
http://weblogs.sqlteam.com/brettk/archive/2005/05/25.aspx
Learn SQL or How to sell Used Cars
For ultra basic questions, follow these links.
http://www.sql-tutorial.net/
http://www.firstsql.com/tutor.htm
http://www.w3schools.com/sql/default.asp
Go to Top of Page

vadmcse
Starting Member

10 Posts

Posted - 2009-04-08 : 12:47:12
YES!!! It worked!

i can't believe i didn't see this.

Thanks
Go to Top of Page
   

- Advertisement -