Extraction des mois dans un champ de type DATETIME et tri

Fermé
stribouille - 5 févr. 2014 à 12:02
 stribouille - 10 févr. 2014 à 14:33
Bonjour,

Je dois récupérer des enregistrements et les trier par mois sur une colonne qui est de type DATETIME. Ceci à faire en mysql (pas en php).

Est-ce possible et si oui comment procéder?

Merci d'avance.

21 réponses

Il s'agit donc de regroupement plutôt que de tri.

Pour avoir tes résultats en colonnes uniquement en mysql, je n'ai trouvé que ça. C'est lourd, mais je ne suis pas un spécialiste.

SELECT a.nom nnom, 
(SELECT SUM(prix) FROM essai WHERE nom=nnom AND MONTH(date) = 1) Janvier,
(SELECT SUM(prix) FROM essai WHERE nom=nnom AND MONTH(date) = 2) Février,
(SELECT SUM(prix) FROM essai WHERE nom=nnom AND MONTH(date) = 3) Mars,
...
(SELECT SUM(prix) FROM essai WHERE nom=nnom AND MONTH(date) = 12) Décembre,
(SELECT SUM(prix) FROM essai WHERE nom=nnom ) Année

FROM  'essai' a GROUP BY  nnom


Attention, les années sont regroupées.
1
Utilisateur anonyme
7 févr. 2014 à 14:50
Quand tu écris :
(select sum('c_commande_commun'.'Total_HT') AS 'Total_HT' from 'c_commande_commun' where (month('c_commande_commun'.'DateCDA') = 1)) AS 'Janvier'

Ttu vois bien que tu ne tiens pas compte de l'année dans ton SUM : la seule condition est que le mois soit 1

Il faut ajouter la condition sur l'année :

SELECT a.nom nnom, YEAR(date) yd, 
(SELECT SUM(prix) FROM essai WHERE nom=nnom AND MONTH(date) = 1 and YEAR(date)=yd) Janvier,
(SELECT SUM(prix) FROM essai WHERE nom=nnom AND MONTH(date) = 2 and YEAR(date)=yd) Février,
(SELECT SUM(prix) FROM essai WHERE nom=nnom AND MONTH(date) = 3 and YEAR(date)=yd) Mars,
...
(SELECT SUM(prix) FROM essai WHERE nom=nnom AND MONTH(date) = 12 and YEAR(date)=yd) Décembre,
(SELECT SUM(prix) FROM essai WHERE nom=nnom  and YEAR(date)=yd) Année 
FROM  essai a GROUP BY  nnom,yd ORDER BY yd, nnom
1
Bonjour

Il existe un tas de fonctions en mysql pour manipuler les dates. Pour extraire le mois, c'est MONTH()
Mais qu'entends-tu par trier par mois ? Tu veux regrouper tous les mois de janvier au début de ta réponse, indépendamment de l'année ?
0
Bonjour le père, merci beaucoup pour ta réponse.

En fait j'ai une liste de commandes de différents produits et je dois récapituler en première colonne par exemple le nom du produit, puis en 12 colonnes le montant des commandes mensuels et en 13ème colonne le total commandé par produit.
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
Merci encore le père, je vais tester tout çà bientôt et reviendrai ici pour dire si oui ou non cela fonctionne ;)
0
bonjour,

Alors le total de l'année (par année) fonctionne: j'ai ajouté un GROUP BY annee à la fin.

Par contre, pour chaque mois, j'ai la même valeur quelque soit l'année. Par exemple, si en juin 2013 j'ai 500, je vois apparaitre 500 pour juin 2013 et juin 2014.

Même en rajoutant GROUP BY annee dans les sous requete cela ne fonctionne pas...

La j'avoue que je sèche.
0
Utilisateur anonyme
7 févr. 2014 à 14:28
Qu'as-tu fait comme requête ?
0
Bonjour le père :)

alors j'ai pris example sur ta requête pour une :montant de tout ce qui est commandé chiffré mois par mois et total de l'année (par année donc).

Voici ma requête:

select sum('c_commande_commun'.'Total_HT') AS 'Total_HT',(select sum('c_commande_commun'.'Total_HT') AS 'Total_HT' from 'c_commande_commun' where (month('c_commande_commun'.'DateCDA') = 1)) AS 'Janvier',(select sum('c_commande_commun'.'Total_HT') AS 'Total_HT' from 'c_commande_commun' where (month('c_commande_commun'.'DateCDA') = 2)) AS 'Février',(select sum('c_commande_commun'.'Total_HT') AS 'Total_HT' from 'c_commande_commun' where (month('c_commande_commun'.'DateCDA') = 3)) AS 'Mars',(select sum('c_commande_commun'.'Total_HT') AS 'Total_HT' from 'c_commande_commun' where (month('c_commande_commun'.'DateCDA') = 4)) AS 'Avril',(select sum('c_commande_commun'.'Total_HT') AS 'Total_HT' from 'c_commande_commun' where (month('c_commande_commun'.'DateCDA') = 5)) AS 'Mai',(select sum('c_commande_commun'.'Total_HT') AS 'Total_HT' from 'c_commande_commun' where (month('c_commande_commun'.'DateCDA') = 6)) AS 'Juin',(select sum('c_commande_commun'.'Total_HT') AS 'Total_HT' from 'c_commande_commun' where (month('c_commande_commun'.'DateCDA') = 7)) AS 'Juillet',(select sum('c_commande_commun'.'Total_HT') AS 'Total_HT' from 'c_commande_commun' where (month('c_commande_commun'.'DateCDA') = 8)) AS 'Août',(select sum('c_commande_commun'.'Total_HT') AS 'Total_HT' from 'c_commande_commun' where (month('c_commande_commun'.'DateCDA') = 9)) AS 'Septembre',(select sum('c_commande_commun'.'Total_HT') AS 'Total_HT' from 'c_commande_commun' where (month('c_commande_commun'.'DateCDA') = 10)) AS 'Octobre',(select sum('c_commande_commun'.'Total_HT') AS 'Total_HT' from 'c_commande_commun' where (month('c_commande_commun'.'DateCDA') = 11)) AS 'Novembre',(select sum('c_commande_commun'.'Total_HT') AS 'Total_HT' from 'c_commande_commun' where (month('c_commande_commun'.'DateCDA') = 12)) AS 'Décembre',year('c_commande_commun'.'DateCDA') AS 'DateCDA' from 'c_commande_commun' where (('c_commande_commun'.'est_commun' = 'C') and ('c_commande_commun'.'DateCDA' is not null)) group by year('c_commande_commun'.'DateCDA')
0
Re,

Oui mais dans mon cas précis (cf au dessus) les valeurs YEAR(date) et yd valent la même chose...du coup c'est pareil :(
0
Utilisateur anonyme
7 févr. 2014 à 17:02
Non, ce n'est pas pareil. As-tu essayé ?
0
Oui mais il m'affiche la même chose.
0
Re,

Ca marche finalement, il fallait faire un SELECT de YEAR(date) yd dans le début de la requête alors que je l'avais mis (dans mon exemple) à la fin.

Tu me sors une grosse épine du pied.

Un grand merci à toi et un tout bon we :)
0
Utilisateur anonyme
7 févr. 2014 à 17:43
De rien. Bon week-end à toi aussi
0
Re,

La toute première requête ne fonctionne pas car systématiquement il me remplace le nnom par a.nom donc dans ta première requête le père ca donne a.nom=a.nom ce qui équivaut à dire du tout :/

Pour info, mon boss m'impose l'utilisation de HeidiSQL

A savoir que si j'écris nom='valeurdanslabase' j'ai bien le bon résultat ;)
0
Utilisateur anonyme
10 févr. 2014 à 11:48
Si tu obtiens ce résultat, c'est que tu n'utilises pas les alias comme je te l'ai indiqué. Le principe est de faire dans les sous-requêtes, une comparaison entre la valeur du nom dans la sous-requête (sans alias) et du nom dans la requête globale (avec un alias). Ça ne peut pas donner a.nom=a.nom.
0
Re,

Oui oui bien sur j'utilise les alias mais HeidiSQL ma les remplace à chaque fois.

Si je mets select 'c_commande_commun'.'Designation' AS 'Nom' il va quand même m'écrire dans la vue WHERE select 'c_commande_commun'.'Designation'= select 'c_commande_commun'.'Designation' AS 'Nom' grrrr
0
Utilisateur anonyme
10 févr. 2014 à 12:06
Peux-tu montrer ta requête ?
0
Ma requête dans PhpMyAdmin est correcte mais pas dans HeidiSQL...La voic:


select 'c_commande_commun'.'Designation' AS 'Nom','c_commande_commun'.'Reference' AS 'Reference','c_commande_commun'.'Fournisseur' AS 'Fournisseur','c_commande_commun'.'EPH_cmp' AS 'EPH_cmp','c_commande_commun'.'EPH_cmp_detail' AS 'EPH_cmp_detail',year('c_commande_commun'.'DateCDA') AS 'Année',(select sum('c_commande_commun'.'Total_HT') AS 'Total_HT' from 'c_commande_commun' where ((year('c_commande_commun'.'DateCDA') = 'Année') and ('c_commande_commun'.'Designation' = 'Nom'))) AS 'Total_HT',(select sum('c_commande_commun'.'Total_HT') AS 'Total_HT' from 'c_commande_commun' where ((month('c_commande_commun'.'DateCDA') = 1) and (year('c_commande_commun'.'DateCDA') = 'Année') and ('c_commande_commun'.'Designation' = 'Nom'))) AS 'Janvier',(select sum('c_commande_commun'.'Total_HT') AS 'Total_HT' from 'c_commande_commun' where ((month('c_commande_commun'.'DateCDA') = 2) and (year('c_commande_commun'.'DateCDA') = 'Année') and ('c_commande_commun'.'Designation' = 'Nom'))) AS 'Février',(select sum('c_commande_commun'.'Total_HT') AS 'Total_HT' from 'c_commande_commun' where ((month('c_commande_commun'.'DateCDA') = 3) and (year('c_commande_commun'.'DateCDA') = 'Année') and ('c_commande_commun'.'Designation' = 'Nom'))) AS 'Mars',(select sum('c_commande_commun'.'Total_HT') AS 'Total_HT' from 'c_commande_commun' where ((month('c_commande_commun'.'DateCDA') = 4) and (year('c_commande_commun'.'DateCDA') = 'Année') and ('c_commande_commun'.'Designation' = 'Nom'))) AS 'Avril',(select sum('c_commande_commun'.'Total_HT') AS 'Total_HT' from 'c_commande_commun' where ((month('c_commande_commun'.'DateCDA') = 5) and (year('c_commande_commun'.'DateCDA') = 'Année') and ('c_commande_commun'.'Designation' = 'c_commande_commun'.'Designation'))) AS 'Mai',(select sum('c_commande_commun'.'Total_HT') AS 'Total_HT' from 'c_commande_commun' where ((month('c_commande_commun'.'DateCDA') = 6) and (year('c_commande_commun'.'DateCDA') = 'Année') and ('c_commande_commun'.'Designation' = 'Nom'))) AS 'Juin',(select sum('c_commande_commun'.'Total_HT') AS 'Total_HT' from 'c_commande_commun' where ((month('c_commande_commun'.'DateCDA') = 7) and (year('c_commande_commun'.'DateCDA') = 'Année') and ('c_commande_commun'.'Designation' = 'Nom'))) AS 'Juillet',(select sum('c_commande_commun'.'Total_HT') AS 'Total_HT' from 'c_commande_commun' where ((month('c_commande_commun'.'DateCDA') = 8) and (year('c_commande_commun'.'DateCDA') = 'Année') and ('c_commande_commun'.'Designation' = 'Nom'))) AS 'Août',(select sum('c_commande_commun'.'Total_HT') AS 'Total_HT' from 'c_commande_commun' where ((month('c_commande_commun'.'DateCDA') = 9) and (year('c_commande_commun'.'DateCDA') = 'Année') and ('c_commande_commun'.'Designation' = 'Nom'))) AS 'Septembre',(select sum('c_commande_commun'.'Total_HT') AS 'Total_HT' from 'c_commande_commun' where ((month('c_commande_commun'.'DateCDA') = 10) and (year('c_commande_commun'.'DateCDA') = 'Année') and ('c_commande_commun'.'Designation' = 'Nom'))) AS 'Octobre',(select sum('c_commande_commun'.'Total_HT') AS 'Total_HT' from 'c_commande_commun' where ((month('c_commande_commun'.'DateCDA') = 11) and (year('c_commande_commun'.'DateCDA') = 'Année') and ('c_commande_commun'.'Designation' = 'Nom'))) AS 'Novembre',(select sum('c_commande_commun'.'Total_HT') AS 'Total_HT' from 'c_commande_commun' where ((month('c_commande_commun'.'DateCDA') = 12) and (year('c_commande_commun'.'DateCDA') = 'Année') and ('c_commande_commun'.'Designation' = 'Nom'))) AS 'Décembre' from 'c_commande_commun' where (('c_commande_commun'.'est_commun' = 'C') and ('c_commande_commun'.'DateCDA' is not null)) group by year('c_commande_commun'.'DateCDA'),'c_commande_commun'.'Designation' order by 'c_commande_commun'.'Designation','c_commande_commun'.'DateCDA'

0
Utilisateur anonyme
10 févr. 2014 à 13:57
Qu'entends-tu par "Ma requête dans PhpMyAdmin est correcte" ? Elle donne le bon résultat ?
0
Oui tout à fait...
0
Utilisateur anonyme
10 févr. 2014 à 14:08
Alors le problème SQL est résolu.
Il te reste un problème de mode d'emploi de HeidiSQL, et comme je ne connais pas, je ne peux pas t'aider. Tu es vraiment sûr qu'il n'y a pas un mode de saisie qui te permet de mettre directement ta requête en SQL avec ce logiciel ?
0