我是这里的新用户,我认为这太棒了! 我需要一些帮助:
我这样做,但不要像我想的那样工作! (count(countOfCat)不好!)
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')
group by species
我想要像这样收到
Cat Dog Horse Duck mouse ------- ------- ------- ------ ------- 1234 2345 3456 0 0
...
我需要所有的计数都在同一行 我不能利用这个
noGood- Cat Dog Horse Duck mouse noGood- ----- ------ -------- ------- ------- noGood- 1234 0 0 0 0 noGood- 0 2345 0 0 0 noGood- 0 0 3456 0 0
谢谢你的时间!
哒!
答案 0 :(得分:6)
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')
答案 1 :(得分:1)
我也很惊讶你是新来的。 :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
注意:dual
是一个始终只有一行的系统表。这样的技巧很方便。
答案 2 :(得分:0)
很简单。 您需要做的就是删除group by子句: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')