xxxxxxxxxx
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
xxxxxxxxxx
SELECT * -- Total invoices per gender
FROM (
SELECT invoice, gender
FROM sales
) d
PIVOT (
sum(invoice)
FOR gender IN ('F' AS "Women", 'M' AS "Men")
);
-- Table Sales:
CREATE TABLE sales (
gender VARCHAR2(1 BYTE), -- 'F' or 'M'
invoice NUMBER
);
xxxxxxxxxx
SELECT <non-pivoted column>,
[first pivoted column] AS <column name>,
[second pivoted column] AS <column name>,
[last pivoted column] AS <column name>
FROM
(<SELECT query that produces the data>)
AS <alias for the source query>
PIVOT
(
<aggregation function>(<column being aggregated>)
FOR
[<column that contains the values that will become column headers>]
IN ( [first pivoted column], [second pivoted column],
last pivoted column]) [
) AS <alias for the pivot table>
<optional ORDER BY clause>;
xxxxxxxxxx
select *
from
(
select game, player, goals
from yourtable
) src
pivot
(
sum(goals)
for player in ([John], [Paul], [Mark], [Luke])
) piv
order by game
xxxxxxxxxx
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
xxxxxxxxxx
select region, -- non pivoted column at 1st
[2010] -- pivoted column
, [2011] -- pivoted column
, [2013] -- pivoted column
, [2012] -- pivoted column
from -- always from
(select * from pivot_demo) a -- need to give alias to query
pivot
(sum(sales) for year in ([2010], [2011], [2013], [2012])) b -- put columns in () & []
xxxxxxxxxx
SELECT * FROM(
SELECT date,line_number,machine_number,TIME,VAL FROM(
select *
from output_tbl
unpivot
(
VAL
for TIME in ([0000HR])
) uvt
)tb1 )tb3
PIVOT (
max(VAL)
FOR TIME IN ([0000HR])
)pvt
xxxxxxxxxx
-- Original query before pivoting
SELECT
Country, Year, COUNT(*) AS Awards
FROM Summer_Medals
WHERE
Country IN ('CHN', 'RUS', 'USA')
AND Year IN (2008, 2012)
AND Medal = 'Gold'
GROUP BY Country, Year
ORDER BY Country ASC, Year ASC;
-- Using pivoting
CREATE EXTENSION IF NOT EXISTS tablefunc;
SELECT * FROM CROSSTAB($$
-- Start original query
SELECT
Country, Year, COUNT(*) :: INTEGER AS Awards -- make sure to cast
FROM Summer_Medals
WHERE
Country IN ('CHN', 'RUS', 'USA')
AND Year IN (2008, 2012)
AND Medal = 'Gold'
GROUP BY Country, Year
ORDER BY Country ASC, Year ASC;
-- End original query
$$) AS ct (Country VARCHAR, "2008" INTEGER, "2012" INTEGER) -- Country column remains, others are new columns
ORDER BY Country ASC;
xxxxxxxxxx
PIVOT
(SUM(TotalDue)
FOR Qyr
IN ([2013 Q1], [2013 Q2], [2013 Q3], [2013 Q4])
) AS PivotTable