1

哪一个查询可以产生table_c

我有三列:day、person 和income_per_person。现在我必须使用两个查询,因为我在生成table_b时丢失了“人” 。

table_a使用所有三列:

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

由于 AVG() 和 GROUP BY,table_b仅使用两列:

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

从table_atable_b创建的table_c

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

3 回答 3

1

也许这会有所帮助,试试这个

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
于 2012-09-11T00:53:33.403 回答
0

您可能想要使用分析函数。一个 Oracle 示例,显示一个人的薪水是否高于其部门的平均薪水。

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
于 2012-09-11T01:00:02.353 回答
0

如果您使用的是支持 windows 功能的数据库,您可以这样做:

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.

于 2012-09-11T01:02:51.793 回答