Note that there are some explanatory texts on larger screens.

plurals
  1. PO1 year monthly summary with share percentage
    primarykey
    data
    text
    <p>I'm trying to make an advance report for my app. And now i'm facing a trouble with some query.</p> <p>Here's the details:</p> <p>table_product</p> <pre><code> id | name -----+--------- 1 | Book 2 | Pen 3 | Ruler 4 | Calendar 5 | Scissors </code></pre> <p>table_event</p> <pre><code> id | name -----+--------- 1 | Launching 2 | Sale 3 | Roadshow 4 | Presentation 5 | Normal </code></pre> <p>table_transaction</p> <pre><code>---+------------+-------+-----------+---------+-----------+---------+-----------+---------+----------+ id | date | event | product_1 | share_1 | product_2 | share_2 | product_3 | share_3 | amount | ---+------------+-------+-----------+---------+-----------+---------+-----------+---------+----------+ 1 | 2013-01-12 | 1 | 3 | 50 | 2 | 50 | | | 5000 | 2 | 2013-02-13 | 3 | 2 | 50 | 1 | 25 | 4 | 25 | 10000 | 3 | 2013-01-13 | 5 | 5 | 50 | 4 | 50 | | | 7000 | 4 | 2013-04-13 | 2 | 1 | 100 | | | | | 5000 | ---+------------+-------+-----------+---------+-----------+---------+-----------+---------+----------+ </code></pre> <p>What I want to display is like this</p> <pre><code>--------------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+ Product Name | Jan | Feb | Mar | Apr | May | Jun | Jul | Aug | Sep | Oct | Nov | Dec | --------------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+ Book | | 2500 | | 5000 | | | | | | | | | Pen | 2500 | 5000 | | | | | | | | | | | Ruler | 2500 | | | | | | | | | | | | Calendar | 3500 | 2500 | | | | | | | | | | | Scissors | 3500 | | | | | | | | | | | | --------------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+ </code></pre> <p>What I have tried: I successfully generate the output for event name grouping with following query</p> <pre><code>SELECT t1.name as sort_name, sum(IF(YEAR(t2.date) = 2013 AND MONTH(t2.date) = 1, t2.amount, 0)) as total_jan, sum(IF(YEAR(t2.date) = 2013 AND MONTH(t2.date) = 2, t2.amount, 0)) as total_feb, sum(IF(YEAR(t2.date) = 2013 AND MONTH(t2.date) = 3, t2.amount, 0)) as total_mar, sum(IF(YEAR(t2.date) = 2013 AND MONTH(t2.date) = 4, t2.amount, 0)) as total_apr, sum(IF(YEAR(t2.date) = 2013 AND MONTH(t2.date) = 5, t2.amount, 0)) as total_may, sum(IF(YEAR(t2.date) = 2013 AND MONTH(t2.date) = 6, t2.amount, 0)) as total_jun, sum(IF(YEAR(t2.date) = 2013 AND MONTH(t2.date) = 7, t2.amount, 0)) as total_jul, sum(IF(YEAR(t2.date) = 2013 AND MONTH(t2.date) = 8, t2.amount, 0)) as total_aug, sum(IF(YEAR(t2.date) = 2013 AND MONTH(t2.date) = 9, t2.amount, 0)) as total_sep, sum(IF(YEAR(t2.date) = 2013 AND MONTH(t2.date) = 10, t2.amount, 0)) as total_oct, sum(IF(YEAR(t2.date) = 2013 AND MONTH(t2.date) = 11, t2.amount, 0)) as total_nov, sum(IF(YEAR(t2.date) = 2013 AND MONTH(t2.date) = 12, t2.amount, 0)) as total_dec, sum(IF(YEAR(t2.date) = 2013, t2.amount, 0)) as event_total FROM events t1 LEFT JOIN transactions t2 ON t2.event_id = t1.id GROUP BY sort_name </code></pre> <p>But I don't have any idea to do group it by product. I've tried playing with union but still return a SQL syntax error.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload