简体   繁体   English

sql查询另一列中给出的季度的季度百分比

[英]sql query for quarter percentage in a column as per the quarter given in another column

I have a Sales_Table 我有一个Sales_Table

month  quarter   sales
1        1       100
2        1       200
3        1       300
4        2       400
5        2       500
6        2       600

I would like to have result for Quarterly percentage of sales(Quarterly column) 我希望获得季度销售额百分比的结果(季度列)

month  quarter   sales      Quarterly
1        1       100          17%
2        1       200          33%
3        1       300          50%
4        2       400          27%
5        2       500          33%
6        2       600          40%

I used the formula : 我用公式:
(Basically Quarterly = (sales*100/total of sales in same quarter) (基本季度=(销售额* 100 /同一季度的销售总额)

And this is my query: 这是我的查询:

Select sales, month, quarter, sales * 100.0 / (select Sum(sales) from Sales_Table group by quarter) as quarterly
from Sales_Table
group by month;

This gives me result : 这给了我结果:

sales   month   quarter quarterly
100       1       1      16.666666666666668
200       2       1      33.333333333333336
300       3       1      50
400       4       2      66.66666666666667
500       5       2      83.33333333333333
600       6       2      100

This result shows correct quarterly value for quarter 1 but incorrect for quarter 2 . 此结果显示季度1季度 正确值,但季度2 则不正确

Kindly help me for correct quarter 2 quarterly column values as well. 也请帮助我确定正确的季度2季度列值。

I would do the quarterly calculations in the from clause, using a subquery: 我将使用子查询在from子句中进行季度计算:

Select st.sales, st.month, st.quarter,
       (st.sales * 100.0 / q.sales) as quarterly
from Sales_Table st join
     (select quarter, sum(sales) as sales
      from Sales_Table
      group by quarter
     ) q
     on st.quarter = q.quarter;

If you want to format this as a percentage, then you can use: 如果要将其格式化为百分比,则可以使用:

concat(format(st.sales * 100.0 / q.sales, 2), '%')

http://sqlfiddle.com/#!9/454ba/6 http://sqlfiddle.com/#!9/454ba/6

SELECT s.sales, s.month, s.quarter, (s.sales * 100 / (q.q_sales)) as quarterly
FROM Sales_Table s
LEFT JOIN (
  SELECT quarter, SUM(sales) as q_sales
  FROM Sales_Table 
  GROUP BY quarter) as q
ON q.quarter = s.quarter

Your query with minimum editing: 您的查询经过最少的修改:

 Select 
     sales, 
     month, 
     quarter, 
     sales * 100.0 / (select Sum(sales) from Sales_Table s WHERE s.quarter=m.quarter) as quarterly 
 from Sales_Table m

That's a non-scalar subquery. 那是一个非标量子查询。 I suspect that MySQL is using one of the values at random just like it does when you reference a non-group-by column without an aggregate function. 我怀疑MySQL就像在引用没有聚合功能的非分组列时一样随机使用其中一个值。 That would be why one is right and the other not. 这就是为什么一个人是正确的而另一个人是不正确的。 (And if you check the math you'll see that 600 was the divisor in all of the calculations.) I think you can fix your query by correlating on quarter . (如果您检查一下数学公式,则将发现所有计算中的600是除数。)我认为您可以通过在quarter上进行相关运算来修正查询。

select
    sales, month, quarter,
    sales * 100.0 / (
        select sum(sales) from Sales_Table st2
        where st2.quarter = st.quarter
        group by quarter
    ) as quarterly
from Sales_Table st

Actually you're also doing the whole non-aggregated column things that I mentioned when you group by month and then include sales and quarter in your output. 实际上,您还按我在按月分组时所提到的全部非汇总列中的内容,然后在您的输出中包括salesquarter In this case you don't need the group by anyway since all your rows are in their own group. 在这种情况下,由于所有行都在各自的组中,因此您根本不需要该组。 But MySQL allows you to run that query where other platforms would have given you an error. 但是MySQL允许您在其他平台会给您错误的地方运行该查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM