Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用SQL按类型获取项的百分比_Sql_Postgresql_Join_Count_Window Functions - Fatal编程技术网

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

如何使用SQL按类型获取项的百分比,sql,postgresql,join,count,window-functions,Sql,Postgresql,Join,Count,Window Functions,我有两张桌子 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),

我有两张桌子

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查询,以获得所需的输出

这就是您想要的:

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之间的值,表示该类型的所有颜色中蓝色和绿色的比率;您可以轻松地将其转换为百分比,并根据需要设置格式(对于我来说,我发现这样更容易理解信息)

type | ratio_blue_green | no_items ---: | ---------------------: | -------: 7 | 0.30000000000000000000 | 6 8 | 0.34285714285714285714 | 7 类型|比率|蓝色|绿色|无项目 ---: | ---------------------: | -------: 7 | 0.30000000000000000000 | 6 8 | 0.34285714285714285714 | 7
你能发布代码来建立数据库吗?所以一个给定的项目可能有不止一种颜色?您应该提供有关计算规则的更多详细信息。@bhristov我添加了查询以创建表。@GMB每个项目id都是唯一的,并且存在于两个表中,并且给定的项目可以有多种颜色。我只想看看这两种颜色(7,8)中蓝色和绿色占总颜色的百分比是多少。这太棒了。我真的很感激。我试图理解第3行中“::int”的用法。你能给我解释一下那排是干什么的吗?谢谢:-)@Sharath:成功时,此操作将条件评估为
1
,否则
0
。取平均值可以得到满足条件的行的比率。