MySQL获取百分比

时间:2019-04-03 07:40:14

标签: mysql

地区

id  | name
-------------------
1   | NY
2   | Paris
3   | London

餐厅

Restaurant_id   | name      | region_id
-------------------------------------------
1               | KFC       | 1
2               | McDonals  | 1
3               | La food   | 2

restaurant_items

id  | top_image | proffesional_photo | is_lunch | is_dinner | is_beverage | restaurant_id
---------------------------------------------------------------------------------------------
1   |default.png| nulll              | false    | false     | false       | 1
2   |default.png| null               | true     | true      | true        | 1
3   |mypic.png  | pic1.png           | false    | false     | false       | 1
4   |default.png| null               | true     | true      | true        | 2
5   |yoyo.png   | nulll              | false    | false     | false       | 2
6   |default.png| some.png           | true     | true      | true        | 3
7   |default.png| another.png        | false    | false     | false       | 3
8   |default.png| new.png            | false    | false     | false       | 3

通过这三个表,我想生成一个这样的报告

location | number_of_restaurants | total dishes | percentage_of_pro_pics | percentage_of_top_pics | percentage_of_tagged
------------------------------------------------------------------------------------------------------------------------
NY       | 2             | 5        | 20%            | 40%                    | 40%
Paris    | 1             | 3        | 100%           | 0%                     | 33.33%
NY       | 0             | 0        | null           | null                   | null

如果我解释这些列,

  

pro_pics的百分比=计数(profesional_photo不为空)/总数   菜* 100

     

percentage_of_top_pics = count(top_image!='default.png')/总数   菜* 100

     

已标记的百分比=计数(is_lunch = true或is_dinner = true或   is_beverage = true)   *这里三列中的至少一列应该为真。

SELECT regions.name,count(distinct restaurants.Restaurant_id),
count(distinct restaurant_items.id)
 FROM test2.restaurant_items
left join restaurants on restaurant_items.restaurant_id = restaurants.Restaurant_id
left join regions on restaurants.region_id = regions.id
group by regions.name;

但是我不知道如何在它们上实现最后三个。

任何帮助!

非常感谢。

2 个答案:

答案 0 :(得分:0)

使用case when expression

SELECT regions.name,
       count(distinct restaurants.Restaurant_id),
       count(distinct restaurant_items.id),
       (count(case when proffesional_photo IS NOT NULL then 1 end)*100.00)/
ISNULL(NULLIF(count(distinct restaurant_items.id),0), 1) as pro_pics_percentage,
       (count(case when top_image != 'default.png' then 1 end)*100.00)/
ISNULL(NULLIF(count(distinct restaurant_items.id),0), 1) as top_pics_percentage,
       (count(case when is_lunch = true OR is_dinner = true OR is_beverage = true then 1 end)*100.00)/
ISNULL(NULLIF(count(distinct restaurant_items.id),0), 1) as tagged_percentage
FROM test2.restaurant_items
left join restaurants on restaurant_items.restaurant_id = restaurants.Restaurant_id
left join regions on restaurants.region_id = regions.id
group by regions.name

答案 1 :(得分:0)

此查询将为您提供所需的结果。它使用派生表来获取每个餐厅的专业照片,热门照片和带标签的物品的数量,然后JOIN到区域和餐厅。在外部查询中,每个字段的总和(例如热门图片)除以菜式总数即可得出所需的百分比:

SELECT rg.name AS location,
       COUNT(r.restaurant_id) AS num_restaurants,
       SUM(total_dishes),
       SUM(pro_pics) / SUM(total_dishes) * 100 AS percent_pro_pics,
       SUM(top_pics) / SUM(total_dishes) * 100 AS percent_top_pics,
       SUM(tagged) / SUM(total_dishes) * 100 AS percent_tagged
FROM regions rg
LEFT JOIN restaurants r ON r.region_id = rg.id
LEFT JOIN (SELECT restaurant_id,
                  COUNT(id) AS total_dishes AS total_dishes,
                  COUNT(proffesional_photo) AS pro_pics,
                  SUM(top_image != 'default.png') AS top_pics,
                  SUM((is_lunch = 'true') + (is_dinner = 'true') + (is_beverage = 'true') > 0) AS tagged
           FROM restaurant_items
           GROUP BY restaurant_id) ri ON ri.restaurant_id = r.restaurant_id
GROUP BY location
ORDER BY num_restaurants DESC

输出:

location    num_restaurants total_dishes    percent_pro_pics    percent_top_pics    percent_tagged
NY          2               5               20                  40                  40
Paris       1               3               100                 0                   33.3333
London      0               null            null                null                null

Demo on dbfiddle