1

Please help me to solve a problem with 2 tables:

  1. tbl_data
  2. tbl_shipped

tbl_data:

id_data nama category
110 Singkong Asin
111 Ubi Pedas
112 Pisang Manis
113 Bawang Asin
114 Tempe Pedas Manis
115 Singkong Pedas
116 Singkong Manis
117 Singkong Pedas Manis
118 Ubi Pedas Manis
119 Ubi Asin
120 Pisang Pedas

tbl_shipped:

id id_data date amount
1 110 2021-05-19 40
2 111 2021-05-21 31
3 112 2021-05-29 80
4 112 2021-06-19 50
5 112 2021-06-22 5
6 112 2021-07-30 23
7 113 2021-08-05 70
8 113 2021-08-13 28
9 114 2021-09-04 8
10 115 2021-11-10 67
11 115 2021-12-20 1
12 117 2021-05-19 12
13 117 2021-06-22 50
14 117 2021-08-05 21
15 120 2021-09-04 71
16 120 2021-11-10 53

Expected result:

nama category 2021-05-19 2021-05-21 2021-05-29 2021-06-19 2021-06-22 2021-07-30 2021-08-05 2021-08-13 2021-09-04 2021-11-10 2021-12-20 Total
Singkong Asin 40 40
Ubi Pedas 31 31
Pisang Manis 80 50 5 23 158
Bawang Asin 70 28 98
Tempe Pedas Manis 8 8
Singkong Pedas 67 1 68
Singkong Pedas Manis 12 50 21 83
Pisang Pedas 71 53 60
All Total : 52 31 80 50 55 23 91 28 15 120 1 546

If expected results are a pivot like on the table in the above picture, how can it be done?

This is my code (From request) which I have tried, but not optimal :

<table>
<thead>
    <?php include "conn.php";
        $result = $connect->query("SELECT * FROM tbl_shipped LEFT JOIN tbl_data ON tbl_data.id_data = tbl_shipped.id_data ORDER BY tbl_shipped.date ASC"); // query-in database
        $data = array();
        $dates = array();
        while ($row = $result->fetch_array()) { // fetching result
            if(!isset($data[$row['id_data']][$row['date']])) {
                $data[$row['id_data']][$row['date']] = array();
            }     
        
                if(!in_array($row['date'], $dates)) {
                    $dates[] = $row['date'];
                }
            
            $data[$row['id_data']][$row['date']] = $row['amount'];
        }
    ?>
    <tr>
        <th></th>
        <?php foreach($dates AS $date) : ?>
        <th><?=$date?></th>
        <?php endforeach; ?>
    </tr>                                            
</thead>
<tbody>
    <?php foreach($data AS $id => $entries) : ?> 
    <tr>     
        <td><?=$id?></th>                                                              
        <?php foreach($dates AS $date) : ?>
        <td><?=(isset($data[$id][$date])?$data[$id][$date]:'')?></td>
        <?php endforeach; ?>
    </tr>
    <?php endforeach; ?>
</tbody>
<tfoot>

</tfoot>
</table>

Based on sorce code on above, this is result:

2021-05-19 2021-05-21 2021-05-29 2021-06-19 2021-06-22 2021-07-30 2021-08-05 2021-08-13 2021-09-04 2021-11-10 2021-12-20
110 40
111 31
112 80 50 5 23
113 70 28
114 8
115 67 1
117 12 50 21
120 7 53

Please help me. And how should i write to add code, i hope get result as expected. Thank you.

Imam aqrom
  • 11
  • 3
  • 1
    Have you made any attempt to research this or write any code yourself? We're happy to help people here but it's not a free do-my-work service. I'm pretty certain you are not the first person to ask how to pivot their data... – ADyson Jul 23 '21 at 12:48
  • 3
    And please don't post images. We're happy to help but a textual form of your data sample is much more quicker to re-create than re-writing everything that's on your images. According to your images, you already have them in ASCII table format so why don't you just copy and paste that [into your question](https://stackoverflow.com/posts/68499358/edit) – FanoFN Jul 23 '21 at 12:50
  • Does this answer your question? [How can I return pivot table output in MySQL?](https://stackoverflow.com/questions/7674786/how-can-i-return-pivot-table-output-in-mysql) – FanoFN Jul 24 '21 at 03:02
  • Oke thanks for your opinion @ADyson so, my code still erorr in the code app. – Imam aqrom Jul 26 '21 at 10:56
  • Thanks for your reply and share link with me, but it`s difference case to my problem @FaNo_FN and then, I have changed format be a table. – Imam aqrom Jul 26 '21 at 10:58
  • 1
    If you have some code, please share it with us, and include some information about what it outputs currently, whether that is some error message, or just the wrong results or whatever. That will at least give us a starting point. If you are having trouble including it in your question then a) read https://stackoverflow.com/help/formatting and b) ensure you include sufficient description as I've just mentioned, because the editor does not like code-only posts - it tries to encourage you to explain your problem as well as just provide the code. – ADyson Jul 26 '21 at 11:45
  • Ok, Let see it @ADyson, i have updating the form. – Imam aqrom Jul 28 '21 at 07:29

1 Answers1

1

I can at least think of one way using prepared statement. Consider this option:

SET @sql := NULL;
SET @columns := NULL;

SET @columns := (SELECT GROUP_CONCAT(
                                 CONCAT("SUM(CASE WHEN `date`='",dates,"' 
                                                  THEN amount 
                                                   ELSE 0 END) AS '",dates,"'") SEPARATOR ',') 
                  FROM (SELECT DISTINCT `date` AS dates FROM tbl_shipped) dt);

SET @sql := CONCAT("SELECT td.id_data,
                           td.nama,
                            td.category,",@columns,"
                      FROM tbl_shipped AS ts 
                      JOIN tbl_data AS td ON ts.id_data=td.id_data
                  GROUP BY td.id_data, td.nama, td.category;");

PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

I'm using @sql as a variable for the final query structure and @columns as a variable for the columns structures. It can be just one variable but I think this is easier to read and edit.

Demo fiddle

FanoFN
  • 6,815
  • 2
  • 13
  • 33
  • Thanks for your answer participation @FaNo_FN , I have seen your code (Demo) my opinion seems logical, but, Sorry i`m beginner for used mysql, how if we writing script inside php on the tabel to show the data, as like my source code above. Please help me for the feedback, thanks. – Imam aqrom Jul 28 '21 at 07:27
  • 1
    @Imamaqrom you could put this SQL code into a stored procedure which you can then execute from PHP. Since (per the demo) this SQL already produces the exact table you need, your PHP code would simply need to loop through the results and output them as a HTML table, with no complicated processing. – ADyson Jul 28 '21 at 07:59