具有GROUP,MIN和NULL值的SQL命令的唯一结果

时间:2017-11-17 13:54:04

标签: sql-server group-by null unique min

我有一个表格inquiry,其中包含jobgender(女性为TRUE,男性为FALSE)和salary。没有列是唯一的,salary可能包含NULL。如何找到女性salaryjob 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

1 个答案:

答案 0 :(得分:0)

聚合函数忽略null。失去联接,你应该没问题:

SELECT   job, MIN(salary)
FROM     inquiry
WHERE    gender = 1
GROUP BY job