加入两个复杂的mysql查询

发布于 2025-02-02 02:12:14 字数 2256 浏览 5 评论 0 原文

我有两个复杂的查询,我需要在之间进行内部连接,并且无法正确地获得格式。

第一个查询查看了列出每个部门经理的表。当表保持的历史更改时,我想仅保留每个部门的经理,并使用最新 from_date。

部门经理表看起来像:

SELECT a.* 
FROM
    (SELECT d1.emp_no , d1.dept_no
     FROM dept_manager d1
     JOIN 
         (SELECT dept_no, MAX(from_date) AS Lastdate 
          FROM dept_manager 
          GROUP BY dept_no) d2 ON d1.from_date = d2.Lastdate  
                               AND d1.dept_no = d2.dept_no) AS a;

表格如下:

emp_no dept_no from_date to_date to_date
110022 D001 1985-01-01 1991-10-01-01
110039 D001 D001 1991-10-01-01-01 9999-01-01-01-01-01-01-01 110085
D002 D002 1984-01-1984-01 1989-19999-12-12 -17
110114 D002 1989-12-17 9999-01-01

等。

第二个查询是针对员工的薪水。由于该表还保留了每个员工的薪水历史,因此我需要使用(Keep)仅对任何员工的最新工资。 The code I did was as follows:

SELECT b.* 
FROM
    (SELECT s1.emp_no , s1.salary
     FROM salaries s1
     JOIN
         (SELECT MAX(from_date) AS Lastdate , emp_no
          FROM salaries 
          GROUP BY emp_no) s2 ON s1.from_date = s2.Lastdate  
                              AND s1.emp_no = s2.emp_no) AS b;

The table looks like:

Emp_no salary from_date to_date
110001 45200 1991-01-01 1992-10-01
110001 47850 1992-01-01 1993-10-01
110001 52000 1993-10-01 1994- 01-01
110022 35000 1985-01-01 1988-10-01
110022 36750 1988-01-01 1991-10-01
110022 38000 1991-10-10-01 1994-01-01

等 在所有经理中,即上面显示的两个复杂查询的内部连接(经理表和工资表)。

正确的语法是什么?

I have two complicated queries which I need to do an inner join between and I can't get the format correct.

The first query looks into the table that lists the manager of each department. As the table keeps a history of every change, I wanted to keep only the manager for each department with the latest from_date.

The department manager table looks like:

SELECT a.* 
FROM
    (SELECT d1.emp_no , d1.dept_no
     FROM dept_manager d1
     JOIN 
         (SELECT dept_no, MAX(from_date) AS Lastdate 
          FROM dept_manager 
          GROUP BY dept_no) d2 ON d1.from_date = d2.Lastdate  
                               AND d1.dept_no = d2.dept_no) AS a;

Table looks like this:

Emp_no dept_no from_date to_date
110022 d001 1985-01-01 1991-10-01
110039 d001 1991-10-01 9999-01-01
110085 d002 1984-01-01 1989-12-17
110114 d002 1989-12-17 9999-01-01

etc..

The second query is for the salaries of employees. As this table also keeps the salary history of each employee, I need to use (keep) only the most recent salary for any employee. The code I did was as follows:

SELECT b.* 
FROM
    (SELECT s1.emp_no , s1.salary
     FROM salaries s1
     JOIN
         (SELECT MAX(from_date) AS Lastdate , emp_no
          FROM salaries 
          GROUP BY emp_no) s2 ON s1.from_date = s2.Lastdate  
                              AND s1.emp_no = s2.emp_no) AS b;

The table looks like:

Emp_no salary from_date to_date
110001 45200 1991-01-01 1992-10-01
110001 47850 1992-01-01 1993-10-01
110001 52000 1993-10-01 1994-01-01
110022 35000 1985-01-01 1988-10-01
110022 36750 1988-01-01 1991-10-01
110022 38000 1991-10-01 1994-01-01

etc..

My objective is to get the average salary of all managers i.e. an inner join of the two complex queries shown above (the manager table and the salary table ).

What is the correct syntax for this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绝情姑娘 2025-02-09 02:12:15

如果我很好地了解您的查询(当前工资的平均工资)。这是一个示例:

这与当前表不起作用,因为最新员工没有薪水。

If I understand your query well (avg salary of current salary). Here is an example : https://www.db-fiddle.com/f/47iAMRgXRAGgg34gFG58JD/0

However this doesn't work with current table because newest employees does not have salary in table.

旧人 2025-02-09 02:12:14
SELECT COUNT(salary),t1.emp_no,dept_no FROM salary t1
JOIN manager t2 ON t1.emp_no=t2.emp_no
GROUP BY t1.emp_no

在这里,您有SIM。由于您的约会有时是9999年,AVG很奇怪。

SELECT COUNT(salary),t1.emp_no,dept_no FROM salary t1
JOIN manager t2 ON t1.emp_no=t2.emp_no
GROUP BY t1.emp_no

Here you have the SIM. As your date some Times are year 9999, avg is strange to do.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文