March 4, 2012 at 3:27 pm
I'm going to feel real dumb when someone posts the answer to this because I know it's real simple. I just can't focus right now.
I have a table called accounts
It has over 800 records.
I need to select the distinct DESCRIPTIONS (easy... and there are 156 distinct records)
Now for the 156 records, I need to SUM the total (column is called balance) of the balance for each distinct record.
So let's say I have a record in the Descriptions column called Peanuts and there are 10 distinct occurrences of peanuts. I need the sum of those 10 occurrences. I can do this no problem, but I want a script that doesn't require me to hard code the description value. :crazy:
March 4, 2012 at 4:07 pm
Is this it?
SELECT Description, SUM(Balance)
FROm Table
GROUP BY Description
March 4, 2012 at 4:20 pm
I knew it so was simple. I was using distinct and that was throwing the whole thing off... I feel real dumb right now... Sorry for wasting your time. 🙁
March 4, 2012 at 5:14 pm
SQL_Enthusiast-AZ (3/4/2012)
I knew it so was simple. I was using distinct and that was throwing the whole thing off... I feel real dumb right now... Sorry for wasting your time. 🙁
I wouldn't call it a waste of time at all. You learned something and you wrote a decent post to help us figure what you were talking about. Heh... you should see some of the simple stuff I get stuck on when I haven't had enough coffee. 😛
--Jeff Moden
Change is inevitable... Change for the better is not.
March 5, 2012 at 1:55 am
Hi
Pls try below code.
declare @test-2 table (id nvarchar(20),name varchar(10),fat int)
insert into @test-2 (id,name,fat) values(1,'aa',10)
insert into @test-2 (id,name,fat) values(1,'aa',10)
insert into @test-2 (id,name,fat) values(1,'aa',10)
insert into @test-2 (id,name,fat) values(2,'bb',20)
select id,name,SUM( distinct fat) fat from @test-2
group by id,name
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply