2017-03-15 168 views
0

我需要兩個結合兩個查詢才能得到一個結果。這是第一個代碼。如何結合兩個查詢來獲得一個結果?

SELECT FKLTI_KTRGN, COUNT(DISTINCT VIEW_PELAJAR_ENROLL.MB_NAMA) AS BIL_UG 
FROM VIEW_PELAJAR_ENROLL, KOD_BANGSA, KOD_NEGERI, JANTINA, KOD_AGAMA, KOD_CACAT, TARAF_KAHWIN 
WHERE KOD_KTRGN_PROGRAM = 'SARJANA MUDA' 
AND MS_SESI = '2015/2016' 
AND MB_BANGSA = KOD_BANGSA 
AND KOD_NEGERI = MB_ASAL 
AND MB_JANTINA = JAN_KOD 
AND KOD_AGAMA = MB_AGAMA 
AND KOD_CACAT = MB_CACAT 
AND KOD_TARAF = MB_TARAF_KAHWIN 
AND MS_STATUS_SEMASA IN 
(SELECT SP_KOD 
FROM STATUS_PELAJAR 
WHERE STATUS_ENROLL = 'Y' 
AND SP_TAMAT IS NULL 
AND SP_KOD = MS_STATUS_SEMASA) 
GROUP BY FKLTI_KTRGN 

這是第二個代碼。

SELECT FKLTI_KTRGN, COUNT(DISTINCT PELAJAR_BIODATA.MBUT_NAMA) AS BIL_PG 
       FROM PELAJAR_BIODATA,KOD_NEGERI,FAKULTI,KOD_IJAZAH,PELAJAR_BUTIR_PENGAJIAN,STATUS_PENGAJIAN A, 
       KOD_STATUS_PELAJAR_IPS, KOD_CACAT, KOD_BANGSA, E_KOD_PENGAJIAN, JANTINA, KOD_PROGRAM 
       WHERE A.STAT_STATUS IN (SELECT KOD_STATUS 
       FROM KOD_STATUS_PELAJAR_IPS 
       WHERE STATUS_ENROLL='Y' 
       AND KOD_STATUS=STAT_STATUS) 
       AND A.STAT_NOMKPB=MBUT_NOMKPB 
       AND PBP_PROGRAM IN ('5','6') 
       AND MBUT_ASAL=KOD_NEGERI(+) 
       AND SUBSTR(A.STAT_KOD_IJAZAH,1,1)=FKLTI_KOD 
       AND PBP_PROGRAM=KOD_PROGRAM.KOD_PROGRAM 
       AND A.STAT_KOD_IJAZAH=KOD_IJAZAH_UM 
       AND A.STAT_NODAFTAR=PBP_NODAFTAR 
       AND MBUT_CACAT=KOD_CACAT 
       AND KOD_BANGSA=MBUT_BANGSA 
       AND PBP_JENIS_PENGAJIAN=KOD_JNS_PENGAJIAN 
       AND MBUT_JANTINA=JAN_KOD 
       AND MBUT_WARGA IS NOT NULL 
       AND MBUT_BANGSA IS NOT NULL 
       AND MBUT_JANTINA IS NOT NULL 
       AND MBUT_NEGERI IS NOT NULL 
       AND PBP_PROGRAM IS NOT NULL 
       AND A.STAT_STATUS=KOD_STATUS 
       AND PBP_KOD_IJAZAH IS NOT NULL 
       AND A.STAT_SESI||A.STAT_SEMESTER IN (SELECT MAX(B.STAT_SESI||B.STAT_SEMESTER) FROM STATUS_PENGAJIAN B 
       WHERE B.STAT_NODAFTAR=A.STAT_NODAFTAR 
       AND B.STAT_NOMKPB=A.STAT_NOMKPB 
       AND A.STAT_SESI||A.STAT_SEMESTER=B.STAT_SESI||B.STAT_SEMESTER) 
       AND A.STAT_SESI = '2015/2016' 
       group by FKLTI_KTRGN 

這是預期的結果:

​​

這就是我得到:

actual result

我試着使用UNION,但它不工作。任何人都可以幫助我?我真的需要你的幫助。先謝謝你!

+0

究竟是如何'union'不爲你工作?兩個查詢都返回兩個相同類型的列 - 所以union *應該可以工作。 –

+0

聯盟應該工作,你得到了什麼錯誤?但是,你想要什麼,你能顯示每個查詢的樣本輸出,以及這些樣本輸出的預期結果嗎? –

+0

爲每個查詢添加示例結果,並向我們展示組合結果以及所有格式化文本! – jarlh

回答

0

爲了使用聯合,您需要具有相同數量的字段和兩個查詢的相同字段名稱。

在你的兩個查詢,第二個字段具有不同的名稱:BIL_PG,BIL_UG

+0

列名無關緊要,只有數量/順序和列類型應該匹配。來自第一個查詢的列名正被用作結果的列名。 –

0

試試這個

select * from 
    (SELECT FKLTI_KTRGN, COUNT(DISTINCT VIEW_PELAJAR_ENROLL.MB_NAMA) AS BIL_UG 
    FROM VIEW_PELAJAR_ENROLL, KOD_BANGSA, KOD_NEGERI, JANTINA, KOD_AGAMA, KOD_CACAT, TARAF_KAHWIN 
    WHERE KOD_KTRGN_PROGRAM = 'SARJANA MUDA' 
    AND MS_SESI = '2015/2016' 
    AND MB_BANGSA = KOD_BANGSA 
    AND KOD_NEGERI = MB_ASAL 
    AND MB_JANTINA = JAN_KOD 
    AND KOD_AGAMA = MB_AGAMA 
    AND KOD_CACAT = MB_CACAT 
    AND KOD_TARAF = MB_TARAF_KAHWIN 
    AND MS_STATUS_SEMASA IN 
    (SELECT SP_KOD 
    FROM STATUS_PELAJAR 
    WHERE STATUS_ENROLL = 'Y' 
    AND SP_TAMAT IS NULL 
    AND SP_KOD = MS_STATUS_SEMASA) 
    GROUP BY FKLTI_KTRGN) 

    union 

    select * from (SELECT FKLTI_KTRGN, COUNT(DISTINCT PELAJAR_BIODATA.MBUT_NAMA) AS BIL_PG 
        FROM PELAJAR_BIODATA,KOD_NEGERI,FAKULTI,KOD_IJAZAH,PELAJAR_BUTIR_PENGAJIAN,STATUS_PENGAJIAN A, 
        KOD_STATUS_PELAJAR_IPS, KOD_CACAT, KOD_BANGSA, E_KOD_PENGAJIAN, JANTINA, KOD_PROGRAM 
        WHERE A.STAT_STATUS IN (SELECT KOD_STATUS 
        FROM KOD_STATUS_PELAJAR_IPS 
        WHERE STATUS_ENROLL='Y' 
        AND KOD_STATUS=STAT_STATUS) 
        AND A.STAT_NOMKPB=MBUT_NOMKPB 
        AND PBP_PROGRAM IN ('5','6') 
        AND MBUT_ASAL=KOD_NEGERI(+) 
        AND SUBSTR(A.STAT_KOD_IJAZAH,1,1)=FKLTI_KOD 
        AND PBP_PROGRAM=KOD_PROGRAM.KOD_PROGRAM 
        AND A.STAT_KOD_IJAZAH=KOD_IJAZAH_UM 
        AND A.STAT_NODAFTAR=PBP_NODAFTAR 
        AND MBUT_CACAT=KOD_CACAT 
        AND KOD_BANGSA=MBUT_BANGSA 
        AND PBP_JENIS_PENGAJIAN=KOD_JNS_PENGAJIAN 
        AND MBUT_JANTINA=JAN_KOD 
        AND MBUT_WARGA IS NOT NULL 
        AND MBUT_BANGSA IS NOT NULL 
        AND MBUT_JANTINA IS NOT NULL 
        AND MBUT_NEGERI IS NOT NULL 
        AND PBP_PROGRAM IS NOT NULL 
        AND A.STAT_STATUS=KOD_STATUS 
        AND PBP_KOD_IJAZAH IS NOT NULL 
        AND A.STAT_SESI||A.STAT_SEMESTER IN (SELECT MAX(B.STAT_SESI||B.STAT_SEMESTER) FROM STATUS_PENGAJIAN B 
        WHERE B.STAT_NODAFTAR=A.STAT_NODAFTAR 
        AND B.STAT_NOMKPB=A.STAT_NOMKPB 
        AND A.STAT_SESI||A.STAT_SEMESTER=B.STAT_SESI||B.STAT_SEMESTER) 
        AND A.STAT_SESI = '2015/2016' 
        group by FKLTI_KTRGN) 
+0

如何將查詢移動到子查詢應該有助於這種特殊情況? –

+0

可能存在明顯的問題並計數 – Vecchiasignora

+0

不可能有這樣的問題。 'count(distinct ...)'不影響將查詢與'union'結合起來。 –

相關問題