Percentage calculation in mysql

1

I have a reservation table that has ID, ORIGIN (site, app or null) I want to know the percentage of reservations made by the app and the site,

select count(id) from booking where origin = 'app' / select count(id) from booking where origin is not null * 100; 

How do I make this calculation in MySQL?

    
asked by anonymous 11.07.2018 / 14:22

1 answer

0

You do not need two queries for this, you just add up the amount of records that have the column origin equal to app and divide by the total. Something like:

SELECT
    100 * SUM(CASE WHEN origin = 'app' THEN 1 ELSE 0 END) / COUNT(id) as resultado
FROM booking
WHERE origin IS NOT NULL
    
11.07.2018 / 14:32