子查询以消除重复记录

时间:2012-01-24 21:22:41

标签: mysql

两个表中都有多个具有相同emp id的记录 我需要查询

  1. 加入条件app.employee_id = fac.empid
  2. app.begindate,应用程序,结束日期,app.percent
  3. max(amt)但我仍然有可能重复记录,因为对于一个员工来说,amt可以是相同的,所以我需要不同的max amt记录。
  4. 但我的下面的查询将首先获得最大记录,然后查询begindate date,end date和percent

    我想按照上面指定的顺序查询。

    SELECT app.amt, fac.email, fac.employee_id, fac.empid, 
                         fac.first_name, fac.last_name, fac.department_id, fac.titlecode,
                         app.begindate, app.enddate, app.percent,
                         app.title_name, app.department_name 
    FROM appointment app
    LEFT JOIN appointment app2                                    -- self-join to locate max
     ON app.employee_id = app2.employee_id AND app.amt < app2.amt --  of app.amt
    INNER JOIN faculty fac ON app.employee_id=fac.empid            -- join to fac
    WHERE app2.amt IS NULL                                        -- isolate rows with max(app.amt)
      AND app.begindate <= NOW() 
      AND app.enddate >= NOW() 
      AND app.percent>.50 group by empid
    

0 个答案:

没有答案