par
sadeq » 30 août 2013, 23:11
Salut, je viens de voir ta question. Voici une résolution par SQL:
-- phpMyAdmin SQL Dump
-- version 4.0.4
-- http://www.phpmyadmin.net
--
-- Client: localhost
-- Généré le: Ven 30 Août 2013 à 20:37
-- Version du serveur: 5.5.24-log
-- Version de PHP: 5.4.3
--
-- Base de données: `test`
--
-- --------------------------------------------------------
--
-- Structure de la table `t_cotisations`
--
CREATE TABLE IF NOT EXISTS `t_cotisations` (
`nom` varchar(50) NOT NULL,
`prenom` varchar(50) NOT NULL,
`annee` int(11) NOT NULL,
`montant` float NOT NULL,
`etat` int(11) NOT NULL,
`ID` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=11 ;
--
-- Contenu de la table `t_cotisations`
--
INSERT INTO `t_cotisations` (`nom`, `prenom`, `annee`, `montant`, `etat`, `ID`) VALUES
('Nom 1', 'Prénom 1', 2009, 2000, 1, 2),
('Nom 2', 'Prénom 2', 2009, 2000, 1, 3),
('Nom 3', 'Prénom 3', 2009, 2000, 1, 4),
('Nom 1', 'Prénom 1', 2010, 2000, 2, 5),
('Nom 2', 'Prénom 2', 2010, 2000, 2, 6),
('Nom 3', 'Prénom 3', 2010, 2000, 1, 7),
('Nom 1', 'Prénom 1', 2011, 2000, 1, 8),
('Nom 2', 'Prénom 2', 2011, 2000, 1, 9),
('Nom 3', 'Prénom 3', 2011, 2000, 2, 10);
--
-- Structure de la vue `v_cotis_par_annee` : Calcul du montant par rapport a l'etat
--
CREATE VIEW `v_cotis_par_annee` AS
SELECT `t_cotisations`.`nom` AS `nom`,`t_cotisations`.`prenom` AS `prenom`,`t_cotisations`.`annee` AS `annee`,
if((`t_cotisations`.`etat` = 1),0,`t_cotisations`.`montant`) AS `montant`
FROM `t_cotisations`
ORDER BY `t_cotisations`.`annee`,`t_cotisations`.`nom`;
--
-- Structure de la vue `v_cotis_croisees_par_annee` : Table croisee entre noms et annees donnant les montants par annee et par nom
--
CREATE VIEW `v_cotis_croisees_par_annee` AS
SELECT `nom`,`prenom`,
sum(if((`annee` = 2009),`montant`,0)) as `2009`,
sum(if((`annee` = 2010),`montant`,0)) as `2010`,
sum(if((`annee` = 2011),`montant`,0)) as `2011`
FROM `v_cotis_par_annee`
GROUP BY `nom`,`prenom`;
--
-- Affichage de la vue `v_cotis_croisees_par_annee`
--
SELECT FROM `v_cotis_croisees_par_annee`
Résultat:
nom____prenom_____2009_____2010_____2011
Nom 1____Prénom 1______0________2000_______0
Nom 2____Prénom 2______0________2000_______0
Nom 3____Prénom 3______0_________0________2000
Salut, je viens de voir ta question. Voici une résolution par SQL:
[sql]-- phpMyAdmin SQL Dump
-- version 4.0.4
-- http://www.phpmyadmin.net
--
-- Client: localhost
-- Généré le: Ven 30 Août 2013 à 20:37
-- Version du serveur: 5.5.24-log
-- Version de PHP: 5.4.3
--
-- Base de données: `test`
--
-- --------------------------------------------------------
--
-- Structure de la table `t_cotisations`
--
CREATE TABLE IF NOT EXISTS `t_cotisations` (
`nom` varchar(50) NOT NULL,
`prenom` varchar(50) NOT NULL,
`annee` int(11) NOT NULL,
`montant` float NOT NULL,
`etat` int(11) NOT NULL,
`ID` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=11 ;
--
-- Contenu de la table `t_cotisations`
--
INSERT INTO `t_cotisations` (`nom`, `prenom`, `annee`, `montant`, `etat`, `ID`) VALUES
('Nom 1', 'Prénom 1', 2009, 2000, 1, 2),
('Nom 2', 'Prénom 2', 2009, 2000, 1, 3),
('Nom 3', 'Prénom 3', 2009, 2000, 1, 4),
('Nom 1', 'Prénom 1', 2010, 2000, 2, 5),
('Nom 2', 'Prénom 2', 2010, 2000, 2, 6),
('Nom 3', 'Prénom 3', 2010, 2000, 1, 7),
('Nom 1', 'Prénom 1', 2011, 2000, 1, 8),
('Nom 2', 'Prénom 2', 2011, 2000, 1, 9),
('Nom 3', 'Prénom 3', 2011, 2000, 2, 10);
--
-- Structure de la vue `v_cotis_par_annee` : Calcul du montant par rapport a l'etat
--
CREATE VIEW `v_cotis_par_annee` AS
SELECT `t_cotisations`.`nom` AS `nom`,`t_cotisations`.`prenom` AS `prenom`,`t_cotisations`.`annee` AS `annee`,
if((`t_cotisations`.`etat` = 1),0,`t_cotisations`.`montant`) AS `montant`
FROM `t_cotisations`
ORDER BY `t_cotisations`.`annee`,`t_cotisations`.`nom`;
--
-- Structure de la vue `v_cotis_croisees_par_annee` : Table croisee entre noms et annees donnant les montants par annee et par nom
--
CREATE VIEW `v_cotis_croisees_par_annee` AS
SELECT `nom`,`prenom`,
sum(if((`annee` = 2009),`montant`,0)) as `2009`,
sum(if((`annee` = 2010),`montant`,0)) as `2010`,
sum(if((`annee` = 2011),`montant`,0)) as `2011`
FROM `v_cotis_par_annee`
GROUP BY `nom`,`prenom`;
--
-- Affichage de la vue `v_cotis_croisees_par_annee`
--
SELECT FROM `v_cotis_croisees_par_annee`[/sql]
Résultat:
[b]nom____prenom_____2009_____2010_____2011[/b]
Nom 1____Prénom 1______0________2000_______0
Nom 2____Prénom 2______0________2000_______0
Nom 3____Prénom 3______0_________0________2000