元招聘 SQL -- 加入第 1 部分
Meta Recruiting SQL -- Join Part 1
我已尽力将下面的元招聘问题排版得井井有条。我正在尝试确定该比率的当前查询是这样的。返回正确的 2/3 值,但比率返回全 1。对我出错的地方有帮助吗?
更新:
在 David Aldridge
的帮助下
我意识到 SqlLite 是这里的问题,转换为数字有助于通过下面的解决方案中的查询获得最终结果。
背景:
以下模式是杂货店关系数据库的子集
链。该连锁店向其销售许多不同产品 类 的产品
不同商店的顾客。它还进行许多不同的
促销活动。
我们要分析的四个表之间的关系如下图所示:
提示:
- CMO 有兴趣了解销售量有何不同
- 产品系列受到促销活动的影响。
- 为此,对于每个可用的产品系列,
- 显示已售出的总单位数,
- 以及有效促销的已售单位比例
- 对于没有促销的售出单位,
-- 按总销售量递增顺序排列。
解法:
SELECT product_family,
SUM(units_sold) as total_units_sold,
CAST(SUM(CASE WHEN s.promotion_id IS NOT 0 then units_sold * 1.0 ELSE 0 END) AS NUMERIC) /
CAST(SUM(CASE WHEN s.promotion_id = 0 THEN units_sold * 1.0 ELSE 0 END) as NUMERIC) as ratio_units_sold_with_promo_to_sold_without_promo
FROM products p
JOIN product_classes c ON c.product_class_id = p.product_class_id
JOIN sales s ON s.product_id = p.product_id
GROUP BY product_family
ORDER BY total_units_sold ASC;
这感觉就像您要找的东西(为了清楚起见,我添加了一个额外的列):
SELECT product_family,
SUM(units_sold) as total_units_sold,
SUM(CASE WHEN s.promotion_id > 0 then units_sold ELSE 0 END) as sold_when_promoted,
SUM(CASE WHEN s.promotion_id > 0 then units_sold ELSE 0 END)::numeric /
SUM(units_sold) AS ratio_units_sold_with_promo_to_sold_without_promo
FROM products p
JOIN product_classes c ON c.product_class_id = p.product_class_id
JOIN sales s ON s.product_id = p.product_id
GROUP BY product_family;
...或使用过滤子句...
SELECT product_family,
SUM(units_sold) as total_units_sold,
SUM(units_sold) filter(where s.promotion_id > 0) as sold_when_promoted,
SUM(units_sold) filter(where s.promotion_id > 0)::numeric /
SUM(units_sold) AS ratio_units_sold_with_promo_to_sold_without_promo
FROM products p
JOIN product_classes c ON c.product_class_id = p.product_class_id
JOIN sales s ON s.product_id = p.product_id
GROUP BY product_family;
我认为您不需要防止被零除,但如果您的某个产品的销售额总和为 zer,则您需要处理它。
编辑:我认为您实际上不需要参考促销 table。
SELECT product_family,
SUM(units_sold) as total_units_sold,
CAST(SUM(CASE WHEN s.promotion_id IS NOT 0 then units_sold * 1.0 ELSE 0 END) AS NUMERIC) /
CAST(SUM(CASE WHEN s.promotion_id = 0 THEN units_sold * 1.0 ELSE 0 END) as NUMERIC) as ratio_units_sold_with_promo_to_sold_without_promo
FROM products p
JOIN product_classes c ON c.product_class_id = p.product_class_id
JOIN sales s ON s.product_id = p.product_id
GROUP BY product_family
ORDER BY total_units_sold ASC;
select avg(case when is_low_fat_flg=1 and is_recyclable_flg=1 then 1 else 0 end)*100 as pct_low_fat_and_recyclable
from products
我已尽力将下面的元招聘问题排版得井井有条。我正在尝试确定该比率的当前查询是这样的。返回正确的 2/3 值,但比率返回全 1。对我出错的地方有帮助吗?
更新: 在 David Aldridge
的帮助下我意识到 SqlLite 是这里的问题,转换为数字有助于通过下面的解决方案中的查询获得最终结果。
背景:
以下模式是杂货店关系数据库的子集 链。该连锁店向其销售许多不同产品 类 的产品 不同商店的顾客。它还进行许多不同的 促销活动。
我们要分析的四个表之间的关系如下图所示:
提示:
- CMO 有兴趣了解销售量有何不同
- 产品系列受到促销活动的影响。
- 为此,对于每个可用的产品系列,
- 显示已售出的总单位数,
- 以及有效促销的已售单位比例
- 对于没有促销的售出单位, -- 按总销售量递增顺序排列。
解法:
SELECT product_family,
SUM(units_sold) as total_units_sold,
CAST(SUM(CASE WHEN s.promotion_id IS NOT 0 then units_sold * 1.0 ELSE 0 END) AS NUMERIC) /
CAST(SUM(CASE WHEN s.promotion_id = 0 THEN units_sold * 1.0 ELSE 0 END) as NUMERIC) as ratio_units_sold_with_promo_to_sold_without_promo
FROM products p
JOIN product_classes c ON c.product_class_id = p.product_class_id
JOIN sales s ON s.product_id = p.product_id
GROUP BY product_family
ORDER BY total_units_sold ASC;
这感觉就像您要找的东西(为了清楚起见,我添加了一个额外的列):
SELECT product_family,
SUM(units_sold) as total_units_sold,
SUM(CASE WHEN s.promotion_id > 0 then units_sold ELSE 0 END) as sold_when_promoted,
SUM(CASE WHEN s.promotion_id > 0 then units_sold ELSE 0 END)::numeric /
SUM(units_sold) AS ratio_units_sold_with_promo_to_sold_without_promo
FROM products p
JOIN product_classes c ON c.product_class_id = p.product_class_id
JOIN sales s ON s.product_id = p.product_id
GROUP BY product_family;
...或使用过滤子句...
SELECT product_family,
SUM(units_sold) as total_units_sold,
SUM(units_sold) filter(where s.promotion_id > 0) as sold_when_promoted,
SUM(units_sold) filter(where s.promotion_id > 0)::numeric /
SUM(units_sold) AS ratio_units_sold_with_promo_to_sold_without_promo
FROM products p
JOIN product_classes c ON c.product_class_id = p.product_class_id
JOIN sales s ON s.product_id = p.product_id
GROUP BY product_family;
我认为您不需要防止被零除,但如果您的某个产品的销售额总和为 zer,则您需要处理它。
编辑:我认为您实际上不需要参考促销 table。
SELECT product_family,
SUM(units_sold) as total_units_sold,
CAST(SUM(CASE WHEN s.promotion_id IS NOT 0 then units_sold * 1.0 ELSE 0 END) AS NUMERIC) /
CAST(SUM(CASE WHEN s.promotion_id = 0 THEN units_sold * 1.0 ELSE 0 END) as NUMERIC) as ratio_units_sold_with_promo_to_sold_without_promo
FROM products p
JOIN product_classes c ON c.product_class_id = p.product_class_id
JOIN sales s ON s.product_id = p.product_id
GROUP BY product_family
ORDER BY total_units_sold ASC;
select avg(case when is_low_fat_flg=1 and is_recyclable_flg=1 then 1 else 0 end)*100 as pct_low_fat_and_recyclable
from products