我需要计算总销售额估算的总增值税(占总销售额的15%)。我正在努力解决这个问题,我首先尝试了一个选择总和然后没有工作 - >
SELECT SUM(price) AS total_sales,
SUM(price) * 0.15 AS fifteen_percent
FROM sales;
这里a是产品表的图片
这是销售表的图片
任何帮助都将不胜感激!谢谢。
答案 0 :(得分:1)
SELECT SUM(price*quantity) AS total_sales,
SUM(price*quantity) * 0.15 AS fifteen_percent
FROM sales
JOIN products ON sales.prod_id = products.prod_id;
答案 1 :(得分:0)
Wawrzyniec是正确的,但您也可以使用Windowed函数进行聚合。
SELECT DISTINCT prod_name,
SUM(price) OVER(PARTITION BY prod_name) * quantity as total_sales,
(SUM(price) OVER(PARTITION BY prod_name) * quantity) * 0.15 as fifteen_percent
FROM sales
INNER JOIN products ON sales.prod_id = products.prod_id;