Joe Joe - 5 months ago 19
SQL Question

What does P mean after the from clause in SQL

I am try to make sense of this SQL code that compiles fine.

SELECT *
FROM
(SELECT [eeeConfigurationId], [TimeInterval]
FROM [dbo].[eeeConfiguration]) P
PIVOT
(MAX([TimeInterval]) FOR [eeeConfigurationId] IN
([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],
[11],[12],[13],[14],[15],[16],[17],[18],[19],[20],
[21],[22],[23],[24],[25],[26],[27],[28],[29],[30],
[31],[32],[33],[34],[35],[36],[37],[38],[39],[40],
[41],[42],[43],[44],[45],[46],[47],[48],[49],[50]) )PV


What does that
P
after the from clause mean? And
PV
at the end?

I inherited code that says I should get a list in C# but SQL Server says int?

Never seen this before. I tried googling.

jpw jpw
Answer

They are the names used for the derived tables.

P is <alias for the source query> and PV is <alias for the pivot table>.

The syntax for the pivot statement is as follows:

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>;

Reference.