SQLite Query没有得到实际计数

时间:2018-02-14 02:06:38

标签: android sqlite

我在计算特定地点的乘客和行李总数时遇到问题。

这是我的表enter image description here

但我的query显示总乘客总是2,

enter image description here

最后,这是query

select
    ticket_placeto,
    (select count(*) as count from tickets where passenger_type <> 'Baggage' group by ticket_placeto)as Pass ,
    ifnull((select count(*) as count from tickets where passenger_type = 'Baggage' group by ticket_placeto),0) as Baggage
from tickets

1 个答案:

答案 0 :(得分:1)

将您的查询改为使用条件聚合:

SELECT
    ticket_placeto,
    COUNT(CASE WHEN passenger_type <> 'Baggage' THEN 1 END) AS Pass,
    COUNT(CASE WHEN passenger_type =  'Baggage' THEN 1 END) AS Baggage
FROM tickets
GROUP BY ticket_placeto;

这里的想法是,对于每个ticket_placeto组,您希望根据乘客类型采取两个条件计数。