P中的from子句后P的含义是什么

时间:2016-07-07 15:24:30

标签: sql sql-server

我试着理解这个编译好的SQL代码。

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

from子句后P是什么意思?最后PV

我继承了代码,说我应该在C#中获取一个列表,但SQL Server会说int?

之前从未见过这个。我试过谷歌搜索。

3 个答案:

答案 0 :(得分:3)

它们是用于派生表的名称。

P<alias for the source query>PV<alias for the pivot table>

pivot语句的语法如下:

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.

答案 1 :(得分:1)

它是一个表别名 - 在查询中使用的简称。您可以选择任何您想要的名称。 例如:

javac

使用USE AdventureWorks2008R2; GO SELECT c.CustomerID, s.Name FROM Sales.Customer AS c JOIN Sales.Store AS s ON c.CustomerID = s.BusinessEntityID ; c比使用sSales.Customer名称更容易操作。 Sales.Store是可选的。你可以省略它。

更多信息here

答案 2 :(得分:0)

P是子查询的别名,PV看起来像是数据透视的别名。

在查询窗口中运行查询并查看它返回的内容。