简体   繁体   English

如何进行按名称计数、求和和分组的查询?

[英]how to make a query to count, sum, and group by name?

I want to make a report like this, but I'm having trouble making the query.我想制作这样的报告,但在进行查询时遇到问题。

no merk默克 tahun塔洪 departemen部下 lokasi洛卡西 history 2020历史 2020 history 2021历史 2021 Total (2020+2021)总计(2020+2021)
1 1 machine A机器A 2014 2014年 Production生产 LA 1洛杉矶 1 1 1 1 1 2 2
2 2 machine B机器B 2019 2019年 Production生产 LA 2洛杉矶 2 0 0 2 2 2 2

and here's my codes for make table.这是我制作表格的代码。

<table id="myTable" width="100%" class="table table-striped table-bordered table-hover" id="dataTables-example">
                    <thead>
                        <tr>
                          <th>NO</th>
                          <th>MERK</th>
                          <th>TAHUN</th>
                          <th>DEPARTEMEN</th>
                          <th>LOKASI</th>
                          <th>HISTORY 2020 (X)</th>
                          <th>HISTORY 2021 (X)</th>
                          <th>TOTAL</td>
                        </tr>
                    </thead>
                    <tbody>
                      <?php
                      $sql    = "SELECT merk_tmf,tahun_tmf,dept_tmf,lokasi_tmf FROM tb_tmf GROUP BY merk_tmf";
                      $data20 = mysqli_query($connect,"SELECT merk_tmf FROM tb_tmf WHERE YEAR(waktu_tmf) = 2020 GROUP BY merk_tmf");
                      $data21 = mysqli_query($connect,"SELECT merk_tmf FROM tb_tmf WHERE YEAR(waktu_tmf) = 2021 GROUP BY merk_tmf");
                      $jumlah = mysqli_query($connect,"SELECT merk_tmf FROM tb_tmf GROUP BY merk_tmf");
                      
                      $jumlah20 = mysqli_num_rows($data20);
                      $jumlah21 = mysqli_num_rows($data21);
                      $penjumlahan = mysqli_num_rows($jumlah);

                      $no= 1;
                      $query = $connect->query($sql);
                      while($row = $query->fetch_assoc()){
                        echo 
                            "<tr>
                                <td>". $no++ ."</td>
                                <td>".$row['merk_tmf']."</td>
                                <td>".$row['tahun_tmf']."</td>
                                <td>".$row['dept_tmf']."</td>
                                <td>".$row['lokasi_tmf']."</td>
                                <td>".$jumlah20."</td>
                                <td>".$jumlah21."</td>
                                <td>".$penjumlahan."</td>
                            </tr>";
                            }
                        ?>
                    </tbody>
                </table>

that data is from this table.该数据来自该表。

waktu_tmf waktu_tmf merk_tmf merk_tmf tahun_tmf tahun_tmf dept_tmf dept_tmf lokasi_tmf lokasi_tmf
2020-06-29 2020-06-29 machine A机器A 1987 1987年 Production生产 LA 1洛杉矶 1
2021-06-29 2021-06-29 machine A机器A 1987 1987年 Production生产 LA 1洛杉矶 1
2021-06-29 2021-06-29 machine B机器B 1990 1990年 Production生产 LA 2洛杉矶 2
2021-06-29 2021-06-29 machine B机器B 1990 1990年 Production生产 LA 2洛杉矶 2

I know that there is an error in the query, because I can't make it right.我知道查询中有错误,因为我做对了。

Try to run this, I hope this can give you idea...尝试运行这个,我希望这可以给你想法......

    select merk_tmf merk, tahun_tmf tahun, dept_tmf departemen, lokasi_tmf lokasi,
          sum(case when date_format(waktu_tmf, '%Y') = 2020 then 1 else 0 end) 'history 2020', 
          sum(case when date_format(waktu_tmf, '%Y') = 2021 then 1 else 0 end) 'history 2021', 
          sum((case when date_format(waktu_tmf, '%Y') = 2020 then 1 else 0 end) +
          (case when date_format(waktu_tmf, '%Y') = 2021 then 1 else 0 end)) 'Total (2020+2021)'
    from tb_tmf 
    group by merk_tmf, lokasi_tmf

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM