dimanche 28 juin 2015

SQL SUM of a column as field inside a joined query

Im trying to get the sum of a column inside the following query but when this sum is present it only returns one column inside the while loop. Where am I getting it wrong?

SELECT *,
                    t.time AS t_time,
                    c.time AS c_time,
                    c.name AS category,
                    SUM(t.amount) AS totalExp


                FROM transaction            

                JOIN category c
                ON c.cid = t.cid

                WHERE t.state = 'a'
                ORDER BY t_time DESC

                LIMIT 50

I want this to return the same results it would would return without the SUM but with an extra field as a sum of the t.amount column. Can I also put a "WHERE" on the SUM so can do something like WHERE t.type = '1'; ?

Thanks.

Aucun commentaire:

Enregistrer un commentaire