Search code examples
mysqlsubquerypercentage

Merging two queries to get a single result in MySQL


I have the following two codes, I need to get the percentage by dividing the 'for_2018_19' by the 'total'. I don't know how to merge them and get a single result which is the percentage.

select sum(maternities) as total from merged
where City = 'Jeza' and Group = 'Unknown';

select sum(Maternities) as for_2018_19 from merged
where Year = '2016/17') percentage ;

Solution

  • You use bth query as the only give back single data, as subqueries

    But yu should keep in mind that group is a reserved word, and you should avoid it

    SELECT
    (select sum(maternities)  from merged
    where City = 'Jeza' and `Group` = 'Unknown')/
    (select sum(Maternities)  from merged
    where Year = '2016/17') as percentage