0

I am trying to achieve the dynamic header based on the column value.

enter image description here

This is the original table. I want to prompt it

enter image description here

The type value will decide what the column header would be.

Would you please drop some hints about how to do it in SQL

NewPy
  • 623
  • 1
  • 5
  • 14
  • I think this is a bit different. The column headers are associated with the type column. (As there are two types, we may be able to simplify the query using hard coded type if needed.) – NewPy Jan 27 '22 at 07:47
  • My question is a bit more complicated compared with the thread you provided. The header of the ABC, DEF in the question you referred to need to be customized based on the type column value – NewPy Jan 27 '22 at 07:52

1 Answers1

3

Use aggregates and expressions:

SELECT id, 
  SUM(CASE WHEN type = 'secure' THEN level ELSE 0 END) AS secure_level,
  SUM(CASE WHEN type = 'hazard' THEN level ELSE 0 END) AS hazard_level,
FROM original_table
GROUP BY id
  • Thanks. This is exact what I want. Then next solution would need to be combining dynamic column value with aggregation – NewPy Jan 27 '22 at 08:21