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.
| Author |
Topic |
|
suchiate
Starting Member
33 Posts |
Posted - 2007-04-01 : 10:52:21
|
| Hi guys,I am working on a query to select the same column twice; one is count and another is count with a condition. How may I do that?Take say the table design is something likeColumn1StatusI wanna do a Select count(column1) and count(column1) where status = 'something'I have a more complex query to follow but I need to clarify if this can be done first. Thanks alot in advance. Hope to hear from you guys soon. |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2007-04-01 : 11:35:15
|
I don't understand purpose of such query, but yes it is possible:Select count(col1) as count1, count(col1) as count2from tablewhere status = 'something Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
|
jezemine
Master Smack Fu Yak Hacker
2886 Posts |
Posted - 2007-04-01 : 11:51:59
|
quote: Originally posted by harsh_athalye I don't understand purpose of such query
because it's more efficient than this:select count(col1) from mytablegoselect count(col1) from mytablego www.elsasoft.org |
 |
|
|
suchiate
Starting Member
33 Posts |
Posted - 2007-04-01 : 12:06:41
|
| NO, you guys got me wrong..I need one single query to select the same column, one with condition and another without.I need to count the total number of columns, as well as the total number of columns with that condition..means the combination ofSelect count(column1) from tblandSelect count(column1) from tbl where condition = something |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-04-01 : 12:15:10
|
| [code]SELECT COUNT(Status) AS Count1, SUM(CASE WHEN Status = 'Something' THEN 1 ELSE 0 END) AS [Count1 with a vengeance]FROM Table1[/code]Peter LarssonHelsingborg, Sweden |
 |
|
|
suchiate
Starting Member
33 Posts |
Posted - 2007-04-01 : 12:30:04
|
quote: Originally posted by Peso
SELECT COUNT(Status) AS Count1, SUM(CASE WHEN Status = 'Something' THEN 1 ELSE 0 END) AS [Count1 with a vengeance]FROM Table1 Peter LarssonHelsingborg, Sweden
Oh my god, Peso, you're great!It's the second time you've helped me. =)Thank you so much! |
 |
|
|
|
|
|
|
|