2015-07-20 113 views
-6

這是我的查詢選擇不同的一列甲骨文

SELECT PHMR_ID, 
    NVOL_MVT_COD, 
    TO_CHAR(DATE_PREVUE, 'DD/MM/YYYY')  AS DATE_PREVUE, 
    TO_CHAR(DATE_ORIGINE_LT, 'DD/MM/YYYY') AS DATE_ORIGINE_LT, 
    HTAD, 
    DHC_AFF, 
    PKG, 
    PKG_GEO, 
    GATE, 
    TTB, 
    BQE, 
    SALLE, 
    ESCALE, 
    TA_CLE, 
    IM_IMMAT, 
    VOL_NB_PAX_GLOB, 
    VOL_CRIT_DOUANIER, 
    VOL_PROTOCOLE, 
    VOL_CORRESP, 
    VOL_ETAT_VOL, 
    NVL(TAS_GLOB, TER_COD1)      AS TAS_GLOB, 
    NVL(TER_TRAITEMENT, NVL(TAS_GLOB, TER_COD1)) AS TER_TRAITEMENT, 
    NUM_SIEGE, 
    VOL_ROT, 
    INFO_SUP, 
    TYPE_PREMIER_MSG, 
    PREMIER_MSG, 
    TYPE_DERNIER_MSG, 
    DERNIER_MSG, 
    TYP_JOUR, 
    PHMR_ID_ARP, 
    DECODE(NVOL_MVT_COD,'A',PHMR_ID_ARP,NULL) AS ID_ARP_A, 
    DECODE(NVOL_MVT_COD,'D',PHMR_ID_ARP,NULL) AS ID_ARP_D, 
    TYP_HORAIRE, 
    DUREE_CORRESP, 
    HOTEL, 
    TYPE_CONTACT, 
    RET_COD1, 
    RET_LIB1, 
    RET_COD2, 
    RET_LIB2, 
    HBLOC, 
    DISTINCT_LOT_CORRESPONDANCE, 
    OTHER_TYPE_MR, 
    TYPE_CHAISE, 
    STATUS_PHMR, 
    LISTE_MSG, 
    LIEU_RDV, 
    CLASSE, 
    PHMR_PRESTA_ID, 
    PRESTA, 
    DECODE(HORS_DELAI,1,'OUI','NON') AS HORSDELAI 
FROM VW_PHMR 
WHERE VW_PHMR.DATE_PREVUE >= to_date('06/07/2015','DD/MM/YYYY') 
AND VW_PHMR.DATE_PREVUE <= to_date('06/07/2015','DD/MM/YYYY') 
AND (phmr_valide   IS NULL 
OR phmr_valide    ='O') 
ORDER BY VW_PHMR.DATE_PREVUE ASC, 
    DHC ASC, 
    PHMR_ID ASC 

我需要得到唯一不同PHMR_ID(僅disticnt此列),我該怎麼辦呢?

要清楚;我有一個觀點: CREATE OR REPLACE FORCE VIEW VW_PHMR ( PHMR_ID, TER_COD1, PHMR_NOM, PHMR_PRENOM, TYPE_MR, CIE_SARIA, CIE_COD_IATA, CIE_COD_OACI, NVOL_LIG_NUM, NVOL_MVT_COD, DATE_PREVUE, DATE_ORIGINE_LT, DHC, HTAD, DHC_AFF, PKG, PKG_GEO, GATE, TTB, BQE, SALLE, ESCALE, TA_CLE, IM_IMMAT, VOL_NB_PAX_GLOB, VOL_CRIT_DOUANIER, VOL_PROTOCOLE, VOL_CORRESP, VOL_ETAT_VOL, TAS_GLOB, TER_TRAITEMENT, NUM_SIEGE, VOL_ROT, INFO_SUP, PREMIER_MSG, TYPE_PREMIER_MSG, DERNIER_MSG, TYPE_DERNIER_MSG, TYP_JOUR, PHMR_ID_ADP, TYP_HORAIRE, DUREE_CORRESP, HOTEL, TYPE_CONTACT, RET_COD1, RET_LIB1, RET_COD2, RET_LIB2, HBLOC, DISTINCT_LOT_CORRESPONDANCE, OTHER_TYPE_MR, TYPE_CHAISE, STATUS_PHMR, ID_STATUS, LISTE_MSG, LIEU_RDV, CLASSE, PHMR_VALIDE, PHMR_PRESTA_ID, PRESTA, HORS_DELAI ) AS SELECT DISTINCT p.phmr_id, p.ter_cod1, p.phmr_nom, p.phmr_prenom, p.liste_mr AS type_mr, p.cie_cle AS cie_saria, c.cie_cod_iata, c.cie_cod_oaci, p.nvol_lig_num, p.nvol_mvt_cod, p.date_exp AS date_prevue, p.vol_dt AS date_origine_lt, v.dhc AS DHC, TO_CHAR (v.HTAD, 'HH24:MI') AS HTAD, DECODE ( v.vol_hadb, NULL, v.DHC_TYP || TO_CHAR (v.dhc, 'HH24:MI'), DECODE (v.dhc_typ, 'N', 'D', v.dhc_typ) || TO_CHAR (v.vol_hadb, 'HH24:MI')) AS DHC_AFF, v.parking AS PKG, v.pkg_geo AS PKG_GEO, v.porte AS GATE, v.tapis AS TTB, v.banque AS BQE, v.salle AS SALLE, v.escale AS ESCALE, v.ta_cle, v.im_immat, v.vol_nb_pax_glob, v.vol_crit_douanier, v.protocole AS VOL_PROTOCOLE, p.vol_corresp, v.vol_etat_vol, v.tas_glob, v.tas_af_pax AS TER_TRAITEMENT, p.num_siege, v2.cie_cle || v2.nvol_lig_num AS vol_rot, p.INFO_SUP AS INFO_SUP, TO_CHAR (p.DH_PREMIER_MSG, 'DD/MM/YYYY HH24:MI') AS premier_msg, tm.typmes_cod AS type_premier_msg, TO_CHAR (p.DH_DERNIER_MSG, 'DD/MM/YYYY HH24:MI') AS dernier_msg, tm2.typmes_cod AS type_dernier_msg, DECODE (p.TYP_JOUR, NULL, 'S', p.TYP_JOUR), p.PHMR_ID_ADP, DECODE (p.TYP_HORAIRE, NULL, 'J', p.TYP_HORAIRE), p.DUREE_CORRESP, p.HOTEL, v.TYPE_CONTACT, v.RET_COD1, v.RET_LIB1, v.RET_COD2, v.RET_LIB2, v.vol_hadb, p.DISTINCT_LOT_CORRESPONDANCE, p.TYPE_MR, P.TYPE_CHAISE, sp.status_lib_court AS STATUS_PHMR, sp.PHMR_STATUS AS ID_STATUS, p.liste_message AS liste_msg, p.phmr_lieupc AS lieu_rdv, p.phmr_classe AS classe, p.phmr_valide AS phmr_valide, p.phmr_presta_id, p.presta, p.HORS_DELAI FROM phmr p, cie c, vw_vols v, vw_vols v2, type_messages tm, type_messages tm2, status_phmr sp WHERE c.cie_cle(+) = p.cie_cle AND v.vol_ident(+) = p.vol_ident AND v2.vol_ident(+) = v.vol_ident_rot AND tm.msg_typ(+) = p.TYP_PREMIER_MSG AND tm2.msg_typ(+) = p.TYP_DERNIER_MSG AND sp.phmr_status(+) = p.phmr_status;

當我在這個視圖下執行該命令我有1904行

現在,我需要有一個左連接來獲得另一列(我必須一直1904行),所以我修改我的視圖CREATE OR REPLACE FORCE VIEW VW_PHMR ( PHMR_ID, TER_COD1, PHMR_NOM, PHMR_PRENOM, TYPE_MR, CIE_SARIA, CIE_COD_IATA, CIE_COD_OACI, NVOL_LIG_NUM, NVOL_MVT_COD, DATE_PREVUE, DATE_ORIGINE_LT, DHC, HTAD, DHC_AFF, PKG, PKG_GEO, GATE, TTB, BQE, SALLE, ESCALE, TA_CLE, IM_IMMAT, VOL_NB_PAX_GLOB, VOL_CRIT_DOUANIER, VOL_PROTOCOLE, VOL_CORRESP, VOL_ETAT_VOL, TAS_GLOB, TER_TRAITEMENT, NUM_SIEGE, VOL_ROT, INFO_SUP, PREMIER_MSG, TYPE_PREMIER_MSG, DERNIER_MSG, TYPE_DERNIER_MSG, TYP_JOUR, PHMR_ID_ADP, TYP_HORAIRE, DUREE_CORRESP, HOTEL, TYPE_CONTACT, RET_COD1, RET_LIB1, RET_COD2, RET_LIB2, HBLOC, DISTINCT_LOT_CORRESPONDANCE, OTHER_TYPE_MR, TYPE_CHAISE, DERNIER_JALON, STATUS_PHMR, ID_STATUS, LISTE_MSG, LIEU_RDV, CLASSE, PHMR_VALIDE, PHMR_PRESTA_ID, PRESTA, HORS_DELAI ) AS SELECT DISTINCT p.phmr_id, p.ter_cod1, p.phmr_nom, p.phmr_prenom, p.liste_mr AS type_mr, p.cie_cle AS cie_saria, c.cie_cod_iata, c.cie_cod_oaci, p.nvol_lig_num, p.nvol_mvt_cod, p.date_exp AS date_prevue, p.vol_dt AS date_origine_lt, v.dhc AS DHC, TO_CHAR (v.HTAD, 'HH24:MI') AS HTAD, DECODE ( v.vol_hadb, NULL, v.DHC_TYP || TO_CHAR (v.dhc, 'HH24:MI'), DECODE (v.dhc_typ, 'N', 'D', v.dhc_typ) || TO_CHAR (v.vol_hadb, 'HH24:MI')) AS DHC_AFF, v.parking AS PKG, v.pkg_geo AS PKG_GEO, v.porte AS GATE, v.tapis AS TTB, v.banque AS BQE, v.salle AS SALLE, v.escale AS ESCALE, v.ta_cle, v.im_immat, v.vol_nb_pax_glob, v.vol_crit_douanier, v.protocole AS VOL_PROTOCOLE, p.vol_corresp, v.vol_etat_vol, v.tas_glob, v.tas_af_pax AS TER_TRAITEMENT, p.num_siege, v2.cie_cle || v2.nvol_lig_num AS vol_rot, p.INFO_SUP AS INFO_SUP, TO_CHAR (p.DH_PREMIER_MSG, 'DD/MM/YYYY HH24:MI') AS premier_msg, tm.typmes_cod AS type_premier_msg, TO_CHAR (p.DH_DERNIER_MSG, 'DD/MM/YYYY HH24:MI') AS dernier_msg, tm2.typmes_cod AS type_dernier_msg, DECODE (p.TYP_JOUR, NULL, 'S', p.TYP_JOUR), p.PHMR_ID_ADP, DECODE (p.TYP_HORAIRE, NULL, 'J', p.TYP_HORAIRE), p.DUREE_CORRESP, p.HOTEL, v.TYPE_CONTACT, v.RET_COD1, v.RET_LIB1, v.RET_COD2, v.RET_LIB2, v.vol_hadb, p.DISTINCT_LOT_CORRESPONDANCE, p.TYPE_MR, P.TYPE_CHAISE, **p2.TYP_DERNIER_JALON AS DERNIER_JALON**, sp.status_lib_court AS STATUS_PHMR, sp.PHMR_STATUS AS ID_STATUS, p.liste_message AS liste_msg, p.phmr_lieupc AS lieu_rdv, p.phmr_classe AS classe, p.phmr_valide AS phmr_valide, p.phmr_presta_id, p.presta, p.HORS_DELAI FROM phmr p, **phmr_presta p2**, cie c, vw_vols v, vw_vols v2, type_messages tm, type_messages tm2, status_phmr sp WHERE c.cie_cle(+) = p.cie_cle AND v.vol_ident(+) = p.vol_ident AND v2.vol_ident(+) = v.vol_ident_rot AND tm.msg_typ(+) = p.TYP_PREMIER_MSG AND tm2.msg_typ(+) = p.TYP_DERNIER_MSG AND sp.phmr_status(+) = p.phmr_status **AND p.phmr_id(+) = p2.phmr_id**;

我該如何解決這個問題?

Regards

+1

PHMR_ID使用組 – apomene

+3

您必須決定將爲其他列返回的值。 'Min'? 'Max'? 'sum'?,'Count'或其他集合函數。否則,你不能只得到一個「獨特」。你可以寫一個單獨的查詢只返回列的不同值但是...顯示的一些數據和預期的結果,我們也許可以想出不同的方法(或寫入SQLFIDDLE.com) – xQbert

+0

我解釋我需要在基於您的評論/回答另一個評論 – user1428798

回答

2

我認爲,如果你更換:

FROM phmr p, 
     **phmr_presta p2**, 

隨着

FROM phmr p, 
(SELECT IP1.PHMR_ID, IP1.TYP_DERNIER_JALON 
FROM phmr_Presta IP1, 
     (SELECT phmr_id, max(phmr_presta_id) M_PRESTA_ID 
     FROM phmr_presta 
     GROUP BY phmr_id 
     ) IP2 
where IP1.PHMR_ID = IP2.PHMR_ID 
    and IP1.PHMR_PRESTA_ID = IP2.M_PRESTA_ID) P2 

你會得到期望的結果。

這樣做是爲phmr_presta中的每個phmr_ID獲取max_PHMR_PRESETA_ID。然後它返回到PHMR_PRESTA以獲取您所要的TYP_DERNIER_JALON。然後,我將它替換爲您擁有的表格,因爲這是該表格的子集。

可能有更有效的方式來編寫整個視圖。但我關注的是你所遇到的具體問題,而不是整個觀點。哦... ,你應該不需要在此之後有所區別...

如果你用樣本數據創建一個小提琴,我可以測試這個,但我不傾向於沒有數據和模型你提供。所以這是UNTESTED,可能有一些語法錯誤。

注意未來的問題:消除所有額外的列等。將問題分解爲最簡單的形式並將其作爲問題。這裏額外的列和表是「噪音」,這使得這有點多思考。而且相當不必要。

+0

非常感謝您的答案和您的意見。我認爲語法不正確! – user1428798

0

這就是你怎麼做到的。如果你想要別的東西,那麼別忘了寫一個更好的規範。

select distinct PHMR_ID 
from VW_PHMR 
+0

我解釋我的需要在另一個評論 – user1428798