0

我有两个疑问。此查询给出正确的结果:

SELECT SFRSTCR_PIDM
FROM SATURN.SFRSTCR 
INNER JOIN SATURN.SSBSECT
     ON SFRSTCR_TERM_CODE = SSBSECT_TERM_CODE
    AND SFRSTCR_CRN = SSBSECT_CRN
    AND (SFRSTCR_RSTS_CODE LIKE 'R%'
     OR  SFRSTCR_RSTS_CODE LIKE 'W%') 
    AND SFRSTCR_CREDIT_HR >= 1 
    AND SSBSECT_SUBJ_CODE IN ('ASTR', 'BIOL', 'CHEM', 'ENVS', 'GEOL', 'PHYS')
    AND SSBSECT_CRSE_NUMB NOT LIKE '%L'
    AND SFRSTCR_TERM_CODE BETWEEN '&BeginAY' AND '&EndAY' 
INNER JOIN SATURN.SHRDGMR
     ON SFRSTCR_PIDM = SHRDGMR_PIDM
    AND SHRDGMR_GRST_CODE = 'AW'
    AND SHRDGMR_TERM_CODE_GRAD BETWEEN '&BeginAY' AND '&EndAY' 
    AND SHRDGMR_MAJR_CODE_1 = '&MajorCode'
GROUP BY SFRSTCR_PIDM
HAVING COUNT(SFRSTCR_CRN) = 2
ORDER BY SFRSTCR_PIDM

它返回 8 条符合条件的记录。

此查询给出不正确的结果:

SELECT SFRSTCR_PIDM,
       SSBSECT_SUBJ_CODE,
       SSBSECT_CRSE_NUMB
FROM SATURN.SFRSTCR 
INNER JOIN SATURN.SSBSECT
     ON SFRSTCR_TERM_CODE = SSBSECT_TERM_CODE
    AND SFRSTCR_CRN = SSBSECT_CRN
    AND (SFRSTCR_RSTS_CODE LIKE 'R%'
     OR  SFRSTCR_RSTS_CODE LIKE 'W%') 
    AND SFRSTCR_CREDIT_HR >= 1 
    AND SSBSECT_SUBJ_CODE IN ('ASTR', 'BIOL', 'CHEM', 'ENVS', 'GEOL', 'PHYS')
    AND SSBSECT_CRSE_NUMB NOT LIKE '%L'
    AND SFRSTCR_TERM_CODE BETWEEN '&BeginAY' AND '&EndAY' 
INNER JOIN SATURN.SHRDGMR
     ON SFRSTCR_PIDM = SHRDGMR_PIDM
    AND SHRDGMR_GRST_CODE = 'AW'
    AND SHRDGMR_TERM_CODE_GRAD BETWEEN '&BeginAY' AND '&EndAY' 
    AND SHRDGMR_MAJR_CODE_1 = '&MajorCode'
GROUP BY SFRSTCR_PIDM, 
         SSBSECT_SUBJ_CODE,
         SSBSECT_CRSE_NUMB
HAVING COUNT(SFRSTCR_CRN) = 2
ORDER BY SFRSTCR_PIDM

我想要它做的是返回 16 条记录:第一个查询中的 8 个 SFRSTCR_PIDM 应该每个重复两次,并且每条记录应该有一个 SSBSECT_SUBJ_CODE 和一个 SSBSECT_CRSE_NUMB。它实际上做的是给我 8 条完全不同的 SFRSTCR_PIDM 记录。

我不太确定问题出在哪里,但我认为有某种方法可以操纵 GROUP BY 子句以使其正常工作。我会很感激一些帮助。

编辑:这可行,但必须有一个更简单的解决方案:

SELECT iv1.SFRSTCR_PIDM, 
       sect2.SSBSECT_SUBJ_CODE, 
       sect2.SSBSECT_CRSE_NUMB
FROM SATURN.SFRSTCR stcr2
   INNER JOIN SATURN.SSBSECT sect2
      ON stcr2.SFRSTCR_TERM_CODE = sect2.SSBSECT_TERM_CODE
     AND stcr2.SFRSTCR_CRN = sect2.SSBSECT_CRN
     AND sect2.SSBSECT_SUBJ_CODE IN ('ASTR', 'BIOL', 'CHEM', 'ENVS', 'GEOL', 'PHYS')
     AND sect2.SSBSECT_CRSE_NUMB NOT LIKE '%L'
     AND stcr2.SFRSTCR_TERM_CODE BETWEEN '&BeginAY' AND '&EndAY' 
   INNER JOIN (SELECT stcr1.SFRSTCR_PIDM
               FROM SATURN.SFRSTCR stcr1
                  INNER JOIN SATURN.SSBSECT sect1
                     ON stcr1.SFRSTCR_TERM_CODE = sect1.SSBSECT_TERM_CODE
                    AND stcr1.SFRSTCR_CRN = sect1.SSBSECT_CRN
                    AND (stcr1.SFRSTCR_RSTS_CODE LIKE 'R%'
                     OR  stcr1.SFRSTCR_RSTS_CODE LIKE 'W%') 
                    AND stcr1.SFRSTCR_CREDIT_HR >= 1
                    AND sect1.SSBSECT_SUBJ_CODE IN ('ASTR', 'BIOL', 'CHEM', 'ENVS', 'GEOL', 'PHYS')
                    AND sect1.SSBSECT_CRSE_NUMB NOT LIKE '%L'
                    AND stcr1.SFRSTCR_TERM_CODE BETWEEN '&BeginAY' AND '&EndAY' 
                  INNER JOIN SATURN.SHRDGMR dgmr1
                     ON stcr1.SFRSTCR_PIDM = dgmr1.SHRDGMR_PIDM
                    AND dgmr1.SHRDGMR_GRST_CODE = 'AW'
                    AND dgmr1.SHRDGMR_TERM_CODE_GRAD BETWEEN '&BeginAY' AND '&EndAY' 
                    AND dgmr1.SHRDGMR_MAJR_CODE_1 = '&MajorCode'
               GROUP BY stcr1.SFRSTCR_PIDM
               HAVING COUNT(stcr1.SFRSTCR_CRN) = 2) iv1
      ON stcr2.SFRSTCR_PIDM = iv1.SFRSTCR_PIDM
ORDER BY iv1.SFRSTCR_PIDM,
         sect2.SSBSECT_SUBJ_CODE, 
         sect2.SSBSECT_CRSE_NUMB
4

1 回答 1

0

这里的问题是向 中添加两列GROUP BY会使HAVING子句过滤另一组行。

在第一个查询中,您会得到那些SFRSTCR_PIDMswhere for givenSFRSTCR_PIDM仅有两行来自 both 的结果JOINs

另一方面,第二个查询仅返回由三列组成的每组中只有两行的行SFRSTCR_PIDMSSBSECT_SUBJ_CODESSBSECT_CRSE_NUMB。这是完全不同的事情,也是你得到不同结果的原因。

你能提供样本数据和预期结果吗?我还建议您将加入表的条件和过滤记录的条件分开——这样会更容易看到发生了什么。最重要的是,表 DDL 也会有所帮助 - SQLFiddle 上的示例:SQLFiddle

于 2013-10-24T14:27:55.807 回答