简体   繁体   中英

MySQL AVG() in sub-query

What one query can produce table_c ?

I have three columns: day, person, and revenue_per_person. Right now I have to use two queries since I lose 'person' when producing table_b .

table_a uses all three columns:

SELECT day, person, revenue_per_person
FROM purchase_table
GROUP BY day, person

table_b uses only two columns due to AVG() and GROUP BY:

SELECT day, AVG(revenue) as avg_revenue
FROM purchase_table
GROUP BY day

table_c created from table_a and table_b :

SELECT 
   CASE 
   WHEN revenue_per_person > avg_revenue THEN 'big spender'
   ELSE 'small spender'
   END as spending_bucket
FROM ????

Maybe this could help, try this one

SELECT a.day,
    CASE 
        WHEN a.revenue_per_person > b.avg_revenue THEN 'big spender'
        ELSE 'small spender'
   END as spending_bucket
FROM
    (
        SELECT day, person, AVG(revenue) revenue_per_person
        FROM purchase_table
        GROUP BY day, person
    ) a INNER JOIN
    (
        SELECT day, AVG(revenue) as avg_revenue
        FROM purchase_table
        GROUP BY day
    ) b ON a.day = b.day

You might want to use analytic functions . An Oracle example showing if a person's salary is greater than average salary in his department.

08:56:54 HR@vm_xe> ed                                                     
Wrote file s:\toolkit\service\buffer.sql                                  

  1  select                                                               
  2    department_id                                                      
  3    ,employee_id                                                       
  4    ,salary                                                            
  5    ,avg_salary                                                        
  6    ,case when salary > avg_salary then 1 else 0 end case_is_greater   
  7  from (                                                               
  8    select                                                             
  9       department_id                                                   
 10      ,employee_id                                                     
 11      ,salary                                                          
 12      ,round(avg(salary) over(partition by department_id),2) avg_salary
 13    from employees                                                     
 14  )                                                                    
 15* where department_id = 30                                             
08:58:56 HR@vm_xe> /                                                      

DEPARTMENT_ID EMPLOYEE_ID     SALARY AVG_SALARY CASE_IS_GREATER           
------------- ----------- ---------- ---------- ---------------           
           30         114      11000       4150               1           
           30         115       3100       4150               0           
           30         116       2900       4150               0           
           30         117       2800       4150               0           
           30         118       2600       4150               0           
           30         119       2500       4150               0           

6 rows selected.                                                          

Elapsed: 00:00:00.01

If you are using a database that supports windows functions, you can do this as:

SELECT (CASE WHEN revenue_per_person > avg_revenue THEN 'big spender'
             ELSE 'small spender'
        END) as spending_bucket
FROM (select pt.*,
             avg(revenue) over (partition by day, person) as revenue_per_person,
             avg(revenue) over (partition by day) as avg_revenue,
             row_number() over (partition by day, person order by day) as seqnum
      from purchase_table pt
     ) t
where seqnum = 1

The purpose of seqnum is to just get one row per person/day combination.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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