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)
 Select same column twice from same table

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 like

Column1
Status

I 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 count2
from table
where status = 'something



Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

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 mytable
go
select count(col1) from mytable
go




www.elsasoft.org
Go to Top of Page

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 of

Select count(column1) from tbl
and
Select count(column1) from tbl where condition = something
Go to Top of Page

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 Larsson
Helsingborg, Sweden
Go to Top of Page

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 Larsson
Helsingborg, Sweden



Oh my god, Peso, you're great!
It's the second time you've helped me. =)
Thank you so much!
Go to Top of Page
   

- Advertisement -