选择不同的一列oracle

时间:2015-07-20 13:40:43

标签: sql oracle oracle11g

这是我的查询

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**;

我该如何解决这个问题?

此致

2 个答案:

答案 0 :(得分: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并且可能有一些语法错误。

注意未来的问题:消除所有额外的列等。将问题分解为最简单的形式并将其作为问题。这里额外的列和表是&#34; noise&#34;尽管如此,还是要多考虑一下;并且非常不必要。

答案 1 :(得分:0)

这就是你如何做到的。如果你想要别的东西,那就别忘了写一个更好的规范。

select distinct PHMR_ID
from VW_PHMR