-1

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?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Amr
  • 23
  • 4
  • 1
    Please Share data structures (describe table) – JoelCrypto May 28 '22 at 19:01
  • Please in code questions give a [mre]--cut & paste & runnable code & example input; desired & actual output (including verbatim error messages); tags & versions; clear specification & explanation. For SQL include DDL & tabular initialization code. For debug that includes the least code you can give that is code that you show is OK extended by code that you show is not OK. [ask] [Help] When you get a result you don't expect, pause your overall goal, chop to the 1st subexpression with unexpected result & say what you expected & why, justified by documentation. (Debugging fundamental.) – philipxy May 28 '22 at 22:06
  • Does this answer your question? [Fetch the row which has the Max value for a column](https://stackoverflow.com/questions/121387/fetch-the-row-which-has-the-max-value-for-a-column) – philipxy May 28 '22 at 22:06
  • Please before considering posting read the manual/reference & google any error message & many clear, concise & precise phrasings of your question/problem/goal, with & without your particular names/strings/numbers, 'site:stackoverflow.com' & tags; read many answers. Reflect research in posts. SO/SE search is poor & literal & sometimes limited to titles, but read the help. Google re googling/searching, including in Q&A at [meta] & [meta.se]. [How much research effort is expected of Stack Overflow users?](https://meta.stackoverflow.com/q/261592/3404097) – philipxy May 28 '22 at 22:07
  • Please always edit by clicking on 'edit' to be editing the most recent version of your post, others may have edited it. Also presumably you were told another edit had been made. Your recent edits undid my edit. I have repeated my edit. [Help] [meta] [meta.se] PS That is not "cut & paste & runnable code & example input" etc. PS See [How do comment replies work?](https://meta.stackexchange.com/q/43019/266284) to learn to use `@x` to notify one non-sole non-poster commenter `x` re a comment. Posters, sole commenters & followers of posts always get notified. – philipxy May 28 '22 at 22:36
  • @philipxy No, unfortunately the link does not help in getting the proper output. But thanks for sharing. – Amr May 29 '22 at 10:48

2 Answers2

0
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.

JoelCrypto
  • 462
  • 2
  • 12
  • Thank you for trying JoelCrypto but unfortunately that is not the output required. – Amr May 28 '22 at 21:59
  • Right now the only solution I can think of (which has seemed to work) is to save the first and second queries as views, and then doing an inner join on the two views. But that takes three separate steps. Am wondering if there is a code that may be more efficient that that. – Amr May 29 '22 at 08:01
0

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.

JoelCrypto
  • 462
  • 2
  • 12