mysql - Combining two GROUP BY queries grouped by the same column -
mysql - Combining two GROUP BY queries grouped by the same column -
i have transaction table, , tag table. want sum of transactions in transaction table grouped tag. there 2 different types of transaction: "budget" , "actual"
this query homecoming me want "budget" transactions:
select tg.name tag, sum(amount) budgettotal transaction tx bring together transaction_tag tt on tt.transaction_id = tx.id bring together tag tg on tg.id = tt.tag_id tx.type = "budget" , tx.date >= '2011-07-15' , tx.date < '2011-08-15' grouping tg.name
and of course of study pretty much same query "actual" transactions:
select tg.name tag, sum(amount) actualtotal transaction tx bring together transaction_tag tt on tt.transaction_id = tx.id bring together tag tg on tg.id = tt.tag_id tx.type = "actual" , tx.date >= '2011-07-15' , tx.date < '2011-08-15' grouping tg.name
my question: how grouping results of these 2 queries, one, 1 results table 3 columns: tag, budgettotal , actualtotal?
try this:
select tg.name, case when tx.type = "actual" sum(amount) end actualtotal, case when tx.type = "budget" sum(amount) end budgettotal from.... tx.type in ("actual", "budget") , .... grouping tg.name
mysql sql
Comments
Post a Comment