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)
 Summing Col A when B is distinct

Author  Topic 

SLM09
Starting Member

31 Posts

Posted - 2009-10-29 : 08:43:53
I have been trying to find how to sum one column based on a distinct other column and a boat-load of criteria in the where.

What I have now is a

Select count(distinct(col_x))
From tbl
Where a, b, c, ....

Now I need a $ total from col_y to go along with the distinct count.

What I "need" would be something like

Select sum(col_y), count(distinct(col_x)) but you can't do that.

... or something like

Sum(col_y), count(col_x) ... Where distinct(col_x), a, b, ...

Anyone know how to do something like this?

Thanks

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2009-10-29 : 09:22:20
Can you post some sample data and your expected output.
Go to Top of Page

khalik
Constraint Violating Yak Guru

443 Posts

Posted - 2009-10-29 : 12:20:23
try this..
select cnt,sum from(
Select count(distinct(col_x)) as cnt, 0 as sum
From tbl
Where a, b, c, ....
union
Select 0,sum(col_y), from ....) as t

========================================
Project Manager who loves to code.
===============
Ask to your self before u ask someone
Go to Top of Page
   

- Advertisement -