计算销售总桶数

时间:2014-10-22 08:40:47

标签: sql

我需要计算总销售额估算的总增值税(占总销售额的15%)。我正在努力解决这个问题,我首先尝试了一个选择总和然后没有工作 - >

SELECT SUM(price) AS total_sales,
       SUM(price) * 0.15 AS fifteen_percent
FROM   sales;

这里a是产品表的图片 products

这是销售表的图片 sales table

任何帮助都将不胜感激!谢谢。

2 个答案:

答案 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;