pivot table sql server code example
Example 1: sql pivot
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
);
Example 2: pivot table sql
SELECT SalesAgent AS PivotSalesAgent, India, US, UK FROM tblAgentsSales
PIVOT
(
SUM(SalesAmount) FOR SalesCountry IN (India, US, UK)
)
AS testPivotTable
SELECT SalesAgent GrpBySalesAgent, SalesCountry, SUM(SalesAmount) Sales
from tblAgentsSales
GROUP BY SalesAgent, SalesCountry
select SalesAgent TableSalesAgent, SalesCountry, SalesAmount
from tblAgentsSales
Example 3: pivot table sql server
SELECT SalesAgent AS PivotSalesAgent, India, US, UK FROM tblAgentsSales
PIVOT
(
SUM(SalesAmount) FOR SalesCountry IN (India, US, UK)
)
AS testPivotTable
SELECT SalesAgent GrpBySalesAgent, SalesCountry, SUM(SalesAmount) Sales from tblAgentsSales
GROUP BY SalesAgent, SalesCountry
select SalesAgent TableSalesAgent, SalesCountry, SalesAmount from tblAgentsSales