我有3个查询,但我想将它们合并为一个(有三列)。 this are my results
这是我的代码:
Select family = count(s.destiny) from rental
inner join cars as s on s.number_car = ID_car
where s.destiny in (select destiny from cars where destiny like 'kids')
Select lux = count(s.destiny) from rental
inner join cars as s on s.number_car = ID_car
where s.destiny in (select destiny from cars where destiny like 'luxury')
Select sports = count(s.destiny) from rental
inner join cars as s on s.number_car = ID_car
where s.destiny in (select destiny from cars where destiny like 'sport car')
你能帮我把它组合成'select'吗?
答案 0 :(得分:3)
条件聚合似乎是这种正确的方法:
Select sum(case when density = 'kids' then 1 else 0 end) as family,
sum(case when density = 'lux' then 1 else 0 end) as luxury,
sum(case when density = 'sport car' then 1 else 0 end) as sports
from rental r inner join
cars s
on s.number_car = ID_car ;
答案 1 :(得分:0)
简单的答案是:
SELECT
(
Select family = count(s.destiny) from rental
inner join cars as s on s.number_car = ID_car
where s.destiny in (select destiny from cars where destiny like 'kids')
) as kids,
(
Select lux = count(s.destiny) from rental
inner join cars as s on s.number_car = ID_car
where s.destiny in (select destiny from cars where destiny like 'luxury')
) as luxury,
(
Select sports = count(s.destiny) from rental
inner join cars as s on s.number_car = ID_car
where s.destiny in (select destiny from cars where destiny like 'sport car')
) as sportsCar
话虽如此,我强烈建议您考虑在此处删除子查询。
这样的事情:
SELECT destiny, COUNT(1)
FROM cars
GROUP BY destiny
答案 2 :(得分:0)
为什么你不能尝试这个?
Select destiny,count(*) from rental
inner join cars as s on s.number_car = ID_car
where s.destiny in ( 'cars','luxury')
group by destiny