我正在尝试创建一个视图,该视图创建一个表,该表给出了狗的总和和猫的总和。 这就是我目前所拥有的。但是我不确定如何进入视图格式。
select count(PetType) as [Amount of Dogs]
from Pets
where pettype = 'dog'
select count(PetType) as [Amount of Cats]
from Pets
where PetType = 'cat'
谢谢您的帮助。
答案 0 :(得分:0)
CREATE VIEW v AS
SELECT * FROM
(select count(PetType) as [Amount of Dogs] from Pets where pettype = 'dog') d
CROSS JOIN
(select count(PetType) as [Amount of Cats] from Pets where PetType = 'cat') c
答案 1 :(得分:0)
仅使用条件聚合:
select sum(case when PetType = 'dog' then 1 else 0 end) as num_dogs,
sum(case when PetType = 'cat' then 1 else 0 end) as num_cats
from Pets;