How to dynamically pivot data using SQL