2017-09-08 45 views
-2

首先,我需要从2016年的前3个月得到体育和音乐部门TotalPrice的总和,其次,我需要从2016年的所有TotalPrice总和中得出结果所有部门,第三 - 我需要得到的第一个结果除以总价的总和来自多年。 所有这一切在相同的查询!我该如何查询一些利润?

谢谢!

该表名为Sales,属性为:S_id,日期,部门,totalPrice。

THIS IS MY CHRY:

Select sum(TotalPrice) as sportMusic, sportMusic/sum(TotalPrice) 
From Sales 
Where (Department="MUSIC" OR Department="SPORT") and 
     DATE BETWEEN "2016/01/01" AND "2016/03/31" 
+3

添加表定义,样本表数据和预期结果(作为格式化文本,而不是图像)。并向我们​​展示您当前的查询尝试。 – jarlh

+0

你对E/R模型有什么想法吗? – Frank

+0

该表名为Sales,属性为:S_id,日期,部门,totalPrice。 THIS IS MY CHRY:SELECT SUM(TotalPrice)作为sportMusic,sportMusic /总和(TotalPrice) 销售 凡(部门= “MUSIC” 或部门= “SPORT”) 和日之间的“2016/01/01“和”2016/03/31“ – user3374271

回答

0

您可以使用您的查询,FROM子句中两个查询,子查询(也称为 “派生表”)在你的。交叉连接三个结果行并在您的select子句中使用总计。沿线的东西:

select 
    ms_2016_q1.total as ms_2016_q1_total, 
    ms_2016_q1.total/all_2016.total as rate_2016, 
    ms_2016_q1.total/all_years.total as rate_all 
from 
(
    select sum(totalprice) as total 
    from sales 
    where department in ('MUSIC', 'SPORT') 
    and date between date '2016-01-01' and date '2016-03-31' 
) ms_2016_q1 
cross join 
(
    select sum(totalprice) as total 
    from sales 
    where date between date '2016-01-01' and date '2016-12-31' 
) all_2016 
cross join 
(
    select sum(totalprice) as total 
    from sales 
) all_years;