我有两个表(“ stock”,“ website”)具有相同的结构,都具有一个称为“ product_id”的表 我想在“网站”表和“库存”表中找出product_id的百分比
没有找到简单的方法来实现。希望得到一些建议。 尝试过的INTERSECT运算符
create table stock (product_id, update) as
(
select 123, ‘201904’ from dual
union all select 223, ‘201904’ from dual
union all select 234, ‘201904’ from dual
union all select 124, ‘201904’ from dual
union all select 321, ‘201904’ from dual
union all select 455, ‘201904’ from dual
union all select 412, ‘201904’ from dual
);
create table website (product_id, update) as
(
select 113, ‘201904’ from dual
union all select 223, ‘201904’ from dual
union all select 222, ‘201904’ from dual
union all select 324, ‘201904’ from dual
union all select 321, ‘201904’ from dual
union all select 456, ‘201904’ from dual
union all select 411, ‘201904’ from dual
);
答案 0 :(得分:1)
您可以使用外部联接,然后在每个表中计算产品ID,然后计算出百分比,例如:
WITH stock AS (select 123 product_id, '201904' update_dt from dual UNION ALL
select 223, '201904' from dual UNION ALL
select 234, '201904' from dual UNION ALL
select 124, '201904' from dual UNION ALL
select 321, '201904' from dual UNION ALL
select 455, '201904' from dual UNION ALL
select 412, '201904' from dual),
website AS (select 113 product_id, '201904' update_dt from dual UNION ALL
select 223, '201904' from dual UNION ALL
select 222, '201904' from dual UNION ALL
select 324, '201904' from dual UNION ALL
select 321, '201904' from dual UNION ALL
select 456, '201904' from dual UNION ALL
select 411, '201904' from dual)
SELECT round(100 * COUNT(s.product_id) / COUNT(w.product_id), 2) website_in_stock_percentage
FROM website w
LEFT OUTER JOIN stock s ON w.product_id = s.product_id;
WEBSITE_IN_STOCK_PERCENTAGE
---------------------------
28.57