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

Oracle Discussion :

pivot+ oracle 11g


Sujet :

Oracle

  1. #1
    Membre du Club
    Profil pro
    MOE
    Inscrit en
    Juillet 2007
    Messages
    62
    Détails du profil
    Informations personnelles :
    Âge : 41
    Localisation : France

    Informations professionnelles :
    Activité : MOE

    Informations forums :
    Inscription : Juillet 2007
    Messages : 62
    Points : 42
    Points
    42
    Par défaut pivot+ oracle 11g
    Bonjour,

    j 'ai une table appelée " tache " qui contient comme champ : (idProjet, statutProjet, nomcourt)

    je veux bien avoir le nom des projet en colonne :



    ------------- nom1 -------- nom2 ------- nom3 ...

    idProjet1 statutProjet statutProjet statutProjet


    idProjet2 statutProjet statutProjet statutProjet



    idProjet3 statutProjet statutProjet statutProjet



    idProjet4 statutProjet statutProjet statutProjet



    Pour cela, j ai ecrit la requete suivante :


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    select idProjet,  statutProjet
     
    from tache   
     
    PIVOT ( max(statutProjet)
     
    for nomCourt in (select  listagg( ''''||libelle||''' as "'||libelle||'"',',')  
     
    within GROUP (ORDER BY null) FROM (SELECT DISTINCT nomCourt AS libelle 
     
      FROM tache)));
    Par contre, j 'ai une erreur : ORA-00936 expression absente, sachant que :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    select  listagg( ''''||libelle||''' as "'||libelle||'"',',')  
     
    within GROUP (ORDER BY null) FROM (SELECT DISTINCT nomCourt AS libelle 
     
      FROM tache)
    me retient bien une liste dynamique.

  2. #2
    Expert confirmé
    Profil pro
    Inscrit en
    Août 2008
    Messages
    2 947
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2008
    Messages : 2 947
    Points : 5 846
    Points
    5 846
    Par défaut
    Tu ne peux pas exploiter le listagg directement. Il faut d'abord générer la liste puis l'exploiter dans une requête dynamique.
    Regarde ce post :
    http://www.developpez.net/forums/d11...g/#post6206926

  3. #3
    Membre du Club
    Profil pro
    MOE
    Inscrit en
    Juillet 2007
    Messages
    62
    Détails du profil
    Informations personnelles :
    Âge : 41
    Localisation : France

    Informations professionnelles :
    Activité : MOE

    Informations forums :
    Inscription : Juillet 2007
    Messages : 62
    Points : 42
    Points
    42
    Par défaut
    j 'ai ecrit le code suivant :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
     
    var r refcursor
     declare
       courtNomList varchar2(4000);
      begin
          SELECT listagg( ''''||libelle||''' as "'||libelle||'"',',') within GROUP (ORDER BY libelle)
           INTO courtNomList FROM (SELECT DISTINCT nomcourt AS libelle FROM tache );
          open :r FOR
          ' select t.projet, t.nomcourt,t.statutProjet
            from tache t
            pivot (max (statutProjet) for (nomcourt) in ('||courtNomList||'))
            ';
       end;

    par contre j a'i eu ce message d'erreur :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
     
    ORA-00972: l'identificateur est trop long
    ORA-06512: à ligne 6
    00972. 00000 -  "identifier is too long"
    *Cause:    An identifier with more than 30 characters was specified.
    *Action:   Specify at most 30 characters.
    Cependant
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    SELECT  listagg( ''''||libelle||''' as "'||libelle||'"',',')  
     
    within GROUP (ORDER BY NULL) FROM (SELECT DISTINCT nomCourt AS libelle 
     
      FROM tache)
    fonctionne et retourne bien une liste !!

    Merci d'avance

  4. #4
    Expert confirmé
    Profil pro
    Inscrit en
    Août 2008
    Messages
    2 947
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2008
    Messages : 2 947
    Points : 5 846
    Points
    5 846
    Par défaut
    Il faut passer par une sous requête, Pivot and Unpivot :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    select * 
      from (select t.projet, t.nomcourt,t.statutProjet
              from tache t)
     pivot (max (statutProjet) for (nomcourt) in ('||courtNomList||'))

  5. #5
    Membre du Club
    Profil pro
    MOE
    Inscrit en
    Juillet 2007
    Messages
    62
    Détails du profil
    Informations personnelles :
    Âge : 41
    Localisation : France

    Informations professionnelles :
    Activité : MOE

    Informations forums :
    Inscription : Juillet 2007
    Messages : 62
    Points : 42
    Points
    42
    Par défaut
    j ai modifié mon code :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
     
     
     
    CREATE OR REPLACE PROCEDURE affiche()
    IS
    var r refcursor
     declare
       courtNomList varchar2(4000);
      begin
          SELECT listagg( ''''||libelle||''' as "'||libelle||'"',',') within GROUP (ORDER BY libelle)
           INTO courtNomList FROM (SELECT DISTINCT nomcourt AS libelle FROM tache where projet between 1 and 1000 ORDER BY nomcourt );
               dbms_output.put_line(courtNomList);
          open :r FOR
          ' select * from (select t.projet, t.nomcourt,t.statuttache
            from tache t)
            pivot (max (statuttache) for (nomcourt) in ('||courtNomList||'))
            ';
     
       end;

    Par contre j 'ai deux probleme :

    1/ la procedure est compilée mais rien n'est affiché :

    Elément PROCEDURE affiche compilé
    Warning: exécution terminée avec avertissement

    2/ je suis obligé d'ajouter where projet between 1 and 1000 ORDER BY nomcourt
    sinon j ai ce message


    ORA-00972: l'identificateur est trop long
    ORA-06512: à ligne 7
    00972. 00000 - "identifier is too long"
    *Cause: An identifier with more than 30 characters was specified.
    *Action: Specify at most 30 characters.

    Merci d'avance

  6. #6
    Expert confirmé
    Profil pro
    Inscrit en
    Août 2008
    Messages
    2 947
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2008
    Messages : 2 947
    Points : 5 846
    Points
    5 846
    Par défaut
    Syntaxiquement tu confonds du sqlplus et du pl/sql.
    Si tu souhaites céer une procedure tu peux faire, selon mon précédent exemple :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    SQL> create or replace procedure p (r out sys_refcursor) as
      2   joblist varchar2(4000);
      3  begin
      4    SELECT listagg( ''''||libelle||''' as "'||libelle||'"',',') within GROUP (ORDER BY libelle)
      5      INTO joblist FROM (SELECT DISTINCT job AS libelle FROM emp);
      6  
      7    open r FOR
      8         'select *
      9            from (
     10          select d.dname, e.job, e.sal
     11            from dept d
     12            join emp e on e.deptno = d.deptno
     13                 )
     14           pivot (sum(sal) for (job) in ('||joblist||'))';
     15  end;
     16  /
     
    Procedure created.
     
    SQL> var rc refcursor
    SQL> exec p(:rc);
     
    PL/SQL procedure successfully completed.
     
    SQL> print rc
     
    DNAME             ANALYST      CLERK    MANAGER  PRESIDENT   SALESMAN
    -------------- ---------- ---------- ---------- ---------- ----------
    ACCOUNTING                      1300       2450       5000
    RESEARCH             6000       1900       2975
    SALES                            950       2850                  5600
     
    SQL>
    Pour voir les erreurs à la compilation depuis sqlplus faire (ça c'est pour le 1) ) :
    Par contre je ne sais pas s'il y a une limite en colonne pour le pivot.

Discussions similaires

  1. install Oracle 11g sur Enterprise Linux Oracle
    Par Soutou dans le forum Installation
    Réponses: 2
    Dernier message: 31/10/2007, 11h38
  2. mandriva 2008 & oracle 11g
    Par jmjmjm dans le forum Installation
    Réponses: 2
    Dernier message: 28/10/2007, 20h29
  3. Installation de Oracle 11g sous Fedora 5 bloquée à 77%
    Par Yann39 dans le forum Installation
    Réponses: 3
    Dernier message: 26/10/2007, 00h16
  4. Oracle 11g disponible sur Linux
    Par pifor dans le forum Oracle
    Réponses: 8
    Dernier message: 16/08/2007, 23h28

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