Précédent   Forum des professionnels en informatique > Bases de données > Oracle > SQL
SQL Forum d'entraide sur le SQL pour Oracle
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse Proposer ce sujet en actualité
 
Outils de la discussion
Publicité
'
Vieux 26/07/2011, 16h09   #1
Candidat au titre de Membre du Club
 
Justine Dreyfus
Inscription : juin 2010
Messages : 109
Détails du profil
Informations personnelles :
Nom : Justine Dreyfus

Informations forums :
Inscription : juin 2010
Messages : 109
Points : 14
Points : 14
Par défaut Utilisation du PIVOT avec paramètres inconnus

Bonjour,

Je souhaite réaliser la requête suivante :

Code sql :
1
2
3
4
5
6
7
8
9
10
SELECT *
FROM   (SELECT pol_numpol, sor_ident, ccp_libelle, gad_prime_nette
FROM f_polices, f_sit_objet_risque, f_garantie_dyn, t_cdecpt, f_tarif_ass
WHERE gad_ptrsorid = sor_ident
AND sor_ptrpolid = pol_ident
AND sor_datetarif = tas_dateffet
AND tas_codeintercalaire = ccp_code
AND gad_prime_nette IS NOT NULL AND gad_prime_nette != 0
PIVOT XML (SUM(gad_prime_nette) AS prime FOR (ccp_libelle) IN (SELECT DISTINCT ccp_code FROM   t_cdecpt))                                                   
ORDER BY pol_numpol, sor_ident

sans que mon résultat ne soit du XML. Le problème est que je ne peux pas connaitre à l'avance ni le nombre, ni les libellés de mes ccp_code... Comment faire?
justinedr71 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 26/07/2011, 17h09   #2
Expert Confirmé Sénior


 
Avatar de laurentschneider
 
Homme Laurent Schneider
Administrateur de base de données
Inscription : décembre 2005
Messages : 2 925
Détails du profil
Informations personnelles :
Nom : Homme Laurent Schneider
Localisation : Suisse

Informations professionnelles :
Activité : Administrateur de base de données
Secteur : Finance

Informations forums :
Inscription : décembre 2005
Messages : 2 925
Points : 4 547
Points : 4 547
dans sql il est impossible d'avoir un nombre dynamique de colonne.

tu peux tenter du plsql

Code :
1
2
3
4
5
6
7
8
9
10
11
12
var r refcursor
 
declare joblist varchar2(4000);
begin
  SELECT listagg( ''''||job||''' as '||job,',') within GROUP (ORDER BY job) 
    INTO joblist FROM (SELECT DISTINCT job FROM emp);
  open :r FOR 'select * from (select deptno,job from emp) pivot  
    (count(*) for (job) in ('||joblist||'))';
end;
/
 
print r
Code :
1
2
3
4
5
    DEPTNO    ANALYST      CLERK    MANAGER  PRESIDENT   SALESMAN
---------- ---------- ---------- ---------- ---------- ----------
        30          0          1          1          0          4
        20          2          2          1          0          0
        10          0          1          1          1          0
__________________
Mon blog : laurentschneider.com
Mon livre : Advanced Oracle SQL Programming
laurentschneider est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 27/07/2011, 10h25   #3
Candidat au titre de Membre du Club
 
Justine Dreyfus
Inscription : juin 2010
Messages : 109
Détails du profil
Informations personnelles :
Nom : Justine Dreyfus

Informations forums :
Inscription : juin 2010
Messages : 109
Points : 14
Points : 14
Merci beaucoup!!!

Donc avec ma requête, si j'ai bien compris ça donne ça :

Code sql :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
var r refcursor
 
declare joblist varchar2(4000);
 
begin
  SELECT listagg( ''''||ccp_libelle||''' as '||ccp_libelle,',') within GROUP (ORDER BY ccp_libelle)
    INTO joblist FROM (SELECT DISTINCT ccp_libelle FROM t_cdecpt);
  open :r FOR 'select * from (select pol_numpol, sor_ident, ccp_libelle, gad_prime_nette
        from f_polices, f_sit_objet_risque, f_garantie_dyn, t_cdecpt, f_tarif_ass
        where gad_ptrsorid = sor_ident
        and sor_ptrpolid = pol_ident
        and sor_datetarif = tas_dateffet
        and tas_codeintercalaire = ccp_code
        and gad_prime_nette is not null and gad_prime_nette != 0) 
        pivot (count(*) for (ccp_libelle) in ('||joblist||'))';
end;
/
 
print r

il me dit que j'ai un problème de virgule à la ligne 6 lorsque je l'exécute sous TOAD... ???
justinedr71 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 27/07/2011, 10h59   #4
Candidat au titre de Membre du Club
 
Justine Dreyfus
Inscription : juin 2010
Messages : 109
Détails du profil
Informations personnelles :
Nom : Justine Dreyfus

Informations forums :
Inscription : juin 2010
Messages : 109
Points : 14
Points : 14
Même en ne mettant que ce code la :

Code :
1
2
3
4
5
6
7
8
9
10
var r refcursor
declare joblist varchar2(4000);
begin
  SELECT listagg( ''''||ccp_libelle||''' as '||ccp_libelle,',') within GROUP (ORDER BY ccp_libelle) 
    INTO joblist FROM (SELECT DISTINCT ccp_libelle FROM t_cdecpt);
    open :r FOR 'select * from (select ccp_code,ccp_libelle from t_cdecpt) pivot  
    (count(*) for (ccp_libelle) in ('||joblist||'))';
end;
/
print r
J'ai toujours un problème de 'virgule' à la ligne 5... J'ai essayé de placer des virgules à plusieurs endroits... Ça ne résout rien
justinedr71 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 27/07/2011, 11h49   #5
Membre confirmé
 
Avatar de jkofr
 
Homme Jacques
Administrateur de base de données
Inscription : octobre 2006
Messages : 251
Détails du profil
Informations personnelles :
Nom : Homme Jacques
Âge : 43
Localisation : Suisse

Informations professionnelles :
Activité : Administrateur de base de données
Secteur : Agroalimentaire - Agriculture

Informations forums :
Inscription : octobre 2006
Messages : 251
Points : 219
Points : 219
Envoyer un message via MSN à jkofr
Aurai-tu une virgule dans la colonne ccp_libelle ?

Jko
__________________
OCA-OCP 11g, SQL and Performance & Tuning Expert 11g
Data Guard 11g, ASM & Grid Control 11g, Apex
jkofr est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 27/07/2011, 12h29   #6
Candidat au titre de Membre du Club
 
Justine Dreyfus
Inscription : juin 2010
Messages : 109
Détails du profil
Informations personnelles :
Nom : Justine Dreyfus

Informations forums :
Inscription : juin 2010
Messages : 109
Points : 14
Points : 14
Non non, juste "ccp_libelle"
justinedr71 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 27/07/2011, 12h44   #7
Membre confirmé
 
Avatar de jkofr
 
Homme Jacques
Administrateur de base de données
Inscription : octobre 2006
Messages : 251
Détails du profil
Informations personnelles :
Nom : Homme Jacques
Âge : 43
Localisation : Suisse

Informations professionnelles :
Activité : Administrateur de base de données
Secteur : Agroalimentaire - Agriculture

Informations forums :
Inscription : octobre 2006
Messages : 251
Points : 219
Points : 219
Envoyer un message via MSN à jkofr
Hello,

Je parlais du contenu de ta colonne ccp_libelle.

Démonstration:

Code :
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
34
35
36
37
38
39
40
41
42
43
44
 
var r refcursor
declare joblist varchar2(4000);
begin
  SELECT listagg( ''''||job||''' as '||job,',') within GROUP (ORDER BY job) 
    INTO joblist FROM (SELECT DISTINCT job FROM scott.emp);
  open :r FOR 'select * from (select deptno,job from scott.emp) pivot  
    (count(*) for (job) in ('||joblist||'))';
end;
/
print r
 
DEPTNO                 ANALYST                CLERK                  MANAGER                PRESIDENT              SALESMAN               
---------------------- ---------------------- ---------------------- ---------------------- ---------------------- ---------------------- 
30                     0                      1                      1                      0                      4                      
20                     2                      2                      1                      0                      0                      
10                     0                      1                      1                      1                      0                      
 
 
SELECT job FROM scott.emp WHERE job = 'ANALYST';
JOB       
---------
ANALYST
 
UPDATE scott.emp SET job = 'ANALY,ST' WHERE job = 'ANALYST'
 
SELECT job FROM scott.emp WHERE job = 'ANALYST';
---------
ANALY,ST
 
var r refcursor
declare joblist varchar2(4000);
begin
  SELECT listagg( ''''||job||''' as '||job,',') within GROUP (ORDER BY job) 
    INTO joblist FROM (SELECT DISTINCT job FROM scott.emp);
  open :r FOR 'select * from (select deptno,job from scott.emp) pivot  
    (count(*) for (job) in ('||joblist||'))';
end;
/
print r
 
Error report:
ORA-56901: non-constant expression IS NOT allowed FOR pivot|unpivot VALUES
ORA-06512: at line 5
jko
__________________
OCA-OCP 11g, SQL and Performance & Tuning Expert 11g
Data Guard 11g, ASM & Grid Control 11g, Apex
jkofr est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 27/07/2011, 12h49   #8
Candidat au titre de Membre du Club
 
Justine Dreyfus
Inscription : juin 2010
Messages : 109
Détails du profil
Informations personnelles :
Nom : Justine Dreyfus

Informations forums :
Inscription : juin 2010
Messages : 109
Points : 14
Points : 14
J'ai pas mal de possibilités pour le contenu de ma colonne ccp_libelle.

Globalement, j'ai les caractères de [0..9a..zA..Z], mais aussi [.,:-_<>]...

Êtes-vous sûr que le problème vient du contenu de ma chaine? Comment y remédier?
justinedr71 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 27/07/2011, 13h12   #9
Membre confirmé
 
Avatar de jkofr
 
Homme Jacques
Administrateur de base de données
Inscription : octobre 2006
Messages : 251
Détails du profil
Informations personnelles :
Nom : Homme Jacques
Âge : 43
Localisation : Suisse

Informations professionnelles :
Activité : Administrateur de base de données
Secteur : Agroalimentaire - Agriculture

Informations forums :
Inscription : octobre 2006
Messages : 251
Points : 219
Points : 219
Envoyer un message via MSN à jkofr
Pour les ',' tu peux rajouter ca

Code :
1
2
3
4
5
6
7
8
9
10
11
12

var r refcursor
declare joblist varchar2(4000);
begin
  SELECT listagg( ''''||job||''' as '||replace (job,',',''),',') within GROUP (ORDER BY job) 
    INTO joblist FROM (SELECT DISTINCT  job FROM scott.emp);
  open :r FOR 'select * from (select deptno,job from scott.emp) pivot  
    (count(*) for (job) in ('||joblist||'))';
end;
/
print r
__________________
OCA-OCP 11g, SQL and Performance & Tuning Expert 11g
Data Guard 11g, ASM & Grid Control 11g, Apex
jkofr est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 27/07/2011, 15h34   #10
Candidat au titre de Membre du Club
 
Justine Dreyfus
Inscription : juin 2010
Messages : 109
Détails du profil
Informations personnelles :
Nom : Justine Dreyfus

Informations forums :
Inscription : juin 2010
Messages : 109
Points : 14
Points : 14
J'ai utilisé votre dernière requête.

Voici ma requête de base :

Code sql :
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
 
SELECT *
FROM (SELECT pol_numpol, sor_ident, ver_compteur, ver_libelle, ver_dateoperation, sor_datedebut, sor_datefin, sor_datedeb_fact, sor_datefin_fact, pol_datresil, pol_datechpro, ccp_libelle,
gad_prime_nette, pas_nb_jours_prorata, pri_type_risque, bpp_type, bpp_pp_sexe, bpp_pp_nais_date, bpp_pp_nais_pay_code, mar_libelle, bpp_pp_nb_enfants, bpp_pp_handicape, in_code_etabl,
in_type
FROM f_polices, f_version_police, f_mouvement, f_sit_objet_risque, f_garantie_dyn, f_produitass, f_prd_risque, f_sit_assure, f_p_c_assure, f_p_personne, t_sit_maritale, f_intermediaire,
f_tarif_ass, t_intercalaire, f_tarif_gar, t_cdecpt
WHERE gad_ptrsorid = sor_ident
AND sor_ptrpolid = pol_ident
AND sor_ident = mvt_ptrsuivantid
AND mvt_ptrverid = ver_ident
AND pol_ptrpasid = pas_ident
AND pas_ident = pri_ptrpasident
AND sor_ident = sar_ptrsorid (+)
AND sar_ptrassid = bpass_ident (+)
AND bpass_ptrbppident = bpp_ident (+)
AND bpp_ptrmarid = mar_ident (+)
AND pol_ptrinid = in_ident (+)
AND sor_datetarif = tas_dateffet
AND tas_codeintercalaire = ta_code
AND ta_cle = tga_cletarif
AND tga_code_compta = ccp_code
AND gad_code = tga_code
AND tga_genre = 'GAR'
AND gad_prime_nette IS NOT NULL AND gad_prime_nette != 0
AND (pol_datresil IS NULL OR pol_datresil >= (sysdate - to_yminterval('03-00')))
AND ((sor_datedebut != sor_datefin) OR (sor_datefin IS NULL AND sor_datedebut != pol_datresil) 
OR (sor_datefin IS NULL AND pol_datresil IS NULL AND sor_datedebut != pol_datechpro))
GROUP BY pol_numpol, pol_datresil, pol_datechpro, ver_compteur, ver_libelle, ver_dateoperation, sor_ident, sor_datedebut, sor_datefin, sor_datedeb_fact, sor_datefin_fact, gad_prime_nette, ccp_libelle, pas_nb_jours_prorata, pri_type_risque, bpp_type, bpp_pp_sexe, bpp_pp_nais_date, bpp_pp_nais_pay_code, mar_libelle, bpp_pp_nb_enfants, bpp_pp_handicape, in_code_etabl, in_type )
PIVOT  (SUM(gad_prime_nette) AS prime FOR (ccp_libelle) IN ('Vol' AS Vol, 'Incendie' AS Incendie))
ORDER BY pol_numpol ASC, ver_compteur ASC, sor_ident ASC

Avec pour exemple les deux libellés Vol et Incendie (qui sont parmis les nombreux libellés existants). A noter que je ne connais pas ni le nombre ni le nom des libellés...

Cette requête fonctionne parfaitement, maintenant, la voici concaténée dans votre requête à vous :

Code sql :
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
34
35
36
37
38
39
 
var r refcursor
declare joblist varchar2(4000);
begin
  SELECT listagg( ''''||ccp_libelle||''' as '||REPLACE (ccp_libelle,',',''),',') within GROUP (ORDER BY ccp_libelle) 
    INTO joblist FROM (SELECT DISTINCT ccp_libelle FROM t_cdecpt);
 
      open :r FOR 'SELECT *
FROM (select pol_numpol, sor_ident, ver_compteur, ver_libelle, ver_dateoperation, sor_datedebut, sor_datefin, sor_datedeb_fact, sor_datefin_fact, pol_datresil, pol_datechpro, ccp_libelle,
gad_prime_nette, pas_nb_jours_prorata, pri_type_risque, bpp_type, bpp_pp_sexe, bpp_pp_nais_date, bpp_pp_nais_pay_code, mar_libelle, bpp_pp_nb_enfants, bpp_pp_handicape, in_code_etabl,
in_type
from f_polices, f_version_police, f_mouvement, f_sit_objet_risque, f_garantie_dyn, f_produitass, f_prd_risque, f_sit_assure, f_p_c_assure, f_p_personne, t_sit_maritale, f_intermediaire,
f_tarif_ass, t_intercalaire, f_tarif_gar, t_cdecpt
where gad_ptrsorid = sor_ident
and sor_ptrpolid = pol_ident
and sor_ident = mvt_ptrsuivantid
and mvt_ptrverid = ver_ident
and pol_ptrpasid = pas_ident
and pas_ident = pri_ptrpasident
and sor_ident = sar_ptrsorid (+)
and sar_ptrassid = bpass_ident (+)
and bpass_ptrbppident = bpp_ident (+)
and bpp_ptrmarid = mar_ident (+)
and pol_ptrinid = in_ident (+)
and sor_datetarif = tas_dateffet
and tas_codeintercalaire = ta_code
and ta_cle = tga_cletarif
and tga_code_compta = ccp_code
and gad_code = tga_code
and tga_genre = 'GAR'
and gad_prime_nette is not null and gad_prime_nette != 0
and (pol_datresil is null or pol_datresil >= (sysdate - to_yminterval('03-00')))
and ((sor_datedebut != sor_datefin) or (sor_datefin is null and sor_datedebut != pol_datresil) 
or (sor_datefin is null and pol_datresil is null and sor_datedebut != pol_datechpro))
group by pol_numpol, pol_datresil, pol_datechpro, ver_compteur, ver_libelle, ver_dateoperation, sor_ident, sor_datedebut, sor_datefin, sor_datedeb_fact, sor_datefin_fact, gad_prime_nette, ccp_libelle, pas_nb_jours_prorata, pri_type_risque, bpp_type, bpp_pp_sexe, bpp_pp_nais_date, bpp_pp_nais_pay_code, mar_libelle, bpp_pp_nb_enfants, bpp_pp_handicape, in_code_etabl, in_type ) 
pivot (sum(gad_prime_nette) for (ccp_libelle) in ('||joblist||'))';    
end;
/
print r

Ici elle ne fonctionne pas. Peut-être est-ce du aux ' ' de chaque côté de 'GAR' ou encore '03-00' ou même '||joblist||'...

J'obtiend l'erreur suivante :

ORA-06550: Ligne 28, colonne 18 :
PLS-00103: Symbole "GAR" rencontré à la place d'un des symboles suivants :

* & = - + ; < / > at in is mod remainder not rem
<exposant (**)> <> or != or ~= >= <= <> and or like like2
like4 likec between using || multiset member submultiset
justinedr71 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 27/07/2011, 16h34   #11
Membre confirmé
 
Avatar de jkofr
 
Homme Jacques
Administrateur de base de données
Inscription : octobre 2006
Messages : 251
Détails du profil
Informations personnelles :
Nom : Homme Jacques
Âge : 43
Localisation : Suisse

Informations professionnelles :
Activité : Administrateur de base de données
Secteur : Agroalimentaire - Agriculture

Informations forums :
Inscription : octobre 2006
Messages : 251
Points : 219
Points : 219
Envoyer un message via MSN à jkofr
Et la?

Code :
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
34
35
36
37
38
39
40
 
 
var r refcursor
declare joblist varchar2(4000);
begin
  SELECT listagg( ''''||ccp_libelle||''' as '||REPLACE (ccp_libelle,',',''),',') within GROUP (ORDER BY ccp_libelle) 
    INTO joblist FROM (SELECT DISTINCT ccp_libelle FROM t_cdecpt);
 
      open :r FOR 'SELECT *
FROM (select pol_numpol, sor_ident, ver_compteur, ver_libelle, ver_dateoperation, sor_datedebut, sor_datefin, sor_datedeb_fact, sor_datefin_fact, pol_datresil, pol_datechpro, ccp_libelle,
gad_prime_nette, pas_nb_jours_prorata, pri_type_risque, bpp_type, bpp_pp_sexe, bpp_pp_nais_date, bpp_pp_nais_pay_code, mar_libelle, bpp_pp_nb_enfants, bpp_pp_handicape, in_code_etabl,
in_type
from f_polices, f_version_police, f_mouvement, f_sit_objet_risque, f_garantie_dyn, f_produitass, f_prd_risque, f_sit_assure, f_p_c_assure, f_p_personne, t_sit_maritale, f_intermediaire,
f_tarif_ass, t_intercalaire, f_tarif_gar, t_cdecpt
where gad_ptrsorid = sor_ident
and sor_ptrpolid = pol_ident
and sor_ident = mvt_ptrsuivantid
and mvt_ptrverid = ver_ident
and pol_ptrpasid = pas_ident
and pas_ident = pri_ptrpasident
and sor_ident = sar_ptrsorid (+)
and sar_ptrassid = bpass_ident (+)
and bpass_ptrbppident = bpp_ident (+)
and bpp_ptrmarid = mar_ident (+)
and pol_ptrinid = in_ident (+)
and sor_datetarif = tas_dateffet
and tas_codeintercalaire = ta_code
and ta_cle = tga_cletarif
and tga_code_compta = ccp_code
and gad_code = tga_code
and tga_genre = ''GAR''
and gad_prime_nette is not null and gad_prime_nette != 0
and (pol_datresil is null or pol_datresil >= (sysdate - to_yminterval(''03-00'')))
and ((sor_datedebut != sor_datefin) or (sor_datefin is null and sor_datedebut != pol_datresil) 
or (sor_datefin is null and pol_datresil is null and sor_datedebut != pol_datechpro))
group by pol_numpol, pol_datresil, pol_datechpro, ver_compteur, ver_libelle, ver_dateoperation, sor_ident, sor_datedebut, sor_datefin, sor_datedeb_fact, sor_datefin_fact, gad_prime_nette, ccp_libelle, pas_nb_jours_prorata, pri_type_risque, bpp_type, bpp_pp_sexe, bpp_pp_nais_date, bpp_pp_nais_pay_code, mar_libelle, bpp_pp_nb_enfants, bpp_pp_handicape, in_code_etabl, in_type ) 
pivot (sum(gad_prime_nette) for (ccp_libelle) in ('||joblist||'))';    
end;
/
print r
__________________
OCA-OCP 11g, SQL and Performance & Tuning Expert 11g
Data Guard 11g, ASM & Grid Control 11g, Apex
jkofr est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 27/07/2011, 16h38   #12
Candidat au titre de Membre du Club
 
Justine Dreyfus
Inscription : juin 2010
Messages : 109
Détails du profil
Informations personnelles :
Nom : Justine Dreyfus

Informations forums :
Inscription : juin 2010
Messages : 109
Points : 14
Points : 14
ORA-00917: virgule absente
ORA-06512: à ligne 6

... Il recommence avec cette histoire de virgule...
justinedr71 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 27/07/2011, 16h40   #13
Candidat au titre de Membre du Club
 
Justine Dreyfus
Inscription : juin 2010
Messages : 109
Détails du profil
Informations personnelles :
Nom : Justine Dreyfus

Informations forums :
Inscription : juin 2010
Messages : 109
Points : 14
Points : 14
Tu as bien mis deux quotes (') à la suite, ou bien ce sont des guillemets (")?
justinedr71 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 27/07/2011, 16h42   #14
Membre confirmé
 
Avatar de jkofr
 
Homme Jacques
Administrateur de base de données
Inscription : octobre 2006
Messages : 251
Détails du profil
Informations personnelles :
Nom : Homme Jacques
Âge : 43
Localisation : Suisse

Informations professionnelles :
Activité : Administrateur de base de données
Secteur : Agroalimentaire - Agriculture

Informations forums :
Inscription : octobre 2006
Messages : 251
Points : 219
Points : 219
Envoyer un message via MSN à jkofr
Code :
1
2
3
4
and tga_genre = ''GAR''
and gad_prime_nette is not null and gad_prime_nette != 0
and (pol_datresil is null or pol_datresil >= (sysdate - to_yminterval(''03-00'')))
__________________
OCA-OCP 11g, SQL and Performance & Tuning Expert 11g
Data Guard 11g, ASM & Grid Control 11g, Apex
jkofr est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 27/07/2011, 16h48   #15
Candidat au titre de Membre du Club
 
Justine Dreyfus
Inscription : juin 2010
Messages : 109
Détails du profil
Informations personnelles :
Nom : Justine Dreyfus

Informations forums :
Inscription : juin 2010
Messages : 109
Points : 14
Points : 14
Ok! C'est bien ce que j'ai...
Pourquoi ce problème de virgule revient...?
justinedr71 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 27/07/2011, 16h49   #16
Membre confirmé
 
Avatar de jkofr
 
Homme Jacques
Administrateur de base de données
Inscription : octobre 2006
Messages : 251
Détails du profil
Informations personnelles :
Nom : Homme Jacques
Âge : 43
Localisation : Suisse

Informations professionnelles :
Activité : Administrateur de base de données
Secteur : Agroalimentaire - Agriculture

Informations forums :
Inscription : octobre 2006
Messages : 251
Points : 219
Points : 219
Envoyer un message via MSN à jkofr
Tiens, essaie ca!

En fait le replace est inutile!

Code :
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
34
35
36
37
38
39
40
 
 
var r refcursor
declare joblist varchar2(4000);
begin
  SELECT listagg( ''''||ccp_libelle||''' as "'||ccp_libelle||'"',',') within GROUP (ORDER BY ccp_libelle) 
    INTO joblist FROM (SELECT DISTINCT ccp_libelle FROM t_cdecpt);
 
      open :r FOR 'SELECT *
FROM (select pol_numpol, sor_ident, ver_compteur, ver_libelle, ver_dateoperation, sor_datedebut, sor_datefin, sor_datedeb_fact, sor_datefin_fact, pol_datresil, pol_datechpro, ccp_libelle,
gad_prime_nette, pas_nb_jours_prorata, pri_type_risque, bpp_type, bpp_pp_sexe, bpp_pp_nais_date, bpp_pp_nais_pay_code, mar_libelle, bpp_pp_nb_enfants, bpp_pp_handicape, in_code_etabl,
in_type
from f_polices, f_version_police, f_mouvement, f_sit_objet_risque, f_garantie_dyn, f_produitass, f_prd_risque, f_sit_assure, f_p_c_assure, f_p_personne, t_sit_maritale, f_intermediaire,
f_tarif_ass, t_intercalaire, f_tarif_gar, t_cdecpt
where gad_ptrsorid = sor_ident
and sor_ptrpolid = pol_ident
and sor_ident = mvt_ptrsuivantid
and mvt_ptrverid = ver_ident
and pol_ptrpasid = pas_ident
and pas_ident = pri_ptrpasident
and sor_ident = sar_ptrsorid (+)
and sar_ptrassid = bpass_ident (+)
and bpass_ptrbppident = bpp_ident (+)
and bpp_ptrmarid = mar_ident (+)
and pol_ptrinid = in_ident (+)
and sor_datetarif = tas_dateffet
and tas_codeintercalaire = ta_code
and ta_cle = tga_cletarif
and tga_code_compta = ccp_code
and gad_code = tga_code
and tga_genre = ''GAR''
and gad_prime_nette is not null and gad_prime_nette != 0
and (pol_datresil is null or pol_datresil >= (sysdate - to_yminterval(''03-00'')))
and ((sor_datedebut != sor_datefin) or (sor_datefin is null and sor_datedebut != pol_datresil) 
or (sor_datefin is null and pol_datresil is null and sor_datedebut != pol_datechpro))
group by pol_numpol, pol_datresil, pol_datechpro, ver_compteur, ver_libelle, ver_dateoperation, sor_ident, sor_datedebut, sor_datefin, sor_datedeb_fact, sor_datefin_fact, gad_prime_nette, ccp_libelle, pas_nb_jours_prorata, pri_type_risque, bpp_type, bpp_pp_sexe, bpp_pp_nais_date, bpp_pp_nais_pay_code, mar_libelle, bpp_pp_nb_enfants, bpp_pp_handicape, in_code_etabl, in_type ) 
pivot (sum(gad_prime_nette) for (ccp_libelle) in ('||joblist||'))';    
end;
/
print r
jko
__________________
OCA-OCP 11g, SQL and Performance & Tuning Expert 11g
Data Guard 11g, ASM & Grid Control 11g, Apex
jkofr est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 27/07/2011, 16h50   #17
Candidat au titre de Membre du Club
 
Justine Dreyfus
Inscription : juin 2010
Messages : 109
Détails du profil
Informations personnelles :
Nom : Justine Dreyfus

Informations forums :
Inscription : juin 2010
Messages : 109
Points : 14
Points : 14
Ah! On change d'erreur :

ORA-00972: l'identificateur est trop long
ORA-06512: à ligne 6

Et il me surligne la ligne :
declare joblist varchar2(4000);

...
justinedr71 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 27/07/2011, 16h58   #18
Membre confirmé
 
Avatar de jkofr
 
Homme Jacques
Administrateur de base de données
Inscription : octobre 2006
Messages : 251
Détails du profil
Informations personnelles :
Nom : Homme Jacques
Âge : 43
Localisation : Suisse

Informations professionnelles :
Activité : Administrateur de base de données
Secteur : Agroalimentaire - Agriculture

Informations forums :
Inscription : octobre 2006
Messages : 251
Points : 219
Points : 219
Envoyer un message via MSN à jkofr
Bien,

Voici un exemple pour que tu comprenne

Code :
1
2
3
4
5
6
7
 
SELECT 1 AS MonLibelledecolonnepourtesterlalongeurmax FROM dual;
 
SQL Error: ORA-00972: identifier IS too long
00972. 00000 -  "identifier is too long"
*Cause:    An identifier WITH more than 30 characters was specified.
*Action:   Specify at most 30 characters.
En bref, ton libelle de colonne ne peut pas faire plus de 30 caractères.

Soit tu fais un substr (ccp_libelle,1,30), soit tu n'as pas de solution facile...

Bon courage
jko
__________________
OCA-OCP 11g, SQL and Performance & Tuning Expert 11g
Data Guard 11g, ASM & Grid Control 11g, Apex
jkofr est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 27/07/2011, 17h02   #19
Candidat au titre de Membre du Club
 
Justine Dreyfus
Inscription : juin 2010
Messages : 109
Détails du profil
Informations personnelles :
Nom : Justine Dreyfus

Informations forums :
Inscription : juin 2010
Messages : 109
Points : 14
Points : 14
Super!
Merci beaucoup!!!

Voici ma requete finale, qui fonctionne :
Code sql :
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
34
35
36
37
38
39
 
var r refcursor
declare joblist varchar2(4000);
begin
  SELECT listagg( ''''||libelle||''' as "'||libelle||'"',',') within GROUP (ORDER BY libelle) 
    INTO joblist FROM (SELECT DISTINCT substr(ccp_libelle,1,30) AS libelle FROM t_cdecpt);
 
      open :r FOR 'SELECT *
FROM (select pol_numpol, sor_ident, ver_compteur, ver_libelle, ver_dateoperation, sor_datedebut, sor_datefin, sor_datedeb_fact, sor_datefin_fact, pol_datresil, pol_datechpro, ccp_libelle,
gad_prime_nette, pas_nb_jours_prorata, pri_type_risque, bpp_type, bpp_pp_sexe, bpp_pp_nais_date, bpp_pp_nais_pay_code, mar_libelle, bpp_pp_nb_enfants, bpp_pp_handicape, in_code_etabl,
in_type
from f_polices, f_version_police, f_mouvement, f_sit_objet_risque, f_garantie_dyn, f_produitass, f_prd_risque, f_sit_assure, f_p_c_assure, f_p_personne, t_sit_maritale, f_intermediaire,
f_tarif_ass, t_intercalaire, f_tarif_gar, t_cdecpt
where gad_ptrsorid = sor_ident
and sor_ptrpolid = pol_ident
and sor_ident = mvt_ptrsuivantid
and mvt_ptrverid = ver_ident
and pol_ptrpasid = pas_ident
and pas_ident = pri_ptrpasident
and sor_ident = sar_ptrsorid (+)
and sar_ptrassid = bpass_ident (+)
and bpass_ptrbppident = bpp_ident (+)
and bpp_ptrmarid = mar_ident (+)
and pol_ptrinid = in_ident (+)
and sor_datetarif = tas_dateffet
and tas_codeintercalaire = ta_code
and ta_cle = tga_cletarif
and tga_code_compta = ccp_code
and gad_code = tga_code
and tga_genre = ''GAR''
and gad_prime_nette is not null and gad_prime_nette != 0
and (pol_datresil is null or pol_datresil >= (sysdate - to_yminterval(''03-00'')))
and ((sor_datedebut != sor_datefin) or (sor_datefin is null and sor_datedebut != pol_datresil) 
or (sor_datefin is null and pol_datresil is null and sor_datedebut != pol_datechpro))
group by pol_numpol, pol_datresil, pol_datechpro, ver_compteur, ver_libelle, ver_dateoperation, sor_ident, sor_datedebut, sor_datefin, sor_datedeb_fact, sor_datefin_fact, gad_prime_nette, ccp_libelle, pas_nb_jours_prorata, pri_type_risque, bpp_type, bpp_pp_sexe, bpp_pp_nais_date, bpp_pp_nais_pay_code, mar_libelle, bpp_pp_nb_enfants, bpp_pp_handicape, in_code_etabl, in_type ) 
pivot (sum(gad_prime_nette) for (ccp_libelle) in ('||joblist||'))';    
end;
/
print r
justinedr71 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 27/07/2011, 17h19   #20
Candidat au titre de Membre du Club
 
Justine Dreyfus
Inscription : juin 2010
Messages : 109
Détails du profil
Informations personnelles :
Nom : Justine Dreyfus

Informations forums :
Inscription : juin 2010
Messages : 109
Points : 14
Points : 14
Ah non une dernière chose. Comment puis-je faire pour que même les garanties ayant un libelle > 30 soient prises en compte.

Ex, j'ai une garantie avec un libelle 'Protection juiridique automobile', 2 caractères de trop!!! Seulement, il me met bien le champ 'Protection juiridique automobi' mais du coup il ne prend pas en compte les valeurs des primes associées à cette garantie dans ma table, ce que je pense normale, car dans ma table ces primes sont associées à 'Protection juiridique automobile' et non 'Protection juiridique automobi'... Comment faire pour palier à cela.

Je pensais aux expressions régulières avec '*' mais je ne sais pas comment le formuler...

Merci pour vos réponses
justinedr71 est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse Proposer ce sujet en actualité
Outils de la discussion



Fuseau horaire GMT +2. Il est actuellement 07h45.


 
 
 
 
Partenaires

Hébergement Web