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
 General SQL Server Forums
 New to SQL Server Programming
 Unique

Author  Topic 

jfm
Posting Yak Master

145 Posts

Posted - 2013-05-09 : 06:21:57
Hi there,

I need to select the distinct ID's that I have in Col_Id and SUM their balance from col_Balance and table1

Any tip?

Thanks

ahmeds08
Aged Yak Warrior

737 Posts

Posted - 2013-05-09 : 06:26:17
is this??

select id,sum(col_balance) from table
group by id

mohammad.javeed.ahmed@gmail.com
Go to Top of Page

jfm
Posting Yak Master

145 Posts

Posted - 2013-05-09 : 06:30:11
Nope.

As I have duplicates I will need to use unique or distinct in the query..

I need not to take into account the exiting duplicates because the balance in the extraction is wrong due to this ID's duplicated

Any idea?

quote:
Originally posted by ahmeds08

is this??

select id,sum(col_balance) from table
group by id

mohammad.javeed.ahmed@gmail.com

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-05-09 : 07:01:27
do you mean this?


select id,
sum(case when rn=1 then col_balance else 0 end)
from
(
select row_number() over (partition by id order by id) as rn,*
from table
)t
group by id


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2013-05-09 : 10:51:53
http://www.sqlservercentral.com/articles/Best+Practices/61537/
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx
Go to Top of Page
   

- Advertisement -