Précédent   Forum des professionnels en informatique > Bases de données > Langage SQL
Langage SQL Forum d'entraide sur le langage SQL et sur les questions liées à la conception de schéma (DDL). Cours SQL
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 27/10/2011, 10h38   #1
Invité de passage
 
Inscription : mars 2007
Messages : 34
Détails du profil
Informations forums :
Inscription : mars 2007
Messages : 34
Points : 0
Points : 0
Par défaut dernier commentaire planning : Planning ayant la date de commentaire la plus récente

Bonjour,

J'ai une base avec des tables de plus de 7000 lignes et pour 3 d'entre elles ( PROJET, PLANNING, COMMENTAIRE) je veux afficher le dernier commentaire de tous PLANNING confondus de chaque projet

Plus de 300 projets avec chacun au moins 10 planning donc afficher le planning ayant la date de commentaire la plus recente ainsi que le codeProjet et le nom du PROJET associé.

Un exemple avec les 3 tables (cijoint un fichier avec l'exemple car n'arrive pas à inserer les table ):

PROJET

id codeProjet NomProjet commentaireProjet
-------------------------------------------------------------------
11 001 Projet1
12 002 Projet2 Amenagement


PLANNING

id nomPlanning projet_id
-------------------------------------------------------------------
71 Planning1 12
72 Planning2 12
73 Planning3 12
74 Planning4 12


COMMENTAIRE

id date_derniere_modif id_objet type_objet Commentaire
---------------------------------------------------------------------
91 2010-04-12 72 PLANNING MAJ1_Planning_2
92 2010-05-25 72 PLANNING MAJ2_Planning_2
93 2010-09-11 73 PLANNING MAJ1_Planning_3
94 2010-10-12 11 PROJET Creation Projet1
95 2011-03-19 74 PLANNING MAJ1_Planning_4
96 2011-06-21 72 PLANNING MAJ3_Planning_2
97 2011-07-14 12 PROJET MAJ Projet2
98 2011-09-27 73 PLANNING MAJ2_Planning_3



Ma requête :
Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
SELECT PROJ.codeProjet, PROJ.nomProjet, PLAN.nomPlanning, 
COMMENT.date_derniere_modif AS date_modif ,COMMENT.commentaire
 
FROM PROJET PROJ
 
LEFT JOIN PLANNING PLAN ON PROJ.id = PLAN.project_id 
LEFT JOIN COMMENTAIRE COMMENT ON 
COMMENT. date_derniere_modif = 
(SELECT max(C.date_derniere_modif) FROM COMMENTAIRE C WHERE C.id_ojet = PLAN.id )
 
WHERE PROJ.commentaireProjet IS NOT NULL
 
GROUP BY PROJ.codeProjet, PROJ.nomProjet, PLAN.nomPlanning, 
COMMENT.date_derniere_modif, COMMENT.commentaire
le resultat :

codeProjet nomProjet nomPlanning date_modif commentaire
-----------------------------------------------------------------------
002 Projet2 Planning2 2011-06-21 MAJ3_Planning2
002 Projet2 Planning3 2011-09-27 MAJ2_Planning3
002 Projet2 Planning4 2011-03-19 MAJ1_Planning4


Mais voici ce que je veux extraire et aficher



codeProjet nomProjet nomPlanning date_modif commentaire
----------------------------------------------------------------------
002 Projet2 Planning3 2011-09-27 MAJ2_Planning_3



Quelqu'un a-t-il une idée de la solution et merci de votre reponse à
l 'avance ?
Fichiers attachés
Type de fichier : doc derniercommentairer.doc (59,0 Ko, 0 affichages)
lightzeus est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 27/10/2011, 10h49   #2
Expert Confirmé
 
Homme
Inscription : mai 2002
Messages : 1 641
Détails du profil
Informations personnelles :
Sexe : Homme
Âge : 29
Localisation : France, Rhône (Rhône Alpes)

Informations forums :
Inscription : mai 2002
Messages : 1 641
Points : 2 634
Points : 2 634
bonjour,

quel est votre SGBD..?
punkoff est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 27/10/2011, 10h51   #3
Membre du Club
 
Inscription : avril 2009
Messages : 134
Détails du profil
Informations personnelles :
Localisation : France, Rhône (Rhône Alpes)

Informations forums :
Inscription : avril 2009
Messages : 134
Points : 42
Points : 42
As tu essayé avec la fonction MAX sur ta date, pour avoir la plus récente ?
En mettant bien le GROUP BY comme il faut.
fhmayn est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 27/10/2011, 10h59   #4
Membre confirmé
 
Homme Grégoire MARTIN
Ingénieur développement logiciels
Inscription : janvier 2011
Messages : 128
Détails du profil
Informations personnelles :
Nom : Homme Grégoire MARTIN
Âge : 32
Localisation : France, Hauts de Seine (Île de France)

Informations professionnelles :
Activité : Ingénieur développement logiciels
Secteur : Finance

Informations forums :
Inscription : janvier 2011
Messages : 128
Points : 225
Points : 225
Bonjour,

Si tu veux le dernier commentaire tous projets confondus , alors il faut décoréler ta sous requete.


Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
 
SELECT PROJ.codeProjet
     , PROJ.nomProjet
     , PLAN.nomPlanning
     , COMMENT.date_derniere_modif AS date_modif 
     , COMMENT.commentaire
 FROM PROJET PROJ
 INNER JOIN PLANNING PLAN ON PROJ.id = PLAN.project_id 
 INNER JOIN COMMENTAIRE COMMENT ON COMMENT.id_objet = PLAN.id
 WHERE PROJ.commentaireProjet IS NOT NULL
 AND COMMENT.date_derniere_modif = (SELECT max(C.date_derniere_modif) 
  FROM COMMENTAIRE C )
;
__________________
Cordialement.
ORA-007 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 27/10/2011, 11h34   #5
Invité de passage
 
Inscription : mars 2007
Messages : 34
Détails du profil
Informations forums :
Inscription : mars 2007
Messages : 34
Points : 0
Points : 0
Par défaut Le planning ayant la date de commentaire la plus récente pour un projet donné.

SGBD PostgreSQL

Pour être un peut plus clair :
Le planning ayant la date de commentaire la plus récente pour un projet donné.

Par exemple le projet 12 à 3 planning (1, 2, 3)
Si le planning 3 à la date de commentaire la plus récente ( les trois planning confondus ) je veux pouvoir l'afficher ainsi que le code du projet.

Cf fichier joint
lightzeus est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 27/10/2011, 11h44   #6
Invité de passage
 
Inscription : mars 2007
Messages : 34
Détails du profil
Informations forums :
Inscription : mars 2007
Messages : 34
Points : 0
Points : 0
Il ne s'agit pas de cela mais du dernier commentaire du planning ayant la date de commentaire la plus récente (tous planning confondus) d'un projet donné
Citation:
Envoyé par ORA-007 Voir le message
Bonjour,

Si tu veux le dernier commentaire tous projets confondus , alors il faut décoréler ta sous requete.


Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
 
SELECT PROJ.codeProjet
     , PROJ.nomProjet
     , PLAN.nomPlanning
     , COMMENT.date_derniere_modif AS date_modif 
     , COMMENT.commentaire
 FROM PROJET PROJ
 INNER JOIN PLANNING PLAN ON PROJ.id = PLAN.project_id 
 INNER JOIN COMMENTAIRE COMMENT ON COMMENT.id_objet = PLAN.id
 WHERE PROJ.commentaireProjet IS NOT NULL
 AND COMMENT.date_derniere_modif = (SELECT max(C.date_derniere_modif) 
  FROM COMMENTAIRE C )
;
lightzeus est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 27/10/2011, 11h47   #7
Expert Confirmé
 
Homme
Inscription : mai 2002
Messages : 1 641
Détails du profil
Informations personnelles :
Sexe : Homme
Âge : 29
Localisation : France, Rhône (Rhône Alpes)

Informations forums :
Inscription : mai 2002
Messages : 1 641
Points : 2 634
Points : 2 634
bonjour,


vu que pgsql supporte les fonctions de fenetrage..

Code :
1
2
3
4
5
6
7
8
9
10
11
12
 
WITH tmp AS (
SELECT pla.projet_id, pla.nomPlanning, com.date_derniere_modif, com.Commentaire,
row_number() over(partition BY pla.projet_id ORDER BY com.date_derniere_modif DESC) AS rnk
FROM PLANNING pla
INNER JOIN COMMENTAIRE com ON com.objet_id = pla.id)
 
 
SELECT prj.nomProjet, b.projet_id, b.nomPlanning, b.date_derniere_modif, b.Commentaire
FROM projet prj
INNER JOIN tmp b ON prj.id = b.projet_id
WHERE b.rnk = 1
punkoff est déconnecté   Envoyer un message privé Réponse avec citation 10
Vieux 28/10/2011, 11h09   #8
Invité de passage
 
Inscription : mars 2007
Messages : 34
Détails du profil
Informations forums :
Inscription : mars 2007
Messages : 34
Points : 0
Points : 0
Merci punkoff ça marche merci encore et à tous ceux qui ont répondu à ma demande.
lightzeus est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse Proposer ce sujet en actualité Cette discussion est résolue.
Outils de la discussion



Fuseau horaire GMT +2. Il est actuellement 05h38.


 
 
 
 
Partenaires

Hébergement Web