如何使用sql按类型获取项的百分比

rjzwgtxy  于 2021-08-13  发布在  Java
关注(0)|答案(1)|浏览(618)

我有两张table

CREATE TABLE item_color (
    item_id INTEGER,
    color VARCHAR(255)
);

CREATE TABLE item_info (
    item_id INTEGER,
    type INTEGER
);

INSERT INTO item_info (item_id, type)
VALUES
  (57510, 7),
  (57509, 7),
  (57508, 8),
  (57507, 8),
  (57506, 7),
  (57505, 7),
  (57504, 8),
  (57503, 8),
  (57501, 8),
  (57500, 8),
  (57499, 7),
  (57498, 7),
  (57497, 8);

INSERT INTO item_color (item_id, color)

VALUES
  (57510,'Red'),  (57509,'Red'),  (57508,'Green'),  (57507,'Blue'),  (57506,'Red'),  (57505,'Red'),
  (57504,'Red'),  (57503,'Green'),  (57501,'Blue'),  (57500,'Red'),  (57499,'Green'),  (57498,'Blue'),
  (57497,'Red'),  (57510,'Red'),  (57509,'Red'),  (57508,'Red'),  (57507,'Red'),  (57506,'Red'),
  (57505,'Red'),  (57504,'Red'),  (57503,'Red'),  (57501,'Blue'),  (57500,'Red'),  (57499,'Red'),
  (57498,'Red'),  (57497,'Red'),  (57510,'Green'),  (57509,'Red'),  (57508,'Red'),  (57507,'Red'),
  (57506,'Blue'),  (57505,'Red'),  (57504,'Green'),  (57503,'Red'),  (57501,'Blue'),  (57500,'Red'),
  (57499,'Red'),  (57498,'Red'),  (57497,'Red'),  (57510,'Green'),  (57509,'Green'),  (57508,'Red'),
  (57507,'Red'),  (57506,'Blue'),  (57505,'Red'),  (57504,'Green'),  (57503,'Green'),  (57501,'Blue'),
  (57500,'Blue'),  (57499,'Blue'),  (57498,'Blue'),  (57497,'Blue'),  (57510,'Red'),  (57509,'Red'),
  (57508,'Red'),  (57507,'Red'),  (57506,'Red'),  (57505,'Red'),  (57504,'Red'),  (57503,'Red'),
  (57501,'Red'),  (57500,'Red'),  (57499,'Red'),  (57498,'Red'),  (57497,'Red');

项目信息

item_id type
57510   7
57509   7
57508   8
57507   8
57506   7
57505   7
57504   8
57503   8
57501   8
57500   8
57499   7
57498   7
57497   8

物品颜色

item_id  color
57510   "Red"
57509   "Red"
57508   "Green"
57507   "Blue"
57506   "Red"
57505   "Red"
57504   "Red"
57503   "Green"
57501   "Blue"
57500   "Red"
57499   "Green"
57498   "Blue"
57497   "Red"
57510   "Red"
57509   "Red"
57508   "Red"
57507   "Red"
57506   "Red"
57505   "Red"
57504   "Red"
57503   "Red"
57501   "Blue"
57500   "Red"
57499   "Red"
57498   "Red"
57497   "Red"
57510   "Green"
57509   "Red"
57508   "Red"
57507   "Red"
57506   "Blue"
57505   "Red"
57504   "Green"
57503   "Red"
57501   "Blue"
57500   "Red"
57499   "Red"
57498   "Red"
57497   "Red"
57510   "Green"
57509   "Green"
57508   "Red"
57507   "Red"
57506   "Blue"
57505   "Red"
57504   "Green"
57503   "Green"
57501   "Blue"
57500   "Blue"
57499   "Blue"
57498   "Blue"
57497   "Blue"
57510   "Red"
57509   "Red"
57508   "Red"
57507   "Red"
57506   "Red"
57505   "Red"
57504   "Red"
57503   "Red"
57501   "Red"
57500   "Red"
57499   "Red"
57498   "Red"
57497   "Red"

我正在尝试执行一个查询,以按项目中绿色或蓝色的类型返回项目的百分比。
期望输出

type  percent_blue_green  total_items
7         30.00               6
8         34.29               7

我编写了一个非常复杂的查询来获得这样的输出

SELECT subquery1.type,
       ROUND( CAST((CAST(subquery3.blue_green_colors AS FLOAT) / CAST(subquery2.total_colors AS FLOAT) * 100) AS numeric), 2) AS percent_blue_green,
       subquery1.total_items
  FROM (SELECT type,
               COUNT(item_id) AS total_items
          FROM item_info
         GROUP BY type) AS subquery1
          JOIN (SELECT type,
                       COUNT(color) AS total_colors
                  FROM item_info AS si1
                       JOIN item_color AS sal1
                         ON sal1.item_id= si1.item_id
                 GROUP BY type) AS subquery2
            ON  subquery2.type= subquery1.type
          JOIN (SELECT type,
                       COUNT(color) AS blue_green_colors
                  FROM item_info AS si2
                       JOIN item_color AS sal2
                         ON sal2.item_id= si2.item_id
                 WHERE color IN ('Blue', 'Green')
                 GROUP BY type) AS subquery3
            ON  subquery3.type= subquery1.type;

我还在学sql。有没有一种更简单有效的方法来编写sql查询以获得所需的输出。

ttygqcqt

ttygqcqt1#

这就是你想要的:

select 
    i.type,
    avg( (c.color in ('Blue', 'Green'))::int ) ratio_blue_green,
    count(distinct i.item_id) no_items
from item_info i
inner join item_color c on c.item_id = i.item_id
group by i.type

第二个值是介于0和1之间的值,表示蓝色和绿色与类型的所有颜色的比率;你可以很容易地把它转换成一个百分比,并根据需要格式化它(对我来说,我发现这样更容易理解信息)。
db小提琴演示:

type |       ratio_blue_green | no_items
---: | ---------------------: | -------:
   7 | 0.30000000000000000000 |        6
   8 | 0.34285714285714285714 |        7

相关问题