sql server rows to columns pivot code example
Example 1: sql server pivot rows to columns
select Firstname, Amount, PostalCode, LastName, AccountNumber
from
(
select value, columnname
from yourtable
) d
pivot
(
max(value)
for columnname in (Firstname, Amount, PostalCode, LastName, AccountNumber)
) piv;
Example 2: sql pivot rows to columns
SELECT *
FROM (
SELECT invoice, gender
FROM sales
) d
PIVOT (
sum(invoice)
FOR gender IN ('F' AS "Women", 'M' AS "Men")
);
CREATE TABLE sales (
gender VARCHAR2(1 BYTE),
invoice NUMBER
);