IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

SQL Oracle Discussion :

First_value et last_value


Sujet :

SQL Oracle

  1. #1
    Membre très actif
    Inscrit en
    Avril 2005
    Messages
    238
    Détails du profil
    Informations forums :
    Inscription : Avril 2005
    Messages : 238
    Par défaut First_value et last_value
    Bonjour,
    En utilisant first_value et last_value dans le code qui est dans l'image en pj,
    j'obtiens bien les valeur de la donnée HOPHJOU.IJRECARANN.

    Seulement ce code me donne 245 lignes, alors que je voudrais obtenir une seule ligne :

    Matri /début /firstHOPHJOUIJRECARANN/ FIN/ HOPHJOU.IJRECARANN
    000689 01/05/2012 -1105.33 31/12/2012 -335.33

    Dans le code j'ai utiliser uniquement un matricule pour essayer.

    Merci pour votre aide
    Images attachées Images attachées  

  2. #2
    Membre Expert Avatar de Garuda
    Homme Profil pro
    Chef de projet / Urbaniste SI
    Inscrit en
    Juin 2007
    Messages
    1 285
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Vaucluse (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Chef de projet / Urbaniste SI
    Secteur : Bâtiment

    Informations forums :
    Inscription : Juin 2007
    Messages : 1 285
    Par défaut
    La requete est illisible dans la piéce jointe (et le copier impossible).
    Merci d'envoyer la requête dans le message (avec les balises CODE) !

  3. #3
    Membre très actif
    Inscrit en
    Avril 2005
    Messages
    238
    Détails du profil
    Informations forums :
    Inscription : Avril 2005
    Messages : 238
    Par défaut
    Voici le code en plus clair
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    SELECT HOPHJOU.SECTORI,
    HOPHJOU.MATRI,
    HOPHJOU.IJRECARANN,
    first_value(HOPHJOU.IJRECARANN/60) over (partition by HOPHJOU.SECTORI order by HOPHJOU.MATRI) a,
    last_value(HOPHJOU.IJRECARANN/60) over (partition by HOPHJOU.SECTORI order by HOPHJOU.MATRI desc
    rows between current row and unbounded following) b
    from HOPHJOU
    WHERE HOPHJOU.MATRI = '000689' AND HOPHJOU.SECTORI = '012234' and EXTRACT(YEAR FROM DAT = 2012

  4. #4
    Expert confirmé Avatar de mnitu
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Octobre 2007
    Messages
    5 611
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Marne (Champagne Ardenne)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Octobre 2007
    Messages : 5 611
    Par défaut
    Utilisez First/Last dans leur version de fonction agrégées (group by ) et non pas des fonctions de fenêtrage (over(...).

  5. #5
    Membre très actif
    Inscrit en
    Avril 2005
    Messages
    238
    Détails du profil
    Informations forums :
    Inscription : Avril 2005
    Messages : 238
    Par défaut
    Merci pour votre réponse mais comment on l'utilise en Group By.

  6. #6
    Modérateur
    Avatar de Waldar
    Homme Profil pro
    Sr. Specialist Solutions Architect @Databricks
    Inscrit en
    Septembre 2008
    Messages
    8 454
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 48
    Localisation : France, Val de Marne (Île de France)

    Informations professionnelles :
    Activité : Sr. Specialist Solutions Architect @Databricks
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2008
    Messages : 8 454
    Par défaut
    Et encore, je me demande si le besoin ce n'est pas min / max, parce que la partition et le order by sur des éléments fixes, on perd une grosse partie de l'intérêt des fonctions first_value et last_value.

    Comme souvent, un jeu de données concis mais précis est toujours intéressant pour illustrer son besoin.

  7. #7
    Expert confirmé Avatar de mnitu
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Octobre 2007
    Messages
    5 611
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Marne (Champagne Ardenne)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Octobre 2007
    Messages : 5 611
    Par défaut
    Citation Envoyé par jopont Voir le message
    Merci pour votre réponse mais comment on l'utilise en Group By.
    FIRST

  8. #8
    Membre actif
    Profil pro
    Inscrit en
    Juillet 2012
    Messages
    21
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2012
    Messages : 21
    Par défaut
    mes petits copains sont un peu tâtillons...

    un select distinct de ton expression fera l'affaire je crois...et plutôt que de t'attirer des problèmes avec la fonction last_value (...) qui est un peu siouxe, utilise

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    first_value () over (...order by ...asc)
    pour ta première colonne

    puis

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    first_value () over (...order by ...desc)
    pour la seconde

    bien sûr on aurait pu le faire avec un bon vieux group by et/ou avec un min et un max. Mais bon! laissons chacun libre dans sa tête ;-)

  9. #9
    Membre très actif
    Inscrit en
    Avril 2005
    Messages
    238
    Détails du profil
    Informations forums :
    Inscription : Avril 2005
    Messages : 238
    Par défaut
    Le code pourrait donc s'écrire comme ci-dessous ?
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    SELECT DISTINCT HOPHJOU.SECTORI,
    HOPHJOU.MATRI,
    HOPHJOU.IJRECARANN,
    first_value(HOPHJOU.IJRECARANN/60) over (partition BY HOPHJOU.SECTORI ORDER BY HOPHJOU.MATRI ASC) a,
    first_value(HOPHJOU.IJRECARANN/60) over (partition BY HOPHJOU.SECTORI ORDER BY HOPHJOU.MATRI DESC
    rows BETWEEN current row AND unbounded following) b
    FROM HOPHJOU
    WHERE HOPHJOU.MATRI = '000689' AND HOPHJOU.SECTORI = '012234' AND EXTRACT(YEAR FROM DAT = 2012
    Merci

  10. #10
    Membre très actif
    Inscrit en
    Avril 2005
    Messages
    238
    Détails du profil
    Informations forums :
    Inscription : Avril 2005
    Messages : 238
    Par défaut
    Bonjour,

    Finalement je suis passé par un GROUP BY avec MIN et MAX.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    SELECT HOPROLE.SELIDE,HOPHJOU.MATRI,HOPEMPL.SEIEQUIP,HOPEMPL.SEITRI,HOPSECH.HORSECT,HOPEMPL.SEIGRADEO,HOPEMPL.NOMPRE,
           MIN(HOPHJOU.DAT) KEEP (DENSE_RANK FIRST ORDER BY HOPHJOU.SECTORI) "Date Deb",
           MAX(HOPHJOU.DAT) KEEP (DENSE_RANK FIRST ORDER BY HOPHJOU.SECTORI) "Date Fin",
           MIN(HOPHJOU.IJRECARANN/60) KEEP (DENSE_RANK FIRST ORDER BY HOPHJOU.SECTORI) "TT début",
           MAX(HOPHJOU.IJRECARANN/60) KEEP (DENSE_RANK LAST ORDER BY HOPHJOU.SECTORI) "TT Fin"
      FROM HOPHJOU
      INNER JOIN HOPEMPL ON HOPEMPL.MATRI = HOPHJOU.MATRI
      INNER JOIN HOPSECH ON HOPSECH.HORSECT = HOPHJOU.SECTORI
      INNER JOIN HOPROLE ON HOPROLE.MATRI = HOPHJOU.MATRI
      WHERE  HOPROLE.SELIDE = 'SPOSPP123' AND SUBSTR(HOPSECH.HORSECT,3,4) = '2234' AND (HOPHJOU.MATRI = '003244' OR HOPHJOU.MATRI = '006820' OR HOPHJOU.MATRI = '000689')AND (HOPHJOU.CTTYPPOP = 'P' OR HOPHJOU.CTTYPPOP = 'W')AND EXTRACT(YEAR FROM HOPHJOU.DAT) = 2012   
      GROUP BY HOPROLE.SELIDE,HOPHJOU.MATRI, HOPEMPL.NOMPRE,HOPSECH.HORSECT,HOPEMPL.SEIEQUIP,HOPEMPL.SEITRI,HOPEMPL.SEIGRADEO
      ORDER BY HOPEMPL.SEIEQUIP,HOPEMPL.SEITRI;
    J'ai une autre petite question :

    Comment faire afficher dans une autre colonne la valeur de TT Début si DATE Fin = 31/12/2012 et sinon la valeur la valeur TT Début moins TT Fin pour tout autre date ?

    Merci

  11. #11
    Expert confirmé Avatar de mnitu
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Octobre 2007
    Messages
    5 611
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Marne (Champagne Ardenne)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Octobre 2007
    Messages : 5 611
    Par défaut
    Avec une instruction Case.

  12. #12
    Membre très actif
    Inscrit en
    Avril 2005
    Messages
    238
    Détails du profil
    Informations forums :
    Inscription : Avril 2005
    Messages : 238
    Par défaut
    Oui mais je ne sais pas où ni comment placer cette instruction

  13. #13
    Expert confirmé Avatar de mnitu
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Octobre 2007
    Messages
    5 611
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Marne (Champagne Ardenne)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Octobre 2007
    Messages : 5 611

  14. #14
    Membre très actif
    Inscrit en
    Avril 2005
    Messages
    238
    Détails du profil
    Informations forums :
    Inscription : Avril 2005
    Messages : 238
    Par défaut
    Merci, je connais la syntaxe simple de l'instruction case, mais je ne sais pas comment l'intégrer dans le code ci-dessus.

  15. #15
    Membre très actif
    Inscrit en
    Avril 2005
    Messages
    238
    Détails du profil
    Informations forums :
    Inscription : Avril 2005
    Messages : 238
    Par défaut
    Bonjour,

    Dans la requête ci-dessous, comment faire afficher la donnée IJRTTEPMOD qui correspond à la date du jour ?
    J'ai essayé avec SYSDATE mais je ne sais pas comment mettre ça en plce dans le code.
    merci
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    SELECT HOPROLE.SELIDE,HOPHJOU.MATRI,HOPEMPL.SEIEQUIP,HOPEMPL.SEITRI,HOPSECH.HORSECT,HOPEMPL.SEIGRADEO,HOPEMPL.NOMPRE,
           MIN(HOPHJOU.DAT) KEEP (DENSE_RANK FIRST ORDER BY HOPHJOU.SECTORI) "Deb",
           MAX(HOPHJOU.DAT) KEEP (DENSE_RANK FIRST ORDER BY HOPHJOU.SECTORI) "Fin", 
           MIN(HOPHJOU.IJRTTEPMOD/60) KEEP (DENSE_RANK FIRST ORDER BY HOPHJOU.SECTORI) "TTDEB",
           MAX(HOPHJOU.IJRTTEPMOD/60) KEEP (DENSE_RANK LAST ORDER BY HOPHJOU.SECTORI) "TTFIN"
      FROM HOPHJOU
      INNER JOIN HOPEMPL ON HOPEMPL.MATRI = HOPHJOU.MATRI
      INNER JOIN HOPSECH ON HOPSECH.HORSECT = HOPHJOU.SECTORI
      INNER JOIN HOPROLE ON HOPROLE.MATRI = HOPHJOU.MATRI
      WHERE  HOPROLE.SELIDE = 'SPOSPP123' AND SUBSTR(HOPSECH.HORSECT,3,4) = '2234'  AND (HOPHJOU.CTTYPPOP = 'P' OR HOPHJOU.CTTYPPOP = 'W')AND EXTRACT(YEAR FROM HOPHJOU.DAT) = 2012  
      GROUP BY HOPROLE.SELIDE,HOPHJOU.MATRI, HOPEMPL.NOMPRE,HOPSECH.HORSECT,HOPEMPL.SEIEQUIP,HOPEMPL.SEITRI,HOPEMPL.SEIGRADEO
      ORDER BY HOPEMPL.SEIEQUIP,HOPEMPL.SEITRI;

  16. #16
    Membre très actif
    Inscrit en
    Avril 2005
    Messages
    238
    Détails du profil
    Informations forums :
    Inscription : Avril 2005
    Messages : 238
    Par défaut
    j'ai essayé avec le code ci-dessous, j'arrive à récupérer la valeur si la date est inférieur à la date du jour.
    Par contre je n'arrive pas à récupérer la valeur de IJRETTEPMOD à la date du jour.
    comment faire ?
    merci
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    SELECT HOPROLE.SELIDE,HOPHJOU.MATRI,HOPEMPL.SEIEQUIP,HOPEMPL.SEITRI,HOPSECH.HORSECT,HOPEMPL.SEIGRADEO,HOPEMPL.NOMPRE,
           MIN(HOPHJOU.DAT) KEEP (DENSE_RANK FIRST ORDER BY HOPHJOU.SECTORI) "Deb",
           (CASE WHEN MAX(HOPHJOU.DAT) KEEP (DENSE_RANK FIRST ORDER BY HOPHJOU.SECTORI)< trunc(SYSDATE) then  MAX(HOPHJOU.DAT) KEEP (DENSE_RANK FIRST ORDER BY HOPHJOU.SECTORI) else trunc(SYSDATE) END) as "fin",
           (CASE WHEN MAX(HOPHJOU.DAT) KEEP (DENSE_RANK FIRST ORDER BY HOPHJOU.SECTORI)< trunc(SYSDATE) then  MAX(HOPHJOU.IJRTTEPMOD/60) KEEP (DENSE_RANK FIRST ORDER BY HOPHJOU.SECTORI)  END) as "TTFIN"
      FROM HOPHJOU
      INNER JOIN HOPEMPL ON HOPEMPL.MATRI = HOPHJOU.MATRI
      INNER JOIN HOPSECH ON HOPSECH.HORSECT = HOPHJOU.SECTORI
      INNER JOIN HOPROLE ON HOPROLE.MATRI = HOPHJOU.MATRI
      WHERE  HOPROLE.SELIDE = 'SPOSPP123' AND SUBSTR(HOPSECH.HORSECT,3,4) = '2234'  AND (HOPHJOU.CTTYPPOP = 'P' OR HOPHJOU.CTTYPPOP = 'W')AND EXTRACT(YEAR FROM HOPHJOU.DAT) = 2012   
      GROUP BY HOPROLE.SELIDE,HOPHJOU.MATRI, HOPEMPL.NOMPRE,HOPSECH.HORSECT,HOPEMPL.SEIEQUIP,HOPEMPL.SEITRI,HOPEMPL.SEIGRADEO
      ORDER BY HOPEMPL.SEIEQUIP,HOPEMPL.SEITRI

Discussions similaires

  1. le comportement de la fonction first_value
    Par saigon dans le forum Requêtes
    Réponses: 6
    Dernier message: 09/11/2011, 17h32
  2. Différence de syntaxe, (KEEP, FIRST_VALUE)
    Par Drizzt [Drone38] dans le forum SQL
    Réponses: 4
    Dernier message: 04/11/2009, 12h21
  3. [SQL / PL/SQL] fonction analytique last_value
    Par Nounoursonne dans le forum SQL
    Réponses: 7
    Dernier message: 23/08/2007, 21h18
  4. [SQL / PL/SQL] fonction analytique last_value
    Par Nounoursonne dans le forum Oracle
    Réponses: 7
    Dernier message: 23/08/2007, 21h18
  5. Pb Fonction analytique last_value
    Par McM dans le forum SQL
    Réponses: 8
    Dernier message: 03/08/2007, 17h23

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo