mysqlsumcustomcolumn

list all the data, merge duplicate rows and sum the total using MySQL


I have the following SQL

SELECT
  i.si_num AS `id`,
  DATE_FORMAT(i.si_date, '%d-%b-%Y') AS `date`,
  i.si_tr AS `tr`,
  d.dl_name AS `customer`,
  i.si_net_value AS `net`,
  DATEDIFF(CURDATE(), si_date) AS Days,
  t.value AS tval,
  t.label AS label
FROM
  invoices AS i
LEFT JOIN
  dealer AS d ON i.si_tr = d.dl_id
LEFT JOIN
  transactions AS t ON i.si_num = t.invoice
WHERE
  i.si_tr = 'TR580494'
ORDER BY `si_num` DESC;

Current Output

+-----+-----------+----------+---------------------+---------+------+-------+-------+
| id  |   date    |    tr    |      customer       |   net   | Days | tval  | label |
+-----+-----------+----------+---------------------+---------+------+-------+-------+
| 404 | 18-Feb-17 | TR580494 | STARSHIP ENTERPRISE | 109790  |   55 | 96070 | acr   |
| 404 | 18-Feb-17 | TR580494 | STARSHIP ENTERPRISE | 109790  |   55 | 10080 | crn   |
| 404 | 18-Feb-17 | TR580494 | STARSHIP ENTERPRISE | 109790  |   55 | 3640  | crn   |
| 240 | 13-Feb-17 | TR580494 | STARSHIP ENTERPRISE | 0       |   60 | NULL  | NULL  |
| 239 | 13-Feb-17 | TR580494 | STARSHIP ENTERPRISE | 81975   |   60 | 30405 | acr   |
| 239 | 13-Feb-17 | TR580494 | STARSHIP ENTERPRISE | 81975   |   60 | 51570 | crn   |
| 132 | 3-Feb-17  | TR580494 | STARSHIP ENTERPRISE | 38132.5 |   70 | 33282 | acr   |
+-----+-----------+----------+---------------------+---------+------+-------+-------+

as you can see there are some duplicate rows being generated like id: 404 & 239 the only difference in these rows are the values in the columns 'tval' & 'label'

the 'tval' and the 'label' columns can populate more than once per id, what i am trying to achieve is for every duplicate record i want to see if the label is a 'crn' or 'arc' and sum the values respective to the id and generate one row and transpose the labels as columns. please see below for the expected output.

expected output

+-----+-----------+----------+---------------------+---------+------+-------+-------+
| id  |   date    |    tr    |      customer       |   net   | days |  crn  |  acr  |
+-----+-----------+----------+---------------------+---------+------+-------+-------+
| 404 | 18-Feb-17 | TR580494 | STARSHIP ENTERPRISE | 109790  |   55 | 13720 | 96070 |
| 240 | 13-Feb-17 | TR580494 | STARSHIP ENTERPRISE | 0       |   60 | NULL  | NULL  |
| 239 | 13-Feb-17 | TR580494 | STARSHIP ENTERPRISE | 81975   |   60 | 51570 | 30405 |
| 132 | 3-Feb-17  | TR580494 | STARSHIP ENTERPRISE | 38132.5 |   70 | NULL  | 33282 |
+-----+-----------+----------+---------------------+---------+------+-------+-------+

Solution

  • You can do that with a case inside a sum:

    SELECT
      i.si_num AS `id`,
      DATE_FORMAT(i.si_date, '%d-%b-%Y') AS `date`,
      i.si_tr AS `tr`,
      d.dl_name AS `customer`,
      i.si_net_value AS `net`,
      DATEDIFF(CURDATE(), si_date) AS Days,
      sum(case when t.label = 'acr' then t.value else null end) as acr
      sum(case when t.label = 'crn' then t.value else null end) as crn
    FROM
      invoices AS i
    LEFT JOIN
      dealer AS d ON i.si_tr = d.dl_id
    LEFT JOIN
      transactions AS t ON i.si_num = t.invoice
    WHERE
      i.si_tr = 'TR580494'
    GROUP BY  i.si_num,
              DATE_FORMAT(i.si_date, '%d-%b-%Y'),
              i.si_tr,
              d.dl_name,
              i.si_net_value,
              DATEDIFF(CURDATE(), si_date)
    ORDER BY `si_num` DESC;