Advertisement
Guest User

Untitled

a guest
Jun 27th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 KB | None | 0 0
  1. category || tran_type || Jan_total || feb_total || mar_total
  2. A || debit || $101 || $201 || $302
  3. A || credit || $500 || $600 || $200
  4.  
  5. SELECT category
  6. ,tran_type
  7. ,SUM(IF(month(date) = 1,ABS(amount),0)) as jan_total
  8. ,SUM(IF(month(date) = 2,ABS(amount),0)) as feb_total
  9. ,SUM(IF(month(date) = 3,ABS(amount),0)) as mar_total
  10. ,SUM(IF(month(date) = 4,ABS(amount),0)) as apr_total
  11. ,SUM(IF(month(date) = 5,ABS(amount),0)) as may_total
  12. ,SUM(IF(month(date) = 6,ABS(amount),0)) as jun_total
  13. ,SUM(IF(month(date) = 7,ABS(amount),0)) as jul_total
  14. ,SUM(IF(month(date) = 8,ABS(amount),0)) as aug_total
  15. ,SUM(IF(month(date) = 9,ABS(amount),0)) as sep_total
  16. ,SUM(IF(month(date) = 10,ABS(amount),0)) as okt_total
  17. ,SUM(IF(month(date) = 11,ABS(amount),0)) as nov_total
  18. ,SUM(IF(month(date) = 12,ABS(amount),0)) as dec_total
  19. FROM transactions
  20. WHERE YEAR(date) = '2011'
  21. GROUP BY category, tran_type
  22.  
  23. SELECT category, YEAR(date), tran_type,
  24.  
  25. (SELECT SUM(amounts) FROM TableName s1 WHERE YEAR(t.date)=YEAR(s1.date) AND MONTH(s1.date)=1 AND t.category=s1.category AND t.tran_type=s1.tran_type) AS 'Jan_Total',
  26.  
  27. (SELECT SUM(amounts) FROM TableName s2 WHERE YEAR(t.date)=YEAR(s2.date) AND MONTH(s2.date)=2 AND t.category=s2.category AND t.tran_type=s2.tran_type) AS 'Feb_Total',
  28.  
  29. ....REPEAT ABOVE 2 LINES FOR EACH MONTH
  30.  
  31. FROM TableName t
  32. WHERE t.date>'2011-01-01'
  33. GROUP BY t.category, YEAR(t.date), t.tran_type;
  34.  
  35. SELECT category, YEAR(date), MONTH(date), tran_type, SUM(amounts)
  36. FROM TableName
  37. GROUP BY TableName;
  38.  
  39. SELECT category, tran_type, SUM(amount_spent) AS total FROM myTable WHERE date>="2011-01-01" AND date<"2011-02-01" GROUP BY category, tran_type;
  40.  
  41. SELECT jan.category, jan.tran_type, jan.total, feb.total FROM
  42. (SELECT category, tran_type, SUM(amount_spent) AS total FROM myTable WHERE date>="2011-01-01" AND date<"2011-02-01" GROUP BY category, tran_type) AS jan,
  43. (SELECT category, tran_type, SUM(amount_spent) AS total FROM myTable WHERE date>="2011-02-01" AND date<"2011-03-01" GROUP BY category, tran_type) AS feb
  44. WHERE jan.category = feb.category
  45. AND jan.tran_type = feb.tran_type;
  46.  
  47. SELECT category, tran_type, MONTH(date) AS theMonth, SUM(amount_spent) AS total FROM myTable WHERE date>="2011-01-01" AND date<"2012-01-01" GROUP BY category, tran_type, theMonth;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement