Question

Could you please help me with my problem? I am using xampp server with mysql database.

I have a table that looks like this:

|----|------------|--------------|-------------|------------|----------|
| id | period     | category     | subcategory | amount     | currency |
|----|------------|--------------|-------------|------------|----------|
|  1 | 2014-01-01 | Category One | SubCat One  |     325.48 | EUR      |
|  2 | 2014-01-01 | Category One | SubCat One  |    1680.20 | GBP      |
|  3 | 2014-01-01 | Category One | SubCat Two  |     229.78 | EUR      |
|  4 | 2014-01-01 | Category One | SubCat Two  |    1152.41 | GBP      |
|  5 | 2014-01-01 | Category Two | SubCat One  |     417.92 | EUR      |
|  6 | 2014-01-01 | Category Two | SubCat One  |     980.64 | GBP      |
|----|------------|--------------|-------------|------------|----------|

I need to get the result that items will be grouped by three fields (period, category, subcategory) and dynamic columns with each currency will be added to each group. Something like this:

|------------|--------------|-------------|--------|---------|
| period     | category     | subcategory | EUR    | GBP     |
|------------|--------------|-------------|--------|---------|
| 2014-01-01 | Category One | SubCat One  | 325.48 | 1680.20 |
| 2014-01-01 | Category One | SubCat Two  | 229.78 | 1152.41 |
| 2014-01-01 | Category Two | SubCat One  | 417.92 |  980.64 |
|------------|--------------|-------------|--------|---------|

What sql query should I run against the table to get the desired result?

The trick is that the query should automatically manage many currencies, so when I add new records with new currency, another column will be dynamically created in my result recordset.

Table after adding rows 7 and 8 with new currency (USD).

|----|------------|--------------|-------------|------------|----------|
| id | period     | category     | subcategory | amount     | currency |
|----|------------|--------------|-------------|------------|----------|
|  1 | 2014-01-01 | Category One | SubCat One  |     325.48 | EUR      |
|  2 | 2014-01-01 | Category One | SubCat One  |    1680.20 | GBP      |
|  3 | 2014-01-01 | Category One | SubCat Two  |     229.78 | EUR      |
|  4 | 2014-01-01 | Category One | SubCat Two  |    1152.41 | GBP      |
|  5 | 2014-01-01 | Category Two | SubCat One  |     417.92 | EUR      |
|  6 | 2014-01-01 | Category Two | SubCat One  |     980.64 | GBP      |
|----|------------|--------------|-------------|------------|----------|
|  7 | 2014-01-01 | Category One | SubCat One  |    2525.50 | USD      |
|  8 | 2014-01-02 | Category One | SubCat One  |     700.12 | USD      |
|----|------------|--------------|-------------|------------|----------|

Result (another currency column created dynamically).

|------------|--------------|-------------|--------|---------|---------|
| period     | category     | subcategory | EUR    | GBP     | USD     |
|------------|--------------|-------------|--------|---------|---------|
| 2014-01-01 | Category One | SubCat One  | 325.48 | 1680.20 | 2525.50 |
| 2014-01-01 | Category One | SubCat Two  | 229.78 | 1152.41 |       0 |
| 2014-01-01 | Category Two | SubCat One  | 417.92 |  980.64 |       0 |
| 2014-01-02 | Category One | SubCat One  |      0 |       0 |  700.12 |
|------------|--------------|-------------|--------|---------|---------|

Is there any simple way I can achieve this using sql query? Or would that required some php programming to prepare the results in desired way?

I appreciate your help,

Thanks

Was it helpful?

Solution 2

I guess I just answered my own question. :) I need to dynamically create SQL query, basing on all values in currency field. With php (using codeigniter) this can be achieved like this:

Step 1. I need to find out, what currencies I have in my table

<?php
$curr_sql = "SELECT DISTINCT currency FROM `table` ORDER BY currency ASC";
$curr_query = $this->db->query($curr_sql);
/* 
|----------|
| currency |
|----------|
| EUR      |
| GBP      |
| USD      |
|----------|
*/
?>

Step 2. I need to prepare sql query (adding specific column for each existing currency).

<?php
$sql = "SELECT period, category, subcategory";
 foreach ($curr_query->result() as $row) {
  $curr = $row->currency;
  $sql .= ", SUM(IF(currency = '" . $curr . "', amount, 0)) AS '" . $curr . "'";
 }
$sql .= " FROM `table` GROUP BY period, category, subcategory";

/* This is the sql I will get:
SELECT period, category, subcategory
    , SUM(IF(currency = 'EUR', amount, 0)) AS 'EUR'
    , SUM(IF(currency = 'GBP', amount, 0)) AS 'GBP'
    , SUM(IF(currency = 'USD', amount, 0)) AS 'USD'
FROM `table`
GROUP BY period, category, subcategory;
*/
?>

Step 3. Now I just execute my dynamically prepared query.

<?php
$myResult = $this->db->query($sql);

/* This is the result I get
|------------|--------------|-------------|--------|---------|---------|
| period     | category     | subcategory | EUR    | GBP     | USD     |
|------------|--------------|-------------|--------|---------|---------|
| 2014-01-01 | Category One | SubCat One  | 325.48 | 1680.20 | 2525.50 |
| 2014-01-01 | Category One | SubCat Two  | 229.78 | 1152.41 |       0 |
| 2014-01-01 | Category Two | SubCat One  | 417.92 |  980.64 |       0 |
| 2014-01-02 | Category One | SubCat One  |      0 |       0 |  700.12 |
|------------|--------------|-------------|--------|---------|---------|
*/
?>

I think that works for me. Thanks!

OTHER TIPS

You can Create Store Procedure, May be Below link would helpful for it

How to select column names dynamically in mySQL

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top