Oracle - count() in same line
i'm new user here, and i think that this is amazing!!! I need a little help:
i do this, but don't work like i want! (count(countOfCat) is no Good!)
select count(countOfCat) as "Cat", count(countOfDog) as "dog", count(countOfHorse) as "horse", 0 as "duck", 0 as "Mouse"
from an开发者_JAVA技巧imal
where Birthdate in
(...
-- i think not important
...
)
and ( species= 'cat' or species= 'dog' or species= 'horse')
group by species
i'm want to receive like this
Cat Dog Horse Duck mouse ------- ------- ------- ------ ------- 1234 2345 3456 0 0
...
I need that all count are in the same row.
I can't utilize thisnoGood- Cat Dog Horse Duck mouse noGood- ----- ------ -------- ------- ------- noGood- 1234 0 0 0 0 noGood- 0 2345 0 0 0 noGood- 0 0 3456 0 0
Thank you for yout time!
Da!select sum(case when species = 'cat' then 1 else 0 end) as "Cat",
sum(case when species = 'dog' then 1 else 0 end) as "Dog",
sum(case when species = 'horse' then 1 else 0 end) as "Horse",
0 as "duck",
0 as "Mouse"
from animal
where species in ('cat', 'dog', 'horse')
I too think amazing that you're new here. :D
select
(select count(*) from animal a where a.species = 'cat') as Cat,
(select count(*) from animal a where a.species = 'horse') as Horse,
(select count(*) from animal a where a.species = 'duck') as Duck
from
dual
NB: dual
is a system table that always has a single row. It is very convenient for tricks like this.
Very simple. All you need to do is remove the group by clause :D
select count(countOfCat) as "Cat", count(countOfDog) as "dog", count(countOfHorse) as "horse", 0 as "duck", 0 as "Mouse"
from animal
where Birthdate in
(...
-- i think not important
...
)
and ( species= 'cat' or species= 'dog' or species= 'horse')
精彩评论