MySQL Forums
Forum List  »  French

Grouper 2 requêtes SQL
Posted by: Michel GOARIN
Date: November 28, 2018 01:02PM

Bonjour,
Je souhaiterai établir sous une même requête les deux requêtes suivantes :
<code>
SELECT `FAMILLE`, SUM(`COUT`) AS `montantEntreeParFamille`, SUM(`QTT`) AS `nbrEntreeParFamille` FROM `mouvement` WHERE `MOUVEMENT` LIKE "%E%" AND (DATE_MOUVEMENT BETWEEN "2018-01-01" AND "2018-11-27") GROUP BY `FAMILLE`

SELECT `FAMILLE`, SUM(`COUT`) AS `montantSortieParFamille`, SUM(`QTT`) AS `nbrSortieParFamille` FROM `mouvement` WHERE `MOUVEMENT` LIKE "%S%" AND (DATE_MOUVEMENT BETWEEN "2018-01-01" AND "2018-11-27") GROUP BY `FAMILLE`
</code>

J'ai trouvé une solution :

<code>
SELECT `FAMILLE`
SUM(CASE WHEN `MOUVEMENT` LIKE "%E%" THEN `COUT` ELSE 0 END) AS `montantEntreeParFamille`,
SUM(CASE WHEN `MOUVEMENT` LIKE '%E%' THEN `QTT` ELSE 0 END) AS `nbrEntreeParFamille`,
SUM(CASE WHEN `MOUVEMENT` LIKE '%S%' THEN `COUT` ELSE 0 END) AS `montantSortieParFamille`,
SUM(CASE WHEN `MOUVEMENT` LIKE '%S%' THEN `QTT` ELSE 0 END) AS `nbrSortieParFamille`

FROM `MOUVEMENT`

WHERE (`MOUVEMENT` LIKE '%E%'
OR `MOUVEMENT` LIKE '%S%')
AND (`DATE_MOUVEMENT` BETWEEN '2018-01-01' AND '2018-11-27')

GROUP BY `FAMILLE`
</code>

Seulement j'ai une erreur :
<code>
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'sum(case when `mouvement` like '%E%' then `COUT` else 0 end) as `montantEntreePa' at line 2
</code>

Auriez vous une solution ?

Je compte sur vous...ça fait quelques jours que je suis bloqué sur le sujet.

Options: ReplyQuote


Subject
Views
Written By
Posted
Grouper 2 requêtes SQL
1323
November 28, 2018 01:02PM
746
November 29, 2018 12:03AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.