0

I want to create controller which showing tranposed table where rows converted to column.

Here's the reference about transposing row to column

Reference code:

SELECT
     hostid
    ,sum(case when itemname = (select distinct itemname from history a order by 1 limit 0,1) then itemvalue end) as A
    ,sum(case when itemname = (select distinct itemname from history a order by 1 limit 1,1) then itemvalue end) as B
    ,sum(case when itemname = (select distinct itemname from history a order by 1 limit 2,1) then itemvalue end) as C
FROM history group by hostid order by 1;
+--------+------+------+------+
| hostid | A    | B    | C    |
+--------+------+------+------+
|      1 |   10 |    3 | NULL |
|      2 |    9 | NULL |   40 |
+--------+------+------+------+

My query which is the controller goals:

select 
  nilai_t1.nim,
  pendaftar.nama,
  sum(case when nilai_t1.id_sk1 = 11 then nilai_t1.nilai else 0 end) as 'Tanggung Jawab',
  sum(case when nilai_t1.id_sk1 = 12 then nilai_t1.nilai else 0 end) as 'Keaktifan',
  sum(case when nilai_t1.id_sk1 = 13 then nilai_t1.nilai else 0 end) as 'Teamwork',
  sum(case when nilai_t1.id_sk1 = 21 then nilai_t1.nilai else 0 end) as 'Wawancara',
  sum(case when nilai_t1.id_sk1 = 31 then nilai_t1.nilai else 0 end) as 'Tes Bakat'
from nilai_t1
inner join sub_kriteria_t1
  on nilai_t1.id_sk1 = sub_kriteria_t1.id_sk1
inner join kriteria_t1
  on sub_kriteria_t1.id_k1 = kriteria_t1.id_k1
inner join peserta_t1
    on nilai_t1.nim = peserta_t1.nim
inner join pendaftar
    on peserta_t1.nim = pendaftar.nim
group by nilai_t1.nim;

Before:
+----+------------+--------+-------+
| id | nim        | id_sk1 | nilai |
+----+------------+--------+-------+
|  1 | 181011**** |     11 |    30 |
|  2 | 181011**** |     12 |    30 |
|  3 | 181011**** |     13 |    25 |
|  4 | 181011**** |     21 |    72 |
|  5 | 181011**** |     31 |    57 |
|  6 | 181052**** |     11 |    20 |
|  7 | 181052**** |     12 |    20 |
|  8 | 181052**** |     13 |    10 |
|  9 | 181052**** |     21 |    75 |
| 10 | 181052**** |     31 |    57 |
+----+------------+--------+-------+

After:
+------------+------------------------+----+----+----+----+----+
| nim        | nama                   | 11 | 12 | 13 | 21 | 31 |
+------------+------------------------+----+----+----+----+----+
| 181011**** | Annisa indra           | 30 | 30 | 25 | 72 | 57 |
| 181052**** | Muhammad fakhri naufal | 20 | 20 | 10 | 75 | 57 |
+------------+------------------------+----+----+----+----+----+

Here's my one of my controller code:

public function calculate(){
        $sk_pluck = $sk = SubKriteriaTahap1::
        join('kriteria_t1', 'sub_kriteria_t1.id_k1', '=', 'kriteria_t1.id_k1')
        ->pluck(
            'sub_kriteria_t1.id_sk1');

        $sk_pluck->all();

        //$kriteria = KriteriaTahap1::all();
        $transpose=[];
        foreach ($sk_pluck as $sk1){
          $transpose = 'sum(case when nilai_t1.id_sk1 =' .$sk1.'  then nilai_t1.nilai else 0 end) AS \'Table\'';
        }

        $trans_array = array($transpose);

        $penilaian1 = PenilaianTahap1::select('nilai_t1.nim',
        'pendaftar.nama', DB::raw(implode(',', $trans_array)))
        ->join('peserta_t1', 'nilai_t1.nim', '=', 'peserta_t1.nim')
        ->join('pendaftar', 'peserta_t1.nim', '=', 'pendaftar.nim')
        ->join('sub_kriteria_t1', 'nilai_t1.id_sk1', '=', 'sub_kriteria_t1.id_sk1')
        ->join('kriteria_t1', 'sub_kriteria_t1.id_k1', '=', 'kriteria_t1.id_k1')
        ->groupBy('nilai_t1.nim')
        ->get([
            'nilai_t1.nim',
            'nilai_t1.nilai',
            'nilai_t1.id_sk1'
        ]);


        $response = [
            'message' => 'id',
            'data' => $trans_array
        ];
        return response()->json($response, Response::HTTP_OK);
    }

But the problem is, the foreach loop didn't work. It only show one kriteria instead many kriteria available. Here's the current result based on REST API:

{"message":"id","data":["sum(case when nilai_t1.id_sk1 =31  then nilai_t1.nilai else 0 end) AS 'Table'"]}

But the $sk_pluck did showing many kriteria id available:

{"message":"id","data":[11,12,13,21,31]}

Because those problem, the controller only resulting query with single kriteria:

{"message":"id","data":[{"nim":1810112048,"nama":"Annisa indra","Table":57},{"nim":1810522038,"nama":"Muhammad fakhri naufal","Table":57}]}

So how I can use foreach on my controller properly so the SUM query for table transposing (converting row to column) works? Or anything solution?

0 Answers0