table 1
invno percentage cost
1 18% 18.00
1 18% 18.00
2 18% 18.00
2 28% 28.00
table 2
id percentage
1 18%
2 28%
The table 2 percentage
column values should become the column headings of output.
In table 1, invno
1 has 2 entries, but the same percentage value of 18%
; invno
2 has 2 entries with different percentage values.
output
invno percentage 18% percentage 28%
1 36.00 0.00
2 18.00 28.00
So far I have written:
SELECT
`invno`,
SUM(CASE WHEN `percentage` = '18' THEN `percentage` ELSE NULL END) AS `percentage_18`,
SUM(CASE WHEN `percentage` = '28' THEN `percentage` ELSE NULL END) AS `percentage_28`
FROM `table1`
GROUP BY `invno`
HAVING 18 IS NOT NULL AND 28 IS NOT NULL
ORDER BY `invno`
This is fine, but I want to get the percentages dynamically.