我有一个表格inquiry
,其中包含job
,gender
(女性为TRUE
,男性为FALSE
)和salary
。没有列是唯一的,salary
可能包含NULL
。如何找到女性salary
每job
TRUE
的最低要求?如果是有条目[pilot ; TRUE ; 100]
,[pilot ; FALSE ; 100]
,[pilot ; TRUE ; NULL]
和[pilot ; FALSE ; 120]
,下面的代码会返回[pilot ; 100]
两次而不是一次。
SELECT TOP (100) PERCENT T.JOB, T.SALARY
FROM INQUIRY AS T INNER JOIN
(SELECT JOB, MIN(SALARY) AS SL
FROM INQUIRY AS T
WHERE (SALARY IS NOT NULL) AND (GENDER = 1)
GROUP BY JOB) AS x ON x.JOB = T.JOB AND x.SL = T.SALARY
答案 0 :(得分:0)
聚合函数忽略null
。失去联接,你应该没问题:
SELECT job, MIN(salary)
FROM inquiry
WHERE gender = 1
GROUP BY job