Use:
SELECT t.inv,
MAX(CASE WHEN t.description = 'CHARGES' THEN t.amount ELSE NULL END) AS charges,
MAX(CASE WHEN t.description = 'FREIGHT' THEN t.amount ELSE NULL END) AS freight,
MAX(CASE WHEN t.description = 'INSURANCE' THEN t.amount ELSE NULL END) AS insurance
FROM YOUR_TABLE t
GROUP BY t.inv
ORDER BY t.inv
In order to support a dynamic list of descriptions, you'd have to specify which database this is for because the dynamic SQL syntax is different for each one.
PIVOT/UNPIVOT is ANSI syntax, but support is limited:
- SQL Server 2005+
- Oracle 11g+
MySQL doesn't support PIVOT, nor does SQLite. I don't know when/if PostgreSQL or DB2 does...
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…