SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 same aggregate but 2 different results
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

masterdineen
Aged Yak Warrior

United Kingdom
548 Posts

Posted - 11/14/2012 :  09:07:40  Show Profile  Reply with Quote
Hello there.

I have a script that will give me three columns and a count(column_ref)

see below.

desc1/ desc2/ desc3/ count(column_ref)
where clause.
group by
order by

that i can acheive easily, but i want to have another count(column_ref) next to the first one using same description columns but with different criterias,

like

desc1/ desc2/ desc3/ count(column_ref)as 'passed', count(column_ref)as 'AllOthers'

does anyone have any idea?

Regards

Rob

khtan
In (Som, Ni, Yak)

Singapore
17642 Posts

Posted - 11/14/2012 :  09:13:27  Show Profile  Reply with Quote
something like this

count( case when somecol =  somecondition then column_ref end ) as passed,
count( case when somecol <> somecondition then column_ref end ) as allothers



KH
Time is always against us

Go to Top of Page

sunitabeck
Flowing Fount of Yak Knowledge

5155 Posts

Posted - 11/14/2012 :  09:16:02  Show Profile  Reply with Quote
One of these
COUNT(CASE WHEN desc1 = 'All others' THEN column_ref END) AS 'All others'
-- or
SUM(CASE WHEN desc1 = 'All others' THEN 1 ELSE 0 END) AS 'All others'
filter the expression within the aggregate using a case expression - I am only guessing what the expression should be. Change it as appropriate.
Go to Top of Page

masterdineen
Aged Yak Warrior

United Kingdom
548 Posts

Posted - 11/14/2012 :  09:16:25  Show Profile  Reply with Quote
o yeah, thank you nice one
will let you know
Go to Top of Page

masterdineen
Aged Yak Warrior

United Kingdom
548 Posts

Posted - 11/14/2012 :  10:17:37  Show Profile  Reply with Quote
excellent, thank you for that.

forgot about case, already have case examples in my script bank.

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000