查找每个部门(dID)中收入最高的员工(Salary + Comm)

时间:2019-03-16 04:40:45

标签: mysql

下面是我的表,我试图编写一个MySQL查询,该查询选择每个部门(dID)中收入最高的员工(Salary + Comm)

EmployeeID  | Name       | mgr | Job         | Salary| Com | HireDate     | dID
1001 | Ron Smith   | 1005| Writer| 90000 | 20000| 20012-04-12| 1
1002 | Ricky Lake  | 1003| Writer| 55000 | 15000| 2013-01-18| 1

这是我到目前为止所拥有的

SELECT dID, MAX(coalesce(Salary+Comm, Salary, Comm)) AS 'TotalPaid'
FROM Employee 
Group By dID

如何继续显示其他列?希望我已经解释了我想要的东西。

3 个答案:

答案 0 :(得分:1)

这是一种方法:

select e.* from 
Employee e inner join 
(SELECT dID, MAX(coalesce(Salary+Comm, Salary, Comm)) as max_salary  AS 'TotalPaid'
FROM Employee 
Group By dID) d
on d.dID = e .dID 
and coalesce(e.Salary+e.Comm, e.Salary, e.Comm)=d.max_salary

答案 1 :(得分:0)

     const userprompts = document.querySelector("#prompts");
    const userstory = document.querySelector("#story");
    const usererror = document.querySelector("#error");

    const submit = document.querySelector("#submit");
    submit.addEventListener("click", completestory, false);

    const reset= document.querySelector("#reset");
    reset.addEventListener("click", resetPage, false);

    document.querySelector('#name').focus();

    const thename = document.querySelector("#name");
    const firstverb = document.querySelector("#firstverb");
    const firstnoun = document.querySelector("#firstnoun");
    const adjective = document.querySelector("#adjective");
    const secondnoun = document.querySelector("#secondnoun");
    const adverb = document.querySelector("#adverb");
    const place = document.querySelector("#place");
    const storyOutput = document.querySelector("#storyOutput");

    userprompts.classList.add("displayBlock");
    userstory.classList.add("displayNone");
    usererror.classList.add("displayNone");

    function checkStory() {
      if (thename.value == "" && firstverb.value == "" && firstnoun.value == "" && adjective.value == "" && secondnoun.value == "" && adverb.value == "" && place.value == "") {
        error.classname.add("displayBlock");
        return false;
      }
      else {
        return true:
      }
    }

    function completestory() {



      let finishedstory = "";
      finishedstory += "There once was a person named " + thename.value + ". ";
      finishedstory += "One day, " + thename.value + " was " + firstverb.value + " out in the "
      + firstnoun.value + ". ";
      finishedstory += "All of a sudden, " + thename.value + " saw a " + adjective.value +
      " dragon! " ;
      finishedstory += thename.value + " thought for a second and did the only thing that came to mind "
      + " and grabbed a " + secondnoun.value + ". " ;
      finishedstory += "With the " + secondnoun.value + " in hand, " + thename.value + " jumped up and " + adverb.value + " attacked the dragon.";
      finishedstory += " The dragon became very confused and left. Our hero returned to their ancestral home of " + place.value + " ." +  " The End?";

      storyOutput.innerHTML = finishedstory;

      userprompts.classList.add("displayNone");
      userstory.classList.add("displayBlock");
      usererror.classList.add("displayNone");
      userprompts.classList.remove("displayBlock");
      userstory.classList.remove("displayNone");
      usererror.classList.remove("displayBlock");

      if (checkStory == false); {
        return;

      }

    }

    function resetPage() {
      userprompts.classList.add("displayBlock");
      story.classList.add("displayNone");
      error.classList.add("displayNone");
      userprompts.classList.remove("displayNone");
      userstory.classList.remove("displayBlock");
      usererror.classList.remove("displayBlock");
      thename.value = "";
      firstverb.value = "";
      firstnoun.value = "";
      adjective.value = "";
      secondnoun.value = "";
      adverb.value = "";

      storyOutput.innerHTML = "";

      thename.focus();

    }

这将完成工作

答案 2 :(得分:0)

SELECT
    E2.*, E3.TotalPaid
FROM
    Employee E2
INNER JOIN
    (
    SELECT
        MAX(eID) AS eID,
        dID,
        MAX(
            (
            SELECT
                MAX(
                    COALESCE(Salary + Comm, Salary, Comm)
                )
            FROM
                Employee E1
            WHERE
                E.eiD = E1.eID
        )
        ) AS 'TotalPaid'
    FROM
        Employee E
    GROUP BY
        dID
) AS E3
ON
    E2.eID = E3.eID