0

我想知道您是否可以提供帮助 - 我已经创建了一个 SQL 查询(SQLServer),尽管没有显示一些结果,我认为这是由于某些连接上的 NULL 结果。例如,我知道一些连接将使用 NULL 结果,因为 100.resp_unit_no 和 100.employee_no 一样可能是 NULL。

到目前为止的查询是:

select 
058.DESCRIPTION "IRU",
111.job_ref "Job Reference",
117.DESCRIPTION "Status",
108.STATUS "Fixed?",
105.DESCRIPTION "Job Type",
111.POSTCODE "Postcode",
111.site_id "Site ID",
108.CONTRACT_START "Contract Earliest",
108.CONTRACT_COMPLETION "Contract Latest",
108.BOOKED_DURATION "Planned Duration (mins)",
111.IMPORTANCE "Importance",
111.CUSTOMER_NAME "Customer Name",
100.expected_start "Expected Start",
100.expected_end "Expected End",
210.NAME "Employee",
111.ON_HOLD "On Hold?"
FROM 
111_JOBS 111,
108_JOB_DETAILS 108,
058_RESPONSIBILITY_UNITS 058,
100_ACTIVITIES 100,
105_ACTIVITY_TYPES 105,
210_EMPLOYEES 210,
117_STATUS_DESCRIPTIONS 117
where 111.JOB_NO=108.JOB_NO
AND 111.JOB_NO=100.JOB_NO
AND 100.RESP_UNIT_NO=058.RESP_UNIT_NO
AND 111.JOB_TYPE=105.ACTIVITY_TYPE
AND 210.EMPLOYEE_NO=100.EMPLOYEE_NO
AND 117.STATUS=100.ACTIVITY_STATUS
order by IRU, Employee, EXPECTED_START;

我想我需要一个正确的外部连接,但我不能完全正确地得到结构......

任何帮助将不胜感激!

4

1 回答 1

0

如果您将查询更改为使用 ANSI-92 显式连接语法而不是您正在使用的 ANSI-89 语法(即,正如名称所暗示的那样,已经过时 20 多年),那么您的查询将变得更容易操作:

SELECT  058.DESCRIPTION "IRU",
        111.job_ref "Job Reference",
        117.DESCRIPTION "Status",
        108.STATUS "Fixed?",
        105.DESCRIPTION "Job Type",
        111.POSTCODE "Postcode",
        111.site_id "Site ID",
        108.CONTRACT_START "Contract Earliest",
        108.CONTRACT_COMPLETION "Contract Latest",
        108.BOOKED_DURATION "Planned Duration (mins)",
        111.IMPORTANCE "Importance",
        111.CUSTOMER_NAME "Customer Name",
        100.expected_start "Expected Start",
        100.expected_end "Expected End",
        210.NAME "Employee",
        111.ON_HOLD "On Hold?"
FROM    111_JOBS 111
        INNER JOIN 108_JOB_DETAILS 108
            ON 111.JOB_NO = 108.JOB_NO
        INNER JOIN 100_ACTIVITIES 100
            ON 111.JOB_NO = 100.JOB_NO
        LEFT JOIN 058_RESPONSIBILITY_UNITS 058          
            100.RESP_UNIT_NO = 058.RESP_UNIT_NO
        LEFT JOIN 105_ACTIVITY_TYPES 105
            ON 111.JOB_TYPE = 105.ACTIVITY_TYPE
        LEFT JOIN 210_EMPLOYEES 210
            ON 210.EMPLOYEE_NO = 100.EMPLOYEE_NO
        LEFT JOIN 117_STATUS_DESCRIPTIONS 117
            ON 117.STATUS = 100.ACTIVITY_STATUS
ORDER BY IRU, Employee, EXPECTED_START;

现在,在不知道哪些列可能为空的情况下,我不知道要从哪个连接更改INNERLEFT,所以我只更改了其中的一些,但您应该可以根据需要再更改一些。

Aaron Bertrand写了一篇很好的文章,有更多理由切换到更新的连接语法。

于 2013-09-30T17:17:19.607 回答