0

我有一个 SQL Server 查询:

(
    SELECT k12_dms_contacts_institution_jobtitles.job_title_id AS id
    FROM   k12_dms_contacts_institution_jobtitles
            INNER JOIN k12_dms_institution_master  ON  k12_dms_institution_master.id = k12_dms_contacts_institution_jobtitles.inst_id
    WHERE   (
                k12_dms_institution_master.state_id='63'
                AND k12_dms_contacts_institution_jobtitles.glevel_id='1'
            )
    GROUP BY
             k12_dms_contacts_institution_jobtitles.job_title_id
) 
INTERSECT 
(
    SELECT k12_dms_contacts_institution_jobtitles.job_title_id AS id
    FROM   k12_dms_contacts_institution_jobtitles
            INNER JOIN k12_dms_institution_master  ON  k12_dms_institution_master.id = k12_dms_contacts_institution_jobtitles.inst_id
    WHERE   (
                k12_dms_institution_master.state_id='63'
                AND k12_dms_contacts_institution_jobtitles.glevel_id='2'
            )
    GROUP BY
             k12_dms_contacts_institution_jobtitles.job_title_id
) 
INTERSECT 
(
    SELECT k12_dms_contacts_institution_jobtitles.job_title_id AS id
    FROM   k12_dms_contacts_institution_jobtitles
            INNER JOIN k12_dms_institution_master  ON  k12_dms_institution_master.id = k12_dms_contacts_institution_jobtitles.inst_id
    WHERE   (
                k12_dms_institution_master.state_id='63'
                AND k12_dms_contacts_institution_jobtitles.glevel_id='3'
            )
    GROUP BY
             k12_dms_contacts_institution_jobtitles.job_title_id
) 
INTERSECT 
(
    SELECT k12_dms_contacts_institution_jobtitles.job_title_id AS id
    FROM   k12_dms_contacts_institution_jobtitles
            INNER JOIN k12_dms_institution_master  ON  k12_dms_institution_master.id = k12_dms_contacts_institution_jobtitles.inst_id
    WHERE   (
                k12_dms_institution_master.state_id='63'
                AND k12_dms_contacts_institution_jobtitles.glevel_id='4'
            )
    GROUP BY
             k12_dms_contacts_institution_jobtitles.job_title_id
) 

有什么办法可以优化上面的查询。交集只需要在Where子句的基础上。我想获取所有年级共有的所有职位。

4

2 回答 2

1

You can make the query more concise as follows:

SELECT IJT.job_title_id AS id
FROM k12_dms_contacts_institution_jobtitles AS IJT
INNER JOIN k12_dms_institution_master AS IM
ON IM.id = IJT.inst_id
WHERE IM.state_id='63'
AND IJT.glevel_id IN ('1', '2', '3', '4')
GROUP BY IJT.job_title_id
HAVING COUNT(DISTINCT IJT.glevel_id) = 4
于 2012-06-27T06:56:03.643 回答
0

这是你想要的。(未经测试)

 SELECT k12_dms_contacts_institution_jobtitles.job_title_id AS id
    FROM   k12_dms_contacts_institution_jobtitles
            INNER JOIN k12_dms_institution_master  ON  k12_dms_institution_master.id = k12_dms_contacts_institution_jobtitles.inst_id
    WHERE   (
                k12_dms_institution_master.state_id='63'
                AND (select count( disctinct k12_dms_contacts_institution_jobtitles.glevel_id) from k12_dms_contacts_institution_jobtitles where k12_dms_contacts_institution_jobtitles.glevel_id in ('1','2','3','4')) = 4
            )
    GROUP BY
             k12_dms_contacts_institution_jobtitles.job_title_id
于 2012-06-27T07:00:11.890 回答