我有两张桌子的数据。在第一个表格中,我有员工ID和他们的名字。在第二个表格中,我有员工详细信息(例如工资,地点,加入日期,处理的项目,指定)。
我正在尝试编写一个查询,我可以按位置和指定来提供最高和最低工资组的数据。这就是我到目前为止所做的:
SELECT E.EMPID,I.EMPID, E.EMPNAME, I.EMPNAME,
B.[MAXIMUM SALARY],F.[MINIMUM SALARY],
B.DESIG,F.DESIG,B.LOCATION,F.LOCATION
FROM
((SELECT MAX (SALARY) [MAXIMUM SALARY], LOCATION, DESIG
FROM
EMPDETAILS
GROUP BY LOCATION, DESIG) B
JOIN
(SELECT C.EMPID,C.SALARY,C.GENDER,C.DESIG,
C.JOINON,C.LOCATION,C.PROJECTS,D.EMPNAME
FROM EMPDETAILS C
JOIN
EMPLOYEE D
ON C.EMPID=D.EMPID) E
ON E.DESIG=B.DESIG AND E.SALARY=B.[MAXIMUM SALARY] AND E.LOCATION =
B.LOCATION)
FULL OUTER JOIN[enter image description here][1]
((SELECT MIN(SALARY) AS [MINIMUM SALARY], LOCATION, DESIG
FROM EMPDETAILS
GROUP BY LOCATION, DESIG) F
JOIN
(SELECT G.EMPID,H.EMPNAME,G.DESIG,G.GENDER,
G.JOINON,G.LOCATION,G.PROJECTS,G.SALARY
FROM EMPDETAILS G
JOIN EMPLOYEE H
ON G.EMPID=H.EMPID) I
ON I.DESIG=F.DESIG AND F.[MINIMUM SALARY]=I.SALARY AND
I.LOCATION=F.LOCATION)
ON I.DESIG=E.DESIG
ORDER BY B.LOCATION, f.location
给我:
答案 0 :(得分:0)
不知道你真正想要/需要什么,但如果你想要employeeID级别的结果,你需要这样的东西:
SELECT
d.*,
min_salary,
max_salary
FROM
EMPDETAILS d JOIN
EMPLOYEE e ON
d.EMPID=e.EMPID JOIN
(SELECT
empid,
MIN(salary) AS min_salary,
MAX(salary) AS max_salary
FROM
EMPDETAILS
GROUP BY
empid) m ON
e.empid = m.empid
如果您想要按照代码中的LOCATION,DESIG分组的最低/最高工资,您需要将这些结果与员工级别的详细信息分开返回:
SELECT
LOCATION, DESIG,
MIN(salary) AS min_salary,
MAX(salary) AS max_salary
FROM
EMPDETAILS
GROUP BY
LOCATION, DESIG
您的min
和max
分组不需要单独的子查询,您不需要多次加入详细信息表,也不需要full outer join