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

Langage SQL Discussion :

Simplifier requête SQL


Sujet :

Langage SQL

  1. #1
    Membre actif
    Homme Profil pro
    Étudiant
    Inscrit en
    Avril 2012
    Messages
    538
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Avril 2012
    Messages : 538
    Points : 262
    Points
    262
    Par défaut Simplifier requête SQL
    Bonjour,

    Je suis débutant en SQL et je reprend le travail d'une autre personne qui à développé cette requête :

    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
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    SELECT
        pat.nom NOM,
        pat.sexe SEXE,
        pat.nip ipp,
        substr(datenais, 1, 4) ANNEE_NAISSANCE,
        substr(datenais, 5, 2) MOIS_NAISSANCE,
        substr(datenais, 7, 2) JOUR_NAISSANCE,
        decode(pat.PATRONYME, null, pat.NOM, '', pat.NOM, pat.PATRONYME) NOM_NAISSANCE,
        pat.prenom PRENOM,
        infpat.NUMSECU NSS,
        decode(infpat.NUMSECU, null, '', '', '', substr(infpat.NUMSECU, 6, 2)) DEP_NAISSANCE,
        decode(infpat.NUMSECU, null, '', '', '', substr(infpat.NUMSECU, 8, 3)) VILLE_NAISSANCE,
        decode(srv.nietablis, 1, 'MZ', 3, 'MZ', 4, 'MZ', 5, 'MZ', 8, 'MZ', 9, 'MZ', 10, 'TH', 11, 'TH', 12, 'TH', 13, 'TH', 14, 'TH', 15, 'TH', 16, 'TH', 17, 'TH', 18, 'TH', 19, 'TH', 20, 'MZ', 21, 'TH', 22, 'MZ', 23, 'TH', 25, 'TH', 24, 'MZ', 'Autre') etb,
        (SELECT BM_REP_S.CODE    FROM BM_TH_S, BM_LIB_S, BM_REP_S, C_QUESTION WHERE BM_TH_S.NITH = BM_LIB_S.NITH AND BM_LIB_S.NILIB = BM_REP_S.NILIB AND BM_LIB_S.NIQ = C_QUESTION.NIQ AND C_QUESTION.NIQ = 4993 AND BM_TH_S.nith = th.nith) PatientNiveauEtudes049, 
        (SELECT BM_REP_S.CODE    FROM BM_TH_S, BM_LIB_S, BM_REP_S, C_QUESTION WHERE BM_TH_S.NITH = BM_LIB_S.NITH AND BM_LIB_S.NILIB = BM_REP_S.NILIB AND BM_LIB_S.NIQ = C_QUESTION.NIQ AND C_QUESTION.NIQ = 5023 AND BM_TH_S.nith = th.nith) PatientSituationGeo050,
        (SELECT BM_REP_S.CODE    FROM BM_TH_S, BM_LIB_S, BM_REP_S, C_QUESTION WHERE BM_TH_S.NITH = BM_LIB_S.NITH AND BM_LIB_S.NILIB = BM_REP_S.NILIB AND BM_LIB_S.NIQ = C_QUESTION.NIQ AND C_QUESTION.NIQ = 4988 AND BM_TH_S.nith = th.nith) PAtientEnvoyePar051,
        (SELECT BM_REP_S.CODE    FROM BM_TH_S, BM_LIB_S, BM_REP_S, C_QUESTION WHERE BM_TH_S.NITH = BM_LIB_S.NITH AND BM_LIB_S.NILIB = BM_REP_S.NILIB AND BM_LIB_S.NIQ = C_QUESTION.NIQ AND C_QUESTION.NIQ = 6025 AND BM_TH_S.nith = th.nith) DclEtatDemence052,
        (SELECT BM_REP_S.REPONSE FROM BM_TH_S, BM_LIB_S, BM_REP_S, C_QUESTION WHERE BM_TH_S.NITH = BM_LIB_S.NITH AND BM_LIB_S.NILIB = BM_REP_S.NILIB AND BM_LIB_S.NIQ = C_QUESTION.NIQ AND C_QUESTION.NIQ = 6031 AND BM_TH_S.nith = th.nith) DclAnneePremierDiag053,
        (SELECT BM_REP_S.CODE    FROM BM_TH_S, BM_LIB_S, BM_REP_S, C_QUESTION WHERE BM_TH_S.NITH = BM_LIB_S.NITH AND BM_LIB_S.NILIB = BM_REP_S.NILIB AND BM_LIB_S.NIQ = C_QUESTION.NIQ AND C_QUESTION.NIQ = 6035 AND BM_TH_S.nith = th.nith) DclDiagnostic054,
        (SELECT BM_REP_S.CODE    FROM BM_TH_S, BM_LIB_S, BM_REP_S, C_QUESTION WHERE BM_TH_S.NITH = BM_LIB_S.NITH AND BM_LIB_S.NILIB = BM_REP_S.NILIB AND BM_LIB_S.NIQ = C_QUESTION.NIQ AND C_QUESTION.NIQ = 6026 AND BM_TH_S.nith = th.nith) DclTypeActe055,
        (SELECT substr(BM_REP_S.REPONSE,0,2)||substr(BM_REP_S.REPONSE,4,2)||substr(BM_REP_S.REPONSE,7,4) FROM BM_TH_S, BM_LIB_S, BM_REP_S, C_QUESTION WHERE BM_TH_S.NITH = BM_LIB_S.NITH AND BM_LIB_S.NILIB = BM_REP_S.NILIB AND BM_LIB_S.NIQ = C_QUESTION.NIQ AND C_QUESTION.NIQ = 4964 and BM_TH_S.nith = th.nith) DclDateActe056, 
        decode((SELECT BM_REP_S.REPONSE FROM BM_TH_S, BM_LIB_S, BM_REP_S, C_QUESTION WHERE BM_TH_S.NITH = BM_LIB_S.NITH AND BM_LIB_S.NILIB = BM_REP_S.NILIB AND BM_LIB_S.NIQ = C_QUESTION.NIQ AND C_QUESTION.NIQ = 6567 and BM_TH_S.nith = th.nith ),null,'',(SELECT BM_REP_S.REPONSE FROM BM_TH_S, BM_LIB_S, BM_REP_S, C_QUESTION WHERE BM_TH_S.NITH = BM_LIB_S.NITH AND BM_LIB_S.NILIB = BM_REP_S.NILIB AND BM_LIB_S.NIQ = C_QUESTION.NIQ AND C_QUESTION.NIQ = 6567 and BM_TH_S.nith = th.nith )) DclMMSE057,
        (SELECT BM_REP_S.REPONSE FROM BM_TH_S, BM_LIB_S, BM_REP_S, C_QUESTION WHERE BM_TH_S.NITH = BM_LIB_S.NITH AND BM_LIB_S.NILIB = BM_REP_S.NILIB AND BM_LIB_S.NIQ = C_QUESTION.NIQ AND C_QUESTION.NIQ = 5015 and BM_TH_S.nith = th.nith ) DcllADLDetection058,
        (SELECT BM_REP_S.REPONSE FROM BM_TH_S, BM_LIB_S, BM_REP_S, C_QUESTION WHERE BM_TH_S.NITH = BM_LIB_S.NITH AND BM_LIB_S.NILIB = BM_REP_S.NILIB AND BM_LIB_S.NIQ = C_QUESTION.NIQ AND C_QUESTION.NIQ = 5014 and BM_TH_S.nith = th.nith ) DcllADLConsequence059, 
        (SELECT BM_REP_S.CODE FROM BM_TH_S, BM_LIB_S, BM_REP_S, C_QUESTION WHERE BM_TH_S.NITH = BM_LIB_S.NITH AND BM_LIB_S.NILIB = BM_REP_S.NILIB AND BM_LIB_S.NIQ = C_QUESTION.NIQ AND C_QUESTION.NIQ = 4992 and BM_TH_S.nith = th.nith ) DclModeDeVie060,
        decode((SELECT BM_REP_S.CODE FROM BM_TH_S, BM_LIB_S, BM_REP_S, C_QUESTION WHERE BM_TH_S.NITH = BM_LIB_S.NITH AND BM_LIB_S.NILIB = BM_REP_S.NILIB AND BM_LIB_S.NIQ = C_QUESTION.NIQ AND C_QUESTION.NIQ = 4956 and BM_TH_S.nith = th.nith ),'','SI',(SELECT BM_REP_S.CODE FROM BM_TH_S, BM_LIB_S, BM_REP_S, C_QUESTION WHERE BM_TH_S.NITH = BM_LIB_S.NITH AND BM_LIB_S.NILIB = BM_REP_S.NILIB AND BM_LIB_S.NIQ = C_QUESTION.NIQ AND C_QUESTION.NIQ = 4956 and BM_TH_S.nith = th.nith )) DclAPA061,
        decode((SELECT BM_REP_S.CODE FROM BM_TH_S, BM_LIB_S, BM_REP_S, C_QUESTION WHERE BM_TH_S.NITH = BM_LIB_S.NITH AND BM_LIB_S.NILIB = BM_REP_S.NILIB AND BM_LIB_S.NIQ = C_QUESTION.NIQ AND C_QUESTION.NIQ = 4955 and BM_TH_S.nith = th.nith ),'','SI',(SELECT BM_REP_S.CODE FROM BM_TH_S, BM_LIB_S, BM_REP_S, C_QUESTION WHERE BM_TH_S.NITH = BM_LIB_S.NITH AND BM_LIB_S.NILIB = BM_REP_S.NILIB AND BM_LIB_S.NIQ = C_QUESTION.NIQ AND C_QUESTION.NIQ = 4955 and BM_TH_S.nith = th.nith )) DclALD062,
        decode((SELECT decode(BM_REP_S.REPONSE,'aucune','AUCUNE','tutelle','TUTELLE','curatelle','CURATELLE','sans information','SI') FROM BM_TH_S, BM_LIB_S, BM_REP_S, C_QUESTION WHERE BM_TH_S.NITH = BM_LIB_S.NITH AND BM_LIB_S.NILIB = BM_REP_S.NILIB AND BM_LIB_S.NIQ = C_QUESTION.NIQ AND C_QUESTION.NIQ = 4990 and BM_TH_S.nith = th.nith ),'','SI',(SELECT decode(BM_REP_S.REPONSE,'aucune','AUCUNE','tutelle','TUTELLE','curatelle','CURATELLE','sans information','SI') FROM BM_TH_S, BM_LIB_S, BM_REP_S, C_QUESTION WHERE BM_TH_S.NITH = BM_LIB_S.NITH AND BM_LIB_S.NILIB = BM_REP_S.NILIB AND BM_LIB_S.NIQ = C_QUESTION.NIQ AND C_QUESTION.NIQ = 4990 and BM_TH_S.nith = th.nith )) DclMesureProtection063,
        (SELECT BM_REP_S.CODE FROM BM_TH_S, BM_LIB_S, BM_REP_S, C_QUESTION WHERE BM_TH_S.NITH = BM_LIB_S.NITH AND BM_LIB_S.NILIB = BM_REP_S.NILIB AND BM_LIB_S.NIQ = C_QUESTION.NIQ AND C_QUESTION.NIQ = 6033 and BM_TH_S.nith = th.nith ) DclFinSuivi064,
        (SELECT substr(BM_REP_S.REPONSE,0,2)||substr(BM_REP_S.REPONSE,4,2)||substr(BM_REP_S.REPONSE,7,4) FROM BM_TH_S, BM_LIB_S, BM_REP_S, C_QUESTION WHERE BM_TH_S.NITH = BM_LIB_S.NITH AND BM_LIB_S.NILIB = BM_REP_S.NILIB AND BM_LIB_S.NIQ = C_QUESTION.NIQ AND C_QUESTION.NIQ = 6034 and BM_TH_S.nith = th.nith ) DclDateFinSuivi065,
        decode((SELECT BM_REP_S.REPONSE FROM BM_TH_S, BM_LIB_S, BM_REP_S, C_QUESTION WHERE BM_TH_S.NITH = BM_LIB_S.NITH AND BM_LIB_S.NILIB = BM_REP_S.NILIB AND BM_LIB_S.NIQ = C_QUESTION.NIQ AND C_QUESTION.NIQ = 5035 and bm_rep_s.reponse='Sans aucun traitement pharmaceutique' and BM_TH_S.nith = th.nith ),NULL,'N','O') DclTPAucun068, 
        decode((SELECT BM_REP_S.REPONSE FROM BM_TH_S, BM_LIB_S, BM_REP_S, C_QUESTION WHERE BM_TH_S.NITH = BM_LIB_S.NITH AND BM_LIB_S.NILIB = BM_REP_S.NILIB AND BM_LIB_S.NIQ = C_QUESTION.NIQ AND C_QUESTION.NIQ = 5035 and bm_rep_s.reponse='Inhibiteurs de l''acétylcholiestérase' and BM_TH_S.nith = th.nith ),NULL,'N','O') DclTPInhibAcetyl069,
        decode((SELECT BM_REP_S.REPONSE FROM BM_TH_S, BM_LIB_S, BM_REP_S, C_QUESTION WHERE BM_TH_S.NITH = BM_LIB_S.NITH AND BM_LIB_S.NILIB = BM_REP_S.NILIB AND BM_LIB_S.NIQ = C_QUESTION.NIQ AND C_QUESTION.NIQ = 5035 and bm_rep_s.reponse='Antagonistes des récepteurs NMDA' and BM_TH_S.nith = th.nith ),NULL,'N','O') DclTPAntagonisteNMDA070,
        decode((SELECT BM_REP_S.REPONSE FROM BM_TH_S, BM_LIB_S, BM_REP_S, C_QUESTION WHERE BM_TH_S.NITH = BM_LIB_S.NITH AND BM_LIB_S.NILIB = BM_REP_S.NILIB AND BM_LIB_S.NIQ = C_QUESTION.NIQ AND C_QUESTION.NIQ = 5035 and bm_rep_s.reponse='Antidépresseurs' and BM_TH_S.nith = th.nith ),NULL,'N','O') DclTPAntidepresseurs071,
        decode((SELECT BM_REP_S.REPONSE FROM BM_TH_S, BM_LIB_S, BM_REP_S, C_QUESTION WHERE BM_TH_S.NITH = BM_LIB_S.NITH AND BM_LIB_S.NILIB = BM_REP_S.NILIB AND BM_LIB_S.NIQ = C_QUESTION.NIQ AND C_QUESTION.NIQ = 5035 and bm_rep_s.reponse='Neuroleptiques' and BM_TH_S.nith = th.nith ),NULL,'N','O') DclTPNeuroleptiques072,
        decode((SELECT BM_REP_S.REPONSE FROM BM_TH_S, BM_LIB_S, BM_REP_S, C_QUESTION WHERE BM_TH_S.NITH = BM_LIB_S.NITH AND BM_LIB_S.NILIB = BM_REP_S.NILIB AND BM_LIB_S.NIQ = C_QUESTION.NIQ AND C_QUESTION.NIQ = 5035 and bm_rep_s.reponse='Anxiolitiques' and BM_TH_S.nith = th.nith ),NULL,'N','O') DclTPAnxiolitiques073,
        decode((SELECT BM_REP_S.REPONSE FROM BM_TH_S, BM_LIB_S, BM_REP_S, C_QUESTION WHERE BM_TH_S.NITH = BM_LIB_S.NITH AND BM_LIB_S.NILIB = BM_REP_S.NILIB AND BM_LIB_S.NIQ = C_QUESTION.NIQ AND C_QUESTION.NIQ = 5035 and bm_rep_s.reponse='Hypnotiques' and BM_TH_S.nith = th.nith ),NULL,'N','O') DclTPHypnotiques074,
        decode((SELECT BM_REP_S.REPONSE FROM BM_TH_S, BM_LIB_S, BM_REP_S, C_QUESTION WHERE BM_TH_S.NITH = BM_LIB_S.NITH AND BM_LIB_S.NILIB = BM_REP_S.NILIB AND BM_LIB_S.NIQ = C_QUESTION.NIQ AND C_QUESTION.NIQ = 5035 and bm_rep_s.reponse='Nootropes' and BM_TH_S.nith = th.nith ),NULL,'N','O') DclTPNootropes075,
        decode((SELECT BM_REP_S.REPONSE FROM BM_TH_S, BM_LIB_S, BM_REP_S, C_QUESTION WHERE BM_TH_S.NITH = BM_LIB_S.NITH AND BM_LIB_S.NILIB = BM_REP_S.NILIB AND BM_LIB_S.NIQ = C_QUESTION.NIQ AND C_QUESTION.NIQ = 5035 and bm_rep_s.reponse='Autres traitements pharmacologiques chroniques' and BM_TH_S.nith = th.nith ),NULL,'N','O') DclTPAutres076,
        decode((SELECT BM_REP_S.REPONSE FROM BM_TH_S, BM_LIB_S, BM_REP_S, C_QUESTION WHERE BM_TH_S.NITH = BM_LIB_S.NITH AND BM_LIB_S.NILIB = BM_REP_S.NILIB AND BM_LIB_S.NIQ = C_QUESTION.NIQ AND C_QUESTION.NIQ = 5035 and bm_rep_s.reponse='Patient traité par aucun des traitements listés' and BM_TH_S.nith = th.nith ),NULL,'N','O') DclTPAucunDansListe077,
        (SELECT decode(BM_REP_S.REPONSE,'Non','N','Oui','O','SR') FROM  BM_TH_S, BM_LIB_S, BM_REP_S, C_QUESTION WHERE BM_TH_S.NITH = BM_LIB_S.NITH AND BM_LIB_S.NILIB = BM_REP_S.NILIB AND BM_LIB_S.NIQ = C_QUESTION.NIQ AND C_QUESTION.NIQ = 4967 and  BM_TH_S.nith = th.nith ) DclEffetIndesirable078,
        decode((SELECT BM_REP_S.REPONSE FROM  BM_TH_S, BM_LIB_S, BM_REP_S, C_QUESTION WHERE BM_TH_S.NITH = BM_LIB_S.NITH AND BM_LIB_S.NILIB = BM_REP_S.NILIB AND BM_LIB_S.NIQ = C_QUESTION.NIQ AND C_QUESTION.NIQ = 4997 and bm_rep_s.reponse='Aucune prise en charge' and BM_TH_S.nith = th.nith ),NULL,'N','O') DclIPSRAucun079,
        decode((SELECT BM_REP_S.REPONSE FROM  BM_TH_S, BM_LIB_S, BM_REP_S, C_QUESTION WHERE BM_TH_S.NITH = BM_LIB_S.NITH AND  BM_LIB_S.NILIB = BM_REP_S.NILIB AND BM_LIB_S.NIQ = C_QUESTION.NIQ AND C_QUESTION.NIQ = 4997 and bm_rep_s.reponse='Par un orthophoniste' and BM_TH_S.nith = th.nith ),NULL,'N','O') DclIPSROrthophoniste080, 
        decode((SELECT BM_REP_S.REPONSE FROM  BM_TH_S, BM_LIB_S, BM_REP_S, C_QUESTION WHERE BM_TH_S.NITH = BM_LIB_S.NITH AND  BM_LIB_S.NILIB = BM_REP_S.NILIB AND BM_LIB_S.NIQ = C_QUESTION.NIQ AND C_QUESTION.NIQ = 4997 and bm_rep_s.reponse='Par un psychologue' and BM_TH_S.nith = th.nith ),NULL,'N','O') DclIPSRPsychologue081,
        decode((SELECT BM_REP_S.REPONSE FROM  BM_TH_S, BM_LIB_S, BM_REP_S, C_QUESTION WHERE BM_TH_S.NITH = BM_LIB_S.NITH AND  BM_LIB_S.NILIB = BM_REP_S.NILIB AND BM_LIB_S.NIQ = C_QUESTION.NIQ AND C_QUESTION.NIQ = 4997 and bm_rep_s.reponse='Par un kinésithérapeute' and BM_TH_S.nith = th.nith ),NULL,'N','O') DclIPSRKinesitherapeute082, 
        decode((SELECT BM_REP_S.REPONSE FROM  BM_TH_S, BM_LIB_S, BM_REP_S, C_QUESTION WHERE BM_TH_S.NITH = BM_LIB_S.NITH AND  BM_LIB_S.NILIB = BM_REP_S.NILIB AND BM_LIB_S.NIQ = C_QUESTION.NIQ AND C_QUESTION.NIQ = 4997 and bm_rep_s.reponse='Par un ergothérapeute' and BM_TH_S.nith = th.nith ),NULL,'N','O') DclIPSRErgotherapeute083, 
        decode((SELECT BM_REP_S.REPONSE FROM  BM_TH_S, BM_LIB_S, BM_REP_S, C_QUESTION WHERE BM_TH_S.NITH = BM_LIB_S.NITH AND  BM_LIB_S.NILIB = BM_REP_S.NILIB AND BM_LIB_S.NIQ = C_QUESTION.NIQ AND C_QUESTION.NIQ = 4997 and bm_rep_s.reponse='Par un groupe d''intervenants' and BM_TH_S.nith = th.nith ),NULL,'N','O') DclIPSRGroupe084, 
        decode((SELECT BM_REP_S.REPONSE FROM  BM_TH_S, BM_LIB_S, BM_REP_S, C_QUESTION WHERE BM_TH_S.NITH = BM_LIB_S.NITH AND  BM_LIB_S.NILIB = BM_REP_S.NILIB AND BM_LIB_S.NIQ = C_QUESTION.NIQ AND C_QUESTION.NIQ = 4997 and bm_rep_s.reponse='Par un accueil de jour' and BM_TH_S.nith = th.nith ),NULL,'N','O') DclIPSRAccueilJour085, 
        decode((SELECT BM_REP_S.REPONSE FROM  BM_TH_S, BM_LIB_S, BM_REP_S, C_QUESTION WHERE BM_TH_S.NITH = BM_LIB_S.NITH AND  BM_LIB_S.NILIB = BM_REP_S.NILIB AND BM_LIB_S.NIQ = C_QUESTION.NIQ AND C_QUESTION.NIQ = 4997 and bm_rep_s.reponse='Par MAIA' and BM_TH_S.nith = th.nith ),NULL,'N','O') DclIPSRMaia086, 
        decode((SELECT BM_REP_S.REPONSE FROM  BM_TH_S, BM_LIB_S, BM_REP_S, C_QUESTION WHERE BM_TH_S.NITH = BM_LIB_S.NITH AND  BM_LIB_S.NILIB = BM_REP_S.NILIB AND BM_LIB_S.NIQ = C_QUESTION.NIQ AND C_QUESTION.NIQ = 4997 and bm_rep_s.reponse='Par CLIC' and BM_TH_S.nith = th.nith ),NULL,'N','O') DclIPSRClic087, 
        decode((SELECT BM_REP_S.REPONSE FROM  BM_TH_S, BM_LIB_S, BM_REP_S, C_QUESTION WHERE BM_TH_S.NITH = BM_LIB_S.NITH AND  BM_LIB_S.NILIB = BM_REP_S.NILIB AND BM_LIB_S.NIQ = C_QUESTION.NIQ AND C_QUESTION.NIQ = 4997 and bm_rep_s.reponse='Par une plateforme de répits' and BM_TH_S.nith = th.nith ),NULL,'N','O') DclIPSRRepits088, 
        decode((SELECT BM_REP_S.REPONSE FROM  BM_TH_S, BM_LIB_S, BM_REP_S, C_QUESTION WHERE BM_TH_S.NITH = BM_LIB_S.NITH AND  BM_LIB_S.NILIB = BM_REP_S.NILIB AND BM_LIB_S.NIQ = C_QUESTION.NIQ AND C_QUESTION.NIQ = 4997 and bm_rep_s.reponse='Par SSIAD' and BM_TH_S.nith = th.nith ),NULL,'N','O') DclIPSRSiad089, 
        decode((SELECT BM_REP_S.REPONSE FROM  BM_TH_S, BM_LIB_S, BM_REP_S, C_QUESTION WHERE BM_TH_S.NITH = BM_LIB_S.NITH AND  BM_LIB_S.NILIB = BM_REP_S.NILIB AND BM_LIB_S.NIQ = C_QUESTION.NIQ AND C_QUESTION.NIQ = 4997 and bm_rep_s.reponse='Autre type de prise en charge' and BM_TH_S.nith = th.nith ),NULL,'N','O') DclIPSRAutre090, 
        decode((SELECT BM_REP_S.REPONSE FROM  BM_TH_S, BM_LIB_S, BM_REP_S, C_QUESTION WHERE BM_TH_S.NITH = BM_LIB_S.NITH AND  BM_LIB_S.NILIB = BM_REP_S.NILIB AND BM_LIB_S.NIQ = C_QUESTION.NIQ AND C_QUESTION.NIQ = 4976 and bm_rep_s.reponse='Aucun' and BM_TH_S.nith = th.nith ),NULL,'N','O') DclPCAucun091,
        decode((SELECT BM_REP_S.REPONSE FROM  BM_TH_S, BM_LIB_S, BM_REP_S, C_QUESTION WHERE BM_TH_S.NITH = BM_LIB_S.NITH AND  BM_LIB_S.NILIB = BM_REP_S.NILIB AND BM_LIB_S.NIQ = C_QUESTION.NIQ AND C_QUESTION.NIQ = 4976 and bm_rep_s.reponse='IRM' and BM_TH_S.nith = th.nith ),NULL,'N','O') DclPCIRM092, 
        decode((SELECT BM_REP_S.REPONSE FROM  BM_TH_S, BM_LIB_S, BM_REP_S, C_QUESTION WHERE BM_TH_S.NITH = BM_LIB_S.NITH AND  BM_LIB_S.NILIB = BM_REP_S.NILIB AND BM_LIB_S.NIQ = C_QUESTION.NIQ AND C_QUESTION.NIQ = 4976 and bm_rep_s.reponse='Scanner' and BM_TH_S.nith = th.nith ),NULL,'N','O') DclPCScanner093, 
        decode((SELECT BM_REP_S.REPONSE FROM  BM_TH_S, BM_LIB_S, BM_REP_S, C_QUESTION WHERE BM_TH_S.NITH = BM_LIB_S.NITH AND  BM_LIB_S.NILIB = BM_REP_S.NILIB AND BM_LIB_S.NIQ = C_QUESTION.NIQ AND C_QUESTION.NIQ = 4976 and bm_rep_s.reponse='SPECT' and BM_TH_S.nith = th.nith ),NULL,'N','O') DclPCSPECT094, 
        decode((SELECT BM_REP_S.REPONSE FROM  BM_TH_S, BM_LIB_S, BM_REP_S, C_QUESTION WHERE BM_TH_S.NITH = BM_LIB_S.NITH AND  BM_LIB_S.NILIB = BM_REP_S.NILIB AND BM_LIB_S.NIQ = C_QUESTION.NIQ AND C_QUESTION.NIQ = 4976 and bm_rep_s.reponse='PET' and BM_TH_S.nith = th.nith ),NULL,'N','O') DclPCPET095,
        decode((SELECT BM_REP_S.REPONSE FROM  BM_TH_S, BM_LIB_S, BM_REP_S, C_QUESTION WHERE BM_TH_S.NITH = BM_LIB_S.NITH AND  BM_LIB_S.NILIB = BM_REP_S.NILIB AND BM_LIB_S.NIQ = C_QUESTION.NIQ AND C_QUESTION.NIQ = 4976 and bm_rep_s.reponse='Biomarqueurs' and BM_TH_S.nith = th.nith ),NULL,'N','O') DclPCBioMarqueurs096, 
        (SELECT decode(BM_REP_S.REPONSE,'Oui','O','Non','N') FROM  BM_TH_S, BM_LIB_S, BM_REP_S, C_QUESTION WHERE BM_TH_S.NITH = BM_LIB_S.NITH AND  BM_LIB_S.NILIB = BM_REP_S.NILIB AND BM_LIB_S.NIQ = C_QUESTION.NIQ AND C_QUESTION.NIQ = 6032 and  BM_TH_S.nith = th.nith ) DclPR097, 
        (SELECT BM_REP_S.CODE FROM BM_TH_S, BM_LIB_S, BM_REP_S, C_QUESTION WHERE BM_TH_S.NITH = BM_LIB_S.NITH AND BM_LIB_S.NILIB = BM_REP_S.NILIB AND BM_LIB_S.NIQ = C_QUESTION.NIQ AND C_QUESTION.NIQ = 6029 and  BM_TH_S.nith = th.nith ) PatientProfession112, 
        (SELECT BM_REP_S.CODE FROM BM_TH_S, BM_LIB_S, BM_REP_S, C_QUESTION WHERE BM_TH_S.NITH = BM_LIB_S.NITH AND BM_LIB_S.NILIB = BM_REP_S.NILIB AND BM_LIB_S.NIQ = C_QUESTION.NIQ AND C_QUESTION.NIQ = 6030 and  BM_TH_S.nith = th.nith ) PatientAccompagnant113
    FROM
        bm_th_s th, 
        patient pat left join infopat infpat on pat.nipatient = infpat.nipatient,
        ej_srv srv
     
    WHERE
        th.niquest = '194'                                 AND  -- 
        th.retrait = 'F'                                   AND  -- 
        pat.nipatient = th.nipatient                       AND  -- 
        substr(dates, 0, 8) = to_char(sysdate, 'yyyymmdd') AND  -- Date du jour 
        th.niservice = srv.niservice                       AND
        srv.retrait = 'F'
    il y a de la duplication de code, est-ce possible de simplifier cette requête ?
    Merci.

  2. #2
    Modérateur
    Avatar de escartefigue
    Homme Profil pro
    bourreau
    Inscrit en
    Mars 2010
    Messages
    10 086
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Loir et Cher (Centre)

    Informations professionnelles :
    Activité : bourreau
    Secteur : Finance

    Informations forums :
    Inscription : Mars 2010
    Messages : 10 086
    Points : 38 380
    Points
    38 380
    Billets dans le blog
    9
    Par défaut
    La requête est assez indigeste avec cette succession sans fin de DECODE, c'est quand même assez limite de faire autant de conversions de valeurs dans une requête SQL.
    SQL a pour vocation première de récolter ou mettre à jour les données, pas de faire toute cette chirurgie esthétique, et à maintenir c'est l'enfer.

    il faudrait aussi ne pas mélanger le filtrage (where) et les critères de jointure (inner join on...)

  3. #3
    Rédacteur

    Avatar de SQLpro
    Homme Profil pro
    Expert bases de données / SQL / MS SQL Server / Postgresql
    Inscrit en
    Mai 2002
    Messages
    21 736
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Var (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Expert bases de données / SQL / MS SQL Server / Postgresql
    Secteur : Conseil

    Informations forums :
    Inscription : Mai 2002
    Messages : 21 736
    Points : 52 448
    Points
    52 448
    Billets dans le blog
    5
    Par défaut
    Les américains disent "garbage in, garbage out". Cela se traduit par : si tu as de la merde en entrée, tu as de merde en sortie....

    Ici votre modèle de données semble merdique. Il aurait sans doute fallut des tables de référence. D’où la merde en sortie des requêtes épouvantable à écrire et aux performances lamentables !

    A +
    Frédéric Brouard - SQLpro - ARCHITECTE DE DONNÉES - expert SGBDR et langage SQL
    Le site sur les SGBD relationnels et le langage SQL: http://sqlpro.developpez.com/
    Blog SQL, SQL Server, SGBDR : http://blog.developpez.com/sqlpro
    Expert Microsoft SQL Server - M.V.P. (Most valuable Professional) MS Corp.
    Entreprise SQL SPOT : modélisation, conseils, audit, optimisation, formation...
    * * * * * Expertise SQL Server : http://mssqlserver.fr/ * * * * *

  4. #4
    Membre éclairé Avatar de bstevy
    Homme Profil pro
    Solutions Architect
    Inscrit en
    Mai 2009
    Messages
    552
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 38
    Localisation : Japon

    Informations professionnelles :
    Activité : Solutions Architect
    Secteur : Finance

    Informations forums :
    Inscription : Mai 2009
    Messages : 552
    Points : 870
    Points
    870
    Par défaut
    C'est pas "shit in shit out" plutot ? moi, c'est plutôt ca que j'avais l'habitude d'entendre
    et c'est plus rapide à dire

  5. #5
    Modérateur

    Avatar de CinePhil
    Homme Profil pro
    Ingénieur d'études en informatique
    Inscrit en
    Août 2006
    Messages
    16 793
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 60
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Ingénieur d'études en informatique
    Secteur : Enseignement

    Informations forums :
    Inscription : Août 2006
    Messages : 16 793
    Points : 34 024
    Points
    34 024
    Billets dans le blog
    14
    Par défaut
    C'est probablement simplifiable tellement cette requête est horrible mais bonjour le boulot !
    Bon courage !

    Début de simplification...

    1) Commençons par récrire correctement les jointures principales et le début du SELECT avant la première sous-requête corrélée.
    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
    SELECT
        pat.nom NOM,
        pat.sexe SEXE,
        pat.nip ipp,
        SUBSTR(datenais, 1, 4) ANNEE_NAISSANCE,
        SUBSTR(datenais, 5, 2) MOIS_NAISSANCE,
        SUBSTR(datenais, 7, 2) JOUR_NAISSANCE,
        DECODE(pat.PATRONYME, null, pat.NOM, '', pat.NOM, pat.PATRONYME) NOM_NAISSANCE,
        pat.prenom PRENOM,
        infpat.NUMSECU NSS,
        DECODE(infpat.NUMSECU, null, '', '', '', substr(infpat.NUMSECU, 6, 2)) DEP_NAISSANCE,
        DECODE(infpat.NUMSECU, null, '', '', '', substr(infpat.NUMSECU, 8, 3)) VILLE_NAISSANCE,
        DECODE(srv.nietablis, 1, 'MZ', 3, 'MZ', 4, 'MZ', 5, 'MZ', 8, 'MZ', 9, 'MZ', 10, 'TH', 11, 'TH', 12, 'TH', 13, 'TH', 14, 'TH', 15, 'TH', 16, 'TH', 17, 'TH', 18, 'TH', 19, 'TH', 20, 'MZ', 21, 'TH', 22, 'MZ', 23, 'TH', 25, 'TH', 24, 'MZ', 'Autre') etb -- les sous-requêtes corrélées viennent ensuite,
    FROM bm_th_s th 
    INNER JOIN patient pat ON pat.nipatient = th.nipatient 
    	LEFT OUTER JOIN infopat infpat ON pat.nipatient = infpat.nipatient
    INNER JOIN ej_srv srv ON th.niservice = srv.niservice
    WHERE th.niquest = '194'
    	AND th.retrait = 'F'
    	AND SUBSTR(dates, 0, 8) = TO_CHAR(SYSDATE, 'yyyymmdd')
    	AND srv.retrait = 'F'
    2) Les sous-requêtes corrélées semblent toutes de la même forme avec les mêmes jointures (je n'ai pas regarder en détail, c'est quand même à vérifier).
    On peut donc passer le bloc FROM de ces sous-requêtes dans les jointures de la requête principale. On en profite pour mettre des alias afin de simplifier l'écriture.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    FROM bm_th_s th 
    INNER JOIN patient pat ON pat.nipatient = th.nipatient 
    	LEFT OUTER JOIN infopat infpat ON pat.nipatient = infpat.nipatient
    INNER JOIN ej_srv srv ON th.niservice = srv.niservice
    -- Jointures pour les sous-requêtes corrélées
    INNER JOIN BM_TH_S bmt ON bmt.nith = th.nith
    	INNER JOIN BM_LIB_S bml ON bml.NITH = bmt.NITH
    		INNER JOIN BM_REP_S bmr ON bmr.NILIB = bml.NILIB
    			INNER JOIN C_QUESTION q ON bml.NIQ = q.NIQ
    3) On transforme la série de requêtes corrélées en série de structures CASE WHEN.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    	CASE
    		WHEN q.NIQ = 4993 THEN bmr.code
    	END AS PatientNiveauEtudes049,
    	CASE
    		WHEN q.NIQ = 5023 THEN bmr_code
    	END AS PAtientEnvoyePar051
    	-- etc. sur le même principe
    Ce qui donne pour ce début de simplification la requête suivante :
    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
    SELECT
        pat.nom NOM,
        pat.sexe SEXE,
        pat.nip ipp,
        SUBSTR(datenais, 1, 4) ANNEE_NAISSANCE,
        SUBSTR(datenais, 5, 2) MOIS_NAISSANCE,
        SUBSTR(datenais, 7, 2) JOUR_NAISSANCE,
        DECODE(pat.PATRONYME, null, pat.NOM, '', pat.NOM, pat.PATRONYME) NOM_NAISSANCE,
        pat.prenom PRENOM,
        infpat.NUMSECU NSS,
        DECODE(infpat.NUMSECU, null, '', '', '', substr(infpat.NUMSECU, 6, 2)) DEP_NAISSANCE,
        DECODE(infpat.NUMSECU, null, '', '', '', substr(infpat.NUMSECU, 8, 3)) VILLE_NAISSANCE,
        DECODE(srv.nietablis, 1, 'MZ', 3, 'MZ', 4, 'MZ', 5, 'MZ', 8, 'MZ', 9, 'MZ', 10, 'TH', 11, 'TH', 12, 'TH', 13, 'TH', 14, 'TH', 15, 'TH', 16, 'TH', 17, 'TH', 18, 'TH', 19, 'TH', 20, 'MZ', 21, 'TH', 22, 'MZ', 23, 'TH', 25, 'TH', 24, 'MZ', 'Autre') etb,
    	CASE
    		WHEN q.NIQ = 4993 THEN bmr.code
    	END AS PatientNiveauEtudes049,
    	CASE
    		WHEN q.NIQ = 5023 THEN bmr_code
    	END AS PAtientEnvoyePar051
    	-- etc. sur le même principe
    FROM bm_th_s th 
    INNER JOIN patient pat ON pat.nipatient = th.nipatient 
    	LEFT OUTER JOIN infopat infpat ON pat.nipatient = infpat.nipatient
    INNER JOIN ej_srv srv ON th.niservice = srv.niservice
    -- Jointures pour les sous-requêtes corrélées
    INNER JOIN BM_TH_S bmt ON bmt.nith = th.nith
    	INNER JOIN BM_LIB_S bml ON bml.NITH = bmt.NITH
    		INNER JOIN BM_REP_S bmr ON bmr.NILIB = bml.NILIB
    			INNER JOIN C_QUESTION q ON bml.NIQ = q.NIQ
    WHERE th.niquest = '194'
    	AND th.retrait = 'F'
    	AND SUBSTR(dates, 0, 8) = TO_CHAR(SYSDATE, 'yyyymmdd')
    	AND srv.retrait = 'F'
    À tester petit à petit, à chaque transformation, pour vérifier que le résultat obtenu est bien identique.

    Encore une fois, bon courage !
    Philippe Leménager. Ingénieur d'étude à l'École Nationale Supérieure de Formation de l'Enseignement Agricole. Autoentrepreneur.
    Mon ancien blog sur la conception des BDD, le langage SQL, le PHP... et mon nouveau blog sur les mêmes sujets.
    « Ce que l'on conçoit bien s'énonce clairement, et les mots pour le dire arrivent aisément ». (Nicolas Boileau)
    À la maison comme au bureau, j'utilise la suite Linux Mageïa !

  6. #6
    Membre actif
    Homme Profil pro
    Étudiant
    Inscrit en
    Avril 2012
    Messages
    538
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Avril 2012
    Messages : 538
    Points : 262
    Points
    262
    Par défaut
    Du peu de SQL que j'ai fait durant mes études, on m'a toujours appris à faire les jointures dans la clause WHERE, non ?

  7. #7
    Modérateur

    Avatar de CinePhil
    Homme Profil pro
    Ingénieur d'études en informatique
    Inscrit en
    Août 2006
    Messages
    16 793
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 60
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Ingénieur d'études en informatique
    Secteur : Enseignement

    Informations forums :
    Inscription : Août 2006
    Messages : 16 793
    Points : 34 024
    Points
    34 024
    Billets dans le blog
    14
    Par défaut
    Ben vos profs ont besoin de se recycler... comme beaucoup de profs, hélas !
    Les jointures s'écrivent depuis plus de 20 ans avec l'opérateur JOIN ; il serait temps de s'y mettre !
    Voir les cours et tutoriels de formation pour apprendre SQL.
    Philippe Leménager. Ingénieur d'étude à l'École Nationale Supérieure de Formation de l'Enseignement Agricole. Autoentrepreneur.
    Mon ancien blog sur la conception des BDD, le langage SQL, le PHP... et mon nouveau blog sur les mêmes sujets.
    « Ce que l'on conçoit bien s'énonce clairement, et les mots pour le dire arrivent aisément ». (Nicolas Boileau)
    À la maison comme au bureau, j'utilise la suite Linux Mageïa !

  8. #8
    Expert éminent
    Avatar de StringBuilder
    Homme Profil pro
    Chef de projets
    Inscrit en
    Février 2010
    Messages
    4 146
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 45
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Chef de projets
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Février 2010
    Messages : 4 146
    Points : 7 388
    Points
    7 388
    Billets dans le blog
    1
    Par défaut
    J'ai fais mes études dans les années 90, à un niveau assez bas : bac + 2

    Et j'ai pourtant appris à l'époque à faire des jointures dans le FROM avec INNER JOIN / LEFT-RIGHT OUTER JOIN

    Les profs qui parlent encore, plus de 20 ans plus tard, de jointures dans le WHERE méritent de pointer au chômage.
    On ne jouit bien que de ce qu’on partage.

  9. #9
    Membre actif
    Homme Profil pro
    Étudiant
    Inscrit en
    Avril 2012
    Messages
    538
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Avril 2012
    Messages : 538
    Points : 262
    Points
    262
    Par défaut
    Il a pas l'air d'aimer les jointures dans le FROM :

    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
    SELECT
        -- 1er Partie
        pat.nom NOM,
        pat.sexe SEXE,
        pat.nip ipp,
        substr(datenais, 1, 4) ANNEE_NAISSANCE,
        substr(datenais, 5, 2) MOIS_NAISSANCE,
        substr(datenais, 7, 2) JOUR_NAISSANCE,
        decode(pat.PATRONYME, null, pat.NOM, '', pat.NOM, pat.PATRONYME) NOM_NAISSANCE,
        pat.prenom PRENOM,
        infpat.NUMSECU NSS,
        decode(infpat.NUMSECU, null, '', '', '', substr(infpat.NUMSECU, 6, 2)) DEP_NAISSANCE,
        decode(infpat.NUMSECU, null, '', '', '', substr(infpat.NUMSECU, 8, 3)) VILLE_NAISSANCE,
        decode(srv.nietablis, 1, 'MZ', 3, 'MZ', 4, 'MZ', 5, 'MZ', 8, 'MZ', 9, 'MZ', 10, 'TH', 11, 'TH', 12, 'TH', 13, 'TH', 14, 'TH', 15, 'TH', 16, 'TH', 17, 'TH', 18, 'TH', 19, 'TH', 20, 'MZ', 21, 'TH', 22, 'MZ', 23, 'TH', 25, 'TH', 24, 'MZ', 'Autre') etb
     
    FROM
        BM_TH_S th, 
          INNER JOIN      patient pat    ON pat.nipatient = th.nipatient,
        LEFT OUTER JOIN infopat infpat ON pat.nipatient = infpat.nipatient,
        INNER JOIN      ej_srv  srv    ON th.niservice  = srv.niservice,
     
    WHERE
        th.niquest = '194'                                 AND  -- Identifiant du Questionnaire de référence
        th.retrait = 'F'                                   AND  -- Etat de la supression logique du document
        substr(dates, 0, 8) = to_char(sysdate, 'yyyymmdd') AND  -- Date du jour 
        srv.retrait = 'F'                                       -- Drapeau de supression de l'enregistrement
    Résultats :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    ORA-00903: nom de table non valide
    00903. 00000 -  "invalid table name"
    *Cause:    
    *Action:
    Erreur à la ligne 1, colonne 1*352
    J'utilise SQL Developer sur une base Oracle.

  10. #10
    Modérateur
    Avatar de escartefigue
    Homme Profil pro
    bourreau
    Inscrit en
    Mars 2010
    Messages
    10 086
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Loir et Cher (Centre)

    Informations professionnelles :
    Activité : bourreau
    Secteur : Finance

    Informations forums :
    Inscription : Mars 2010
    Messages : 10 086
    Points : 38 380
    Points
    38 380
    Billets dans le blog
    9
    Par défaut
    Citation Envoyé par StringBuilder Voir le message
    J'ai fais mes études dans les années 90, à un niveau assez bas : bac + 2

    Et j'ai pourtant appris à l'époque à faire des jointures dans le FROM avec INNER JOIN / LEFT-RIGHT OUTER JOIN

    Les profs qui parlent encore, plus de 20 ans plus tard, de jointures dans le WHERE méritent de pointer au chômage.
    Sans aller jusqu'à cette extrémité , la méconnaissance par certains professionnels (pas seulement les enseignants, mais aussi de nombreux concepteurs et développeurs), de ce type de fondamentaux, met en évidence le besoin pour tous de formation continue. Sans quoi on glisse lentement mais surement de l'expertise vers l'ignorance avec tous les dégâts que ça implique.

    Certains pays, et notamment les pays scandinaves, l'ont bien compris, et on pris des mesures incitatives à cet égard, il serait bon de s'en inspirer

  11. #11
    Modérateur
    Avatar de escartefigue
    Homme Profil pro
    bourreau
    Inscrit en
    Mars 2010
    Messages
    10 086
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Loir et Cher (Centre)

    Informations professionnelles :
    Activité : bourreau
    Secteur : Finance

    Informations forums :
    Inscription : Mars 2010
    Messages : 10 086
    Points : 38 380
    Points
    38 380
    Billets dans le blog
    9
    Par défaut
    Citation Envoyé par CliffeCSTL Voir le message
    Il a pas l'air d'aimer les jointures dans le FROM :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    SELECT
     FROM
        BM_TH_S th, <== VIRGULE A SUPPRIMER
          INNER JOIN      patient pat    ON pat.nipatient = th.nipatient,
        LEFT OUTER JOIN infopat infpat ON pat.nipatient = infpat.nipatient,
        INNER JOIN      ej_srv  srv    ON th.niservice  = srv.niservice,
    Ma réponse dans la requete

  12. #12
    Expert éminent
    Avatar de StringBuilder
    Homme Profil pro
    Chef de projets
    Inscrit en
    Février 2010
    Messages
    4 146
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 45
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Chef de projets
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Février 2010
    Messages : 4 146
    Points : 7 388
    Points
    7 388
    Billets dans le blog
    1
    Par défaut
    Tu peux mettre un "S" à virgule
    On ne jouit bien que de ce qu’on partage.

  13. #13
    Rédacteur/Modérateur

    Homme Profil pro
    Ingénieur qualité méthodes
    Inscrit en
    Décembre 2013
    Messages
    4 038
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur qualité méthodes
    Secteur : Conseil

    Informations forums :
    Inscription : Décembre 2013
    Messages : 4 038
    Points : 9 347
    Points
    9 347
    Par défaut
    Si je reprend la dernière proposition de Cinephil, je la corrigerais de cette façon :

    Proposition initiale :

    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
    34
     
    SELECT
        pat.nom NOM,
        pat.sexe SEXE,
        pat.nip ipp,
        SUBSTR(datenais, 1, 4) ANNEE_NAISSANCE,
        SUBSTR(datenais, 5, 2) MOIS_NAISSANCE,
        SUBSTR(datenais, 7, 2) JOUR_NAISSANCE,
        DECODE(pat.PATRONYME, null, pat.NOM, '', pat.NOM, pat.PATRONYME) NOM_NAISSANCE,
        pat.prenom PRENOM,
        infpat.NUMSECU NSS,
        DECODE(infpat.NUMSECU, null, '', '', '', substr(infpat.NUMSECU, 6, 2)) DEP_NAISSANCE,
        DECODE(infpat.NUMSECU, null, '', '', '', substr(infpat.NUMSECU, 8, 3)) VILLE_NAISSANCE,
        DECODE(srv.nietablis, 1, 'MZ', 3, 'MZ', 4, 'MZ', 5, 'MZ', 8, 'MZ', 9, 'MZ', 10, 'TH', 11, 'TH', 12, 'TH', 13, 'TH', 14, 'TH', 15, 'TH', 16, 'TH', 17, 'TH', 18, 'TH', 19, 'TH', 20, 'MZ', 21, 'TH', 22, 'MZ', 23, 'TH', 25, 'TH', 24, 'MZ', 'Autre') etb,
    	CASE
    		WHEN q.NIQ = 4993 THEN bmr.code
    	END AS PatientNiveauEtudes049,
    	CASE
    		WHEN q.NIQ = 5023 THEN bmr_code
    	END AS PAtientEnvoyePar051
    	-- etc. sur le même principe
    FROM bm_th_s th 
    INNER JOIN patient pat ON pat.nipatient = th.nipatient 
    	LEFT OUTER JOIN infopat infpat ON pat.nipatient = infpat.nipatient
    INNER JOIN ej_srv srv ON th.niservice = srv.niservice
    -- Jointures pour les sous-requêtes corrélées
    INNER JOIN BM_TH_S bmt ON bmt.nith = th.nith
    	INNER JOIN BM_LIB_S bml ON bml.NITH = bmt.NITH
    		INNER JOIN BM_REP_S bmr ON bmr.NILIB = bml.NILIB
    			INNER JOIN C_QUESTION q ON bml.NIQ = q.NIQ
    WHERE th.niquest = '194'
    	AND th.retrait = 'F'
    	AND SUBSTR(dates, 0, 8) = TO_CHAR(SYSDATE, 'yyyymmdd')
    	AND srv.retrait = 'F'
    Correction :

    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
    34
    35
    SELECT
        pat.nom NOM,
        pat.sexe SEXE,
        pat.nip ipp,
        SUBSTR(datenais, 1, 4) ANNEE_NAISSANCE,
        SUBSTR(datenais, 5, 2) MOIS_NAISSANCE,
        SUBSTR(datenais, 7, 2) JOUR_NAISSANCE,
        DECODE(pat.PATRONYME, null, pat.NOM, '', pat.NOM, pat.PATRONYME) NOM_NAISSANCE,
        pat.prenom PRENOM,
        infpat.NUMSECU NSS,
        DECODE(infpat.NUMSECU, null, '', '', '', substr(infpat.NUMSECU, 6, 2)) DEP_NAISSANCE,
        DECODE(infpat.NUMSECU, null, '', '', '', substr(infpat.NUMSECU, 8, 3)) VILLE_NAISSANCE,
        DECODE(srv.nietablis, 1, 'MZ', 3, 'MZ', 4, 'MZ', 5, 'MZ', 8, 'MZ', 9, 'MZ', 10, 'TH', 11, 'TH', 12, 'TH', 13, 'TH', 14, 'TH', 15, 'TH', 16, 'TH', 17, 'TH', 18, 'TH', 19, 'TH', 20, 'MZ', 21, 'TH', 22, 'MZ', 23, 'TH', 25, 'TH', 24, 'MZ', 'Autre') etb,
    	min ( CASE
    		WHEN q.NIQ = 4993 THEN bmr.code
    	END )  AS PatientNiveauEtudes049,
    	min ( CASE
    		WHEN q.NIQ = 5023 THEN bmr_code
    	END ) AS PAtientEnvoyePar051
    	-- etc. sur le même principe
    FROM bm_th_s th 
    INNER JOIN patient pat ON pat.nipatient = th.nipatient 
    	LEFT OUTER JOIN infopat infpat ON pat.nipatient = infpat.nipatient
    INNER JOIN ej_srv srv ON th.niservice = srv.niservice
    -- Jointures pour les sous-requêtes corrélées
    INNER JOIN BM_TH_S bmt ON bmt.nith = th.nith
    	INNER JOIN BM_LIB_S bml ON bml.NITH = bmt.NITH
    		INNER JOIN BM_REP_S bmr ON bmr.NILIB = bml.NILIB
    			INNER JOIN C_QUESTION q ON bml.NIQ = q.NIQ
    WHERE th.niquest = '194'
    	AND th.retrait = 'F'
    	AND SUBSTR(dates, 0, 8) = TO_CHAR(SYSDATE, 'yyyymmdd')
    	AND srv.retrait = 'F'
     
    group by ... ...
    J'ai ajouté des Min et le GROUP BY final ... Au lieu des MIN, je pouvais faire SUM ou MAX ... théoriquement, ça ne change rien.

    En effet , en 'transposant' la requête, tu vas obtenir ton résultat sous forme de tableau, ou même de diagonale, et il faut regrouper sur une même ligne tout ce qui concerne un même enregistrement.
    N'oubliez pas le bouton Résolu si vous avez obtenu une réponse à votre question.

  14. #14
    Membre actif
    Homme Profil pro
    Étudiant
    Inscrit en
    Avril 2012
    Messages
    538
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Avril 2012
    Messages : 538
    Points : 262
    Points
    262
    Par défaut
    Je n'obtiens pas le même résultat avec ceci :

    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
    SELECT DISTINCT
        -- 1er Partie
        pat.nom NOM,
        pat.sexe SEXE,
        pat.nip ipp,
        substr(datenais, 1, 4) ANNEE_NAISSANCE,
        substr(datenais, 5, 2) MOIS_NAISSANCE,
        substr(datenais, 7, 2) JOUR_NAISSANCE,
        decode(pat.PATRONYME, null, pat.NOM, '', pat.NOM, pat.PATRONYME) NOM_NAISSANCE,
        pat.prenom PRENOM,
        infpat.NUMSECU NSS,
        decode(infpat.NUMSECU, null, '', '', '', substr(infpat.NUMSECU, 6, 2)) DEP_NAISSANCE,
        decode(infpat.NUMSECU, null, '', '', '', substr(infpat.NUMSECU, 8, 3)) VILLE_NAISSANCE,
        decode(srv.nietablis, 1, 'MZ', 3, 'MZ', 4, 'MZ', 5, 'MZ', 8, 'MZ', 9, 'MZ', 10, 'TH', 11, 'TH', 12, 'TH', 13, 'TH', 14, 'TH', 15, 'TH', 16, 'TH', 17, 'TH', 18, 'TH', 19, 'TH', 20, 'MZ', 21, 'TH', 22, 'MZ', 23, 'TH', 25, 'TH', 24, 'MZ', 'Autre') etb,
        -- 2eme Partie
        CASE WHEN q.NIQ = 4993 THEN bmr.CODE END AS PatientNiveauEtudes049
     
    FROM
        BM_TH_S th
        INNER JOIN      patient pat    ON pat.nipatient = th.nipatient
        LEFT OUTER JOIN infopat infpat ON pat.nipatient = infpat.nipatient
        INNER JOIN      ej_srv  srv    ON th.niservice  = srv.niservice
        -- Jointures pour les sous-requêtes corrélées
        INNER JOIN BM_TH_S      bmt    ON bmt.nith      = th.nith
        INNER JOIN BM_LIB_S     bml    ON bml.NITH      = bmt.NITH
        INNER JOIN BM_REP_S     bmr    ON bmr.NILIB     = bml.NILIB
        INNER JOIN C_QUESTION   q      ON bml.NIQ       = q.NIQ
     
    WHERE
        th.niquest = '194'                                 AND  -- Identifiant du Questionnaire de référence
        th.retrait = 'F'                                   AND  -- Etat de la supression logique du document
        substr(dates, 0, 8) = to_char(sysdate, 'yyyymmdd') AND  -- Date du jour 
        srv.retrait = 'F'                                       -- Drapeau de supression de l'enregistrement
    La colonne PatientNiveauEtudes049 contient des champs 'null' qui ne devraient pas être la. Je pense que les jointures ne sont pas correctes.


    PS : Lorsque j'ajoute la condition 'PatientNiveauEtudes049 IS NOT NULL' dans le WHERE il me met une erreur : "PATIENTNIVEAUETUDES049" : identificateur non valide

  15. #15
    Rédacteur

    Avatar de SQLpro
    Homme Profil pro
    Expert bases de données / SQL / MS SQL Server / Postgresql
    Inscrit en
    Mai 2002
    Messages
    21 736
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Var (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Expert bases de données / SQL / MS SQL Server / Postgresql
    Secteur : Conseil

    Informations forums :
    Inscription : Mai 2002
    Messages : 21 736
    Points : 52 448
    Points
    52 448
    Billets dans le blog
    5
    Par défaut
    D'où vient le colonne "dates" ?
    Frédéric Brouard - SQLpro - ARCHITECTE DE DONNÉES - expert SGBDR et langage SQL
    Le site sur les SGBD relationnels et le langage SQL: http://sqlpro.developpez.com/
    Blog SQL, SQL Server, SGBDR : http://blog.developpez.com/sqlpro
    Expert Microsoft SQL Server - M.V.P. (Most valuable Professional) MS Corp.
    Entreprise SQL SPOT : modélisation, conseils, audit, optimisation, formation...
    * * * * * Expertise SQL Server : http://mssqlserver.fr/ * * * * *

  16. #16
    Membre actif
    Homme Profil pro
    Étudiant
    Inscrit en
    Avril 2012
    Messages
    538
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Avril 2012
    Messages : 538
    Points : 262
    Points
    262
    Par défaut
    elle est dans : BM_TH_S

  17. #17
    Membre actif
    Homme Profil pro
    Étudiant
    Inscrit en
    Avril 2012
    Messages
    538
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Avril 2012
    Messages : 538
    Points : 262
    Points
    262
    Par défaut
    Si je fais une requête sur deux tables sans jointure, il va faire automatiquement le produit cartésien ?
    Exemple :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    SELECT colonne1, colonne2
    FROM table1, table2

  18. #18
    Modérateur
    Avatar de escartefigue
    Homme Profil pro
    bourreau
    Inscrit en
    Mars 2010
    Messages
    10 086
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Loir et Cher (Centre)

    Informations professionnelles :
    Activité : bourreau
    Secteur : Finance

    Informations forums :
    Inscription : Mars 2010
    Messages : 10 086
    Points : 38 380
    Points
    38 380
    Billets dans le blog
    9
    Par défaut
    Oui le résultat sera le produit cartésien, avec mise en vis à vis de données qui n'ont rien à voir ensemble

  19. #19
    Membre actif
    Homme Profil pro
    Étudiant
    Inscrit en
    Avril 2012
    Messages
    538
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Avril 2012
    Messages : 538
    Points : 262
    Points
    262
    Par défaut
    D'accord. Donc la requête ne peut pas être simplifier.

    Merci quand même.

  20. #20
    Modérateur

    Avatar de CinePhil
    Homme Profil pro
    Ingénieur d'études en informatique
    Inscrit en
    Août 2006
    Messages
    16 793
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 60
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Ingénieur d'études en informatique
    Secteur : Enseignement

    Informations forums :
    Inscription : Août 2006
    Messages : 16 793
    Points : 34 024
    Points
    34 024
    Billets dans le blog
    14
    Par défaut
    Avez-vous essayé la proposition de tbc92 ?
    Philippe Leménager. Ingénieur d'étude à l'École Nationale Supérieure de Formation de l'Enseignement Agricole. Autoentrepreneur.
    Mon ancien blog sur la conception des BDD, le langage SQL, le PHP... et mon nouveau blog sur les mêmes sujets.
    « Ce que l'on conçoit bien s'énonce clairement, et les mots pour le dire arrivent aisément ». (Nicolas Boileau)
    À la maison comme au bureau, j'utilise la suite Linux Mageïa !

Discussions similaires

  1. Aide pour Simplifier/optimiser une requête SQL
    Par bubu06 dans le forum Requêtes
    Réponses: 3
    Dernier message: 10/05/2012, 19h25
  2. [MySQL] Simplifier une requête sql et affichage dans un tableau
    Par Debutant10 dans le forum PHP & Base de données
    Réponses: 9
    Dernier message: 01/05/2012, 15h36
  3. [MySQL] Simplifier une requête SQL
    Par maestro982 dans le forum PHP & Base de données
    Réponses: 11
    Dernier message: 23/05/2010, 14h26
  4. [SQL] Simplifier une requête SQL ?
    Par renaud26 dans le forum PHP & Base de données
    Réponses: 5
    Dernier message: 29/04/2006, 14h50
  5. Utilisation de MAX dans une requête SQL
    Par Evil onE dans le forum Langage SQL
    Réponses: 7
    Dernier message: 15/06/2004, 19h38

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