Home > database >  Give a big help. Please help me to solve how to calculate the average
Give a big help. Please help me to solve how to calculate the average

Time:09-18

The following is a set of data
SELECT '2019-01' AS the Month, 'YT' AS BIZ, 'AP' AS Region, 100.90 AS Revenue
INTO T
UNION ALL
SELECT '2019-01' AS the Month, 'ZY' AS BIZ, 'AP' AS Region, 200.90 AS Revenue
UNION ALL
SELECT '2019-01' AS the Month, AS 'AIO BIZ,' AP 'AS Region, 300 AS Revenue
UNION ALL
SELECT '2019-01' AS the Month, 'y' AS BIZ, 'AP' AS Region, 400 AS Revenue
UNION ALL
SELECT '2019-01' AS the Month, AS BIZ 'AA', 'AP' AS Region, 250 AS Revenue
UNION ALL
SELECT '2019-01' AS the Month, AS BIZ 'BB', 'AP' AS Region, 900 AS Revenue
UNION ALL
SELECT '2019-01' AS the Month, 'y' AS BIZ, 'the EMEA AS Region, 400 AS Revenue
UNION ALL
SELECT '2019-01' AS the Month, AS BIZ 'AA', 'the EMEA AS Region, 250 AS Revenue
UNION ALL
SELECT '2019-01' AS the Month, AS BIZ 'BB', 'EAEA AS Region, 900 AS Revenue

UNION ALL
SELECT '2019-02' AS the Month, AS 'AIO BIZ,' AP 'AS Region, 300 AS Revenue
UNION ALL
SELECT '2019-02' AS the Month, 'y' AS BIZ, 'AP' AS Region, 400 AS Revenue
UNION ALL
SELECT '2019-02' AS the Month, AS BIZ 'AA', 'AP' AS Region, 250 AS Revenue
UNION ALL
SELECT '2019-02' AS the Month, AS BIZ 'BB', 'AP' AS Region, 900 AS Revenue

UNION ALL
SELECT '2019-02' AS the Month, AS 'AIO BIZ,' the EMEA AS Region, 200 AS Revenue
UNION ALL
SELECT '2019-02' AS the Month, 'y' AS BIZ, 'the EMEA AS Region, 100 AS Revenue
UNION ALL
SELECT '2019-02' AS the Month, AS BIZ 'AA', 'the EMEA AS Region, 700 AS Revenue
UNION ALL
SELECT '2019-02' AS the Month, AS BIZ 'BB', 'the EMEA AS Region, 300 AS Revenue

UNION ALL
SELECT '2019-03' AS the Month, AS 'AIO BIZ,' AP 'AS Region, 300 AS Revenue
UNION ALL
SELECT '2019-03' AS the Month, 'y' AS BIZ, 'AP' AS Region, 400 AS Revenue
UNION ALL
SELECT '2019-03' AS the Month, AS BIZ 'AA', 'AP' AS Region, 250 AS Revenue
UNION ALL
SELECT '2019-03' AS the Month, AS BIZ 'BB', 'AP' AS Region, 900 AS Revenue

Built after table,
The first question I want to know all the area and average total cost of each month, month end table need, value, average value, the average here is every month is the same number of
On the second question, area, average gross, (the same area across the month on average)
The above form is the result of the first topic is looking forward to, the following table is the result of the second question is looking forward to
Thank you

CodePudding user response:

The first problem has solution out

Select the month, the sum (revenue) as revenue,
(select sum (revenue)/3 from T) as avg_revune from T
Group by the month

Pure is a small white self-study, if the problem is too childish, also please burke, hope to get your help,
The second question is still in brewing,

CodePudding user response:

Build table data may be slightly different with the last data in a form, but the results are similar,

The second question, I think whether should use the sum (revenue)/month average value of the count (*) to solve each region?
But the solution may be wrong, I should count group by region since the end of the number, not the number of the original region in the table,

CodePudding user response:

What a great god can help me to solve it?
  • Related