'How to convert Row to columns in SQL?

I am having a Table with TERMINAL_ID,Amount and Dates.I want the output as distinct Terminal ID,The distinct date as headers and sum of amount for that Terminal date wise.

I am using below Query but only getting Terminal ID in the Output but i want Dates a column heading and data as sum(TXN_AMT) for respective date

SELECT
    distinct([TERM_ID])
FROM Hitachi_tbl_May_22 Result
PIVOT (
  SUM([TXN_AMT])
  FOR [DATE]
  IN ("2022-05-01","2022-05-02")
) AS PivotTable;


Solution 1:[1]

use this : For two column only if you use dynamic date then first find all date and after get it with IN() and set Column

SELECT
    distinct([TERM_ID]),[2022-05-01],[2022-05-02]
FROM Hitachi_tbl_May_22 Result
PIVOT (
  SUM([TXN_AMT])
  FOR [DATE]
  IN ("2022-05-01","2022-05-02")
) AS PivotTable;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Piyush Kachhadiya