哪里声明用户的平均分钟数大于或等于整体平均分钟数?

时间:2013-10-03 13:38:09

标签: sql sql-server sql-server-2008

我尝试做的是编写一个select语句来显示technician_name列和average_minutes_per_ticket列。但我希望将结果缩小到平均大于或等于每张票的总平均分钟数的技术人员。以下是我到目前为止的情况。

select user_firstname + ' ' + user_lastname as technician_name, 
  avg(ticket_request_date - ticket_closeddate) as average_minutes_per_ticket
  from hd_users
    join hd_tickets on ticket_requestor_id=user_id
  where user_is_technician = ‘Yes’ and
    average_minutes_per_ticket >= ??? 

1 个答案:

答案 0 :(得分:0)

你很亲密!如果要在聚合上使用谓词(where)子句(如avg),则需要使用HAVING子句。在该子句中,您可以将其与子查询中的总体平均值进行比较。

  select user_firstname + ' ' + user_lastname as technician_name, 
    avg(ticket_request_date - ticket_closeddate) as average_minutes_per_ticket
  from hd_users
    join hd_tickets on ticket_requestor_id=user_id
  where user_is_technician = ‘Yes’ 
  having
    avg(ticket_request_date - ticket_closeddate) >= (
         select avg(ticket_request_date - ticket_closeddate) from hd_users)