SQL Server's derived table temporary table

Example 1:

create a query

SELECT ProductID, Name, ListPrice,    
    CASE WHEN ListPrice > 1000 THEN N'High' ELSE N'Normal' END AS PriceType
FROM SalesLT.Product

for further queries without increasing its complexity. At this point, a derived table can be created.

It creates new entries based on the previous query.

SELECT DerivedTable.ProductID, DerivedTable.Name, DerivedTable.ListPrice
FROM    
    (        
        SELECT ProductID, Name, ListPrice,        
        CASE WHEN ListPrice > 1000 THEN N'High' ELSE N'Normal' END AS PriceType        
        FROM SalesLT.Product    
    ) AS DerivedTable
WHERE DerivedTable.PriceType = N'High';

Example 2:

In order to meet the cargo and weight requirements.

SELECT ProductID, Name, Weight, ListPrice,       
    CASE WHEN Weight > 1000 THEN N'Heavy' ELSE N'Normal' END AS WeightType
FROM SalesLT.Product;

Query heavy goods, and the price is higher than 2000.

SELECT DerivedTable.ProductID, DerivedTable.Name, DerivedTable.Weight, DerivedTable.ListPrice
FROM    (        
    SELECT ProductID, Name, Weight, ListPrice,               
    CASE WHEN Weight > 1000. THEN N'Heavy' ELSE N'Normal' END AS WeightType        
    FROM SalesLT.Product    
    ) AS DerivedTable
WHERE DerivedTable.WeightType = N'Heavy' AND DerivedTable.ListPrice > 2000;

This is the derived table.

Guess you like

Origin blog.csdn.net/figosoar/article/details/131720458