困在SQL中的查询

时间:2013-11-03 16:50:51

标签: sql sql-server

嘿伙计们,你介意检查我是否正确地这样做了吗?我坚持这个问题:

生成一个查询,以显示所有会计师的会计名称及其平均工作成本(四舍五入到小数点后两位),这些会计师的平均工作成本高于所有工作的平均工作成本。

SELECT 
  a.name + a.surname, 
  SUM(jt.cost * (DATEDIFF(mi, time_complete, time_start))) % SUM(job_id) AS average_job_cost
FROM accountant AS a 
LEFT OUTER JOIN job AS j ON a.staff_id = j.staff_id 
LEFT OUTER JOIN job_type AS jt ON jt.job_type_id = j.job_type_id
WHERE average_job_cost > (SELECT SUM(jt.cost * (DATEDIFF(mi, time_complete, time_start))) % SUM(job_id);

1 个答案:

答案 0 :(得分:0)

像这样的东西(从原版编辑到使用client_id,如在其他问题上找到的)

WITH jobinfo as
(
   SELECT j.client_id, AVG(jt.cost * (DATEDIFF(mi, timecomplete, timestart))) as avgerage
   FROM job as j
   JOIN job_type as jt ON j.jobtype_id = jt.jobtype_id
   GROUP BY j.client_id
), avgAll AS
(
   SELECT AVG(average) as aall
   FROM jobinfo
)
SELECT 
  c.client_id,
  c.name + c.surname, 
  c.phone, 
  c.email_address, 
  j.avgerage
FROM client as c 
JOIN jobinfo j ON c.client_id= j.client_id
WHERE j.avgerage > (SELECT aall FROM avgALL)
ORDER BY j.avgerage DESC