对基于行的值求和,然后将该值除以列

时间:2019-01-02 18:13:09

标签: php database sum

我需要基于行的sum(values)帮助。

所以我有这样的数据

enter image description here

我想像这样基于行求和

enter image description here

之后,我想根据总和(行)对数据进行划分

所以没有1 [harga]会是这样(结果= 1 / 4.6),依此类推 也许有人可以帮助我?

<table class="table table-bordered">
       <tr>
          <th>No</th>
          <th>Alternatif</th>
             <?php
                $dd = $this->Mymod->ViewData('kriteria'); 
                foreach($dd as $i):
             ?>
          <th><?= $i['kriteria_nama']; ?></th>
             <?php endforeach; ?>
        </tr>
             <?php
                $gc= $this->db->query("SELECT * from alternatif")->result_array();
                $dbobot=array();
                $no=0;
                foreach ($gc as $cc):
                $no++;
             ?>
          <tr>
          <td><?= $no; ?></td>
          <td><?= $cc['alternatif_nama']; ?></td>
             <?php
                $de= $this->db->query("SELECT * from nilai where alternatif_kode = '$cc[alternatif_kode]'")->result_array();
                foreach ($de as $cdd):
                $ef= $this->db->query("SELECT max(nilai_nilai) as maxNilaiK  from nilai where kriteria_kode = '$cdd[kriteria_kode]'")->row_array();
                $Mnormal = $cdd['nilai_nilai'] / $ef['maxNilaiK']
             ?>
          <td><?= $Mnormal; ?></td>
          <?php
          endforeach; ?>
          </tr>
          <?php 
          endforeach;
          ?>
    </table> 

更新:所以根据我的图像我有表。在我的桌子上有No,ALternatif,Harga,Kapasitas,Luas,Jenis,Fasilitas和Jarak。每行都有数据。

1. arrayGedungA ([1],   [0.25], [1],   [0.4], [0.25], [1]);
2. arrayGedungB ([0.4], [1],    [0.4], [1],   [1],    [0.8]);
3. arrayGedungC ([0.2], [1],    [0.4], [1],   [0.5],  [0.8]);
4. arrayGedungD ([0.4], [1],    [0.8], [1],   [1],    [0.4]);
5. arrayGedungE ([1],   [1],    [0.8], [0.2], [0.5],  [0.8]);
6. arrayGedungF ([0.8], [1],    [0.4], [1],   [0.5],  [0.8]);
7. arrayGedungG ([0.8], [0.5],  [0.4], [1],   [0.5],  [0.2]);

我的问题是

  1. 我想从上到下总结harga,kapasitas,luas,jenis,fasilitas和jarak。

所以数据将像这样

Harga = (1 + 0.4 + 0.2 + 0.4 + 1 + 0.8 + 0.8) is 4.6
Kapasitas = (0.25 + 1 + 1 + 1 + 1 + 1 + 0.5) is 5.75
Luas = (1 + 0.4 + 0.4 + 0.8 + 0.8 + 0.4 + 0.4) is 4.2
Jenis = (0.4 + 1 + 1 + 1 + 0.2 + 1 + 1) is 5.6
Fasilitas = (0.25 + 1 + 0.5 + 1 + 0.5 + 0.5 + 0.5) is 4.25
Jarak = (1 + 0.8 + 0.8 + 0.4 + 0.8 + 0.8 + 0.2 ) is 4.8

求和(行)后,我们需要将它们除。 所以会是这样

1. arrayGedungA ([1/4.6],   [0.25/5.75], [1/4.2],   [0.4/5.6], [0.25/4.25], [1/4.8]);
2. arrayGedungB ([0.4/4.6], [1/5.75],    [0.4/4.2], [1/5.6],   [1/4.25],    [0.8/4.8]);
3. arrayGedungC ([0.2/4.6], [1/5.75],    [0.4/4.2], [1/5.6],   [0.5/4.25],  [0.8/4.8]);
4. arrayGedungD ([0.4/4.6], [1/5.75],    [0.8/4.2], [1/5.6],   [1/4.25],    [0.4/4.8]);
5. arrayGedungE ([1/4.6],   [1/5.75],    [0.8/4.2], [0.2/5.6], [0.5/4.25],  [0.8/4.8]);
6. arrayGedungF ([0.8/4.6], [1/5.75],    [0.4/4.2], [1/5.6],   [0.5/4.25],  [0.8/4.8]);
7. arrayGedungG ([0.8/4.6], [0.5/5.75],  [0.4/4.2], [1/5.6],   [0.5/4.25],  [0.2/4.8]);

结果将是

a

1 个答案:

答案 0 :(得分:0)

取决于数据库(并使用Dharman的建议),这在Oracle上有效:

SELECT harga,
       harga / SUM (harga) OVER () harga_sum,
       kapasitas,
       kapasitas / SUM (kapasitas) OVER () kapasitas_sum
  FROM nilai
 WHERE ...

请注意,必须在每个SUM(...)之后和别名列名之前给出OVER()。