-1

hi guys i am new to learn MySQL i have this table

f_id|c_file|e.name|operation|rate|c_no | qty|
 1    015    A      coping   0.30  1    1300
 2    015    A      coping   0.30  2    4567
 3    015    A      coping   0.30  3    1300
 4    015    A      coping   0.30  4    789
 5    015    A      coping   0.30  5    47
 6    015    B      cutting  0.30  1    568
 7    015    B      cutting  0.30  2    123
 8    015    B      cutting  0.30  3    8952
 9    015    B      cutting  0.30  1    456
 10   015    B      cutting  0.30  2    89
 11   015    B      cutting  0.30  3    78

now i want table like in this form of table table2

f_id|c_file|e.name|operation|c_no1|c_no2 |c_no3|c_no4|c_no5|total|rate|total*rate
 2     015  B       cutting 568     123   8952              9643  0.30 2892.9
 2     015  B       cutting 456     89    78                9643  0.30 2892.9
 total                      1024    212   9030
user3602024
  • 49
  • 1
  • 7

1 Answers1

0

You can use aggregation to do the pivoting and user variables to generate the sequence number.

set @fid := 0;

select @fid := @fid + 1 fid,
    c_file,
    e.name,
    operation,
    sum(case when c_no = 1 then qty end) c_no1,
    sum(case when c_no = 2 then qty end) c_no2,
    sum(case when c_no = 3 then qty end) c_no3,
    sum(case when c_no = 4 then qty end) c_no4,
    sum(case when c_no = 5 then qty end) c_no5,
    sum(qty) total,
    rate,
    sum(qty) * rate total_times_rate
from t
group by c_file,
    e.name,
    operation,
    rate
order by min(f_id);
Gurwinder Singh
  • 38,557
  • 6
  • 51
  • 76