Percentage calculation in mysql

Asked

Viewed 692 times

1

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

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?

1 answer

0


You don’t need two queries for this, just you 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
  • Gave syntax error: You have an error in your SQL syntax; check the manual that Corresponds to your Mysql server version for the right syntax to use near ') / COUNT(id) as resultado FROM booking WHERE origin IS NOT NULL' at line 2

  • @Daywisonferreiraleal and you researched what may be the mistake?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.