涉及ALIAS时计算列

时间:2014-02-05 02:08:20

标签: sql sql-server alias

问题:

  

编写一个SELECT语句,从Products表中返回这些列名和数据:

     

ProductName - ProductName列

     

ListPrice - ListPrice列

     

DiscountPercent - DiscountPercent列

     

DiscountAmount - 根据前两列计算的列

     

DiscountPrice - 从前三列

计算的列      

按降序排列DiscountPrice对结果集进行排序。

我已经完成了第一次计算,计算了DiscountAmount。现在,我必须通过减去ListPrice - DiscountPercent - DiscountAmount(这是一个别名)来计算DiscountPrice。

我似乎无法弄清楚这一点。到目前为止我的代码如下:

SELECT ProductName, 
       ListPrice, 
       DiscountPercent, 
       ListPrice - DiscountPercent AS DiscountAmount
FROM Products;

4 个答案:

答案 0 :(得分:2)

有几种方法可以做到这一点。公用表表达式(CTE方法)例如:

;With Data As
(
    SELECT  ProductName, 
            ListPrice, 
            DiscountPercent, 
            ListPrice - DiscountPercent AS DiscountAmount
    FROM Products
)
Select  ProductName,
        ListPrice,
        DiscountPercent,
        DiscountAmount,
        ListPrice-DiscountAmount As DiscountPrice
From    Data;

就个人而言,当这种方法相对简单时,我并不关心这种方法。相反,我通常在必要时重复计算。像这样:

Select  ProductName,
        ListPrice,
        DiscountPercent,
        ListPrice - DiscountPercent As DiscountAmount,
        ListPrice- (ListPrice - DiscountPercent) As DiscountPrice
From    Products;

顺便说一句,我认为您对DiscountAmount的计算是错误的。由于这是一次学习经历,我建议你仔细检查那部分。请注意,即使我认为这是错误的,我也没有更正这部分代码。

答案 1 :(得分:2)

似乎是G.Mastros忘了按降序排序按折扣价对结果集进行排序。所以你的整个查询应该是

SELECT  ProductName,
        ListPrice,
        DiscountPercent,
        ListPrice - DiscountPercent As DiscountAmount,
        ListPrice - (ListPrice - DiscountPercent) As DiscountPrice
FROM    Products
ORDER BY DiscountPrice DESC

答案 2 :(得分:1)

上一个答案会给你不正确的结果。如果你愿意,你可以试试我的答案 -

首先,创建一个样本表 -

CREATE TABLE [dbo].[Products](
    [ProductName] [varchar](50) NULL,
    [ListPrice] [decimal](18, 2) NULL,
    [DiscountPercent] [decimal](18, 2) NULL
) 

INSERT [dbo].[Products] ([ProductName], [ListPrice], [DiscountPercent]) VALUES (N'Bear', CAST(1100.00 AS Decimal(18, 2)), CAST(10.00 AS Decimal(18, 2)))
INSERT [dbo].[Products] ([ProductName], [ListPrice], [DiscountPercent]) VALUES (N'Magazines', CAST(200.00 AS Decimal(18, 2)), CAST(13.50 AS Decimal(18, 2)))
INSERT [dbo].[Products] ([ProductName], [ListPrice], [DiscountPercent]) VALUES (N'Mankini', CAST(25.00 AS Decimal(18, 2)), CAST(45.00 AS Decimal(18, 2)))

你得到的表 -

ProductName ListPrice   DiscountPercent
Bear    1100.00 10.00
Magazines   200.00  13.50
Mankini 25.00   45.00

然后根据您的需要进行查询 -

SELECT *
FROM
(
SELECT 
[ProductName]
,[ListPrice]
,[DiscountPercent]
,CAST(([ListPrice]*[DiscountPercent]/100) as Decimal(18,2)) AS DISCOUNT_AMOUNT
,CAST(([ListPrice]*(1-[DiscountPercent]/100))as Decimal(18,2)) AS DISCOUNT_PRICE
FROM [Products]
) AS [STUFF]
ORDER BY [STUFF].DISCOUNT_PRICE DESC

我的查询结果 -

ProductName ListPrice   DiscountPercent DISCOUNT_AMOUNT DISCOUNT_PRICE
Bear    1100.00 10.00   110.00  990.00
Magazines   200.00  13.50   27.00   173.00
Mankini 25.00   45.00   11.25   13.75

G Mastros查询结果 -

ProductName ListPrice   DiscountPercent DiscountAmount  DiscountPrice
Bear    1100.00 10.00   1090.00 10.00
Magazines   200.00  13.50   186.50  13.50
Mankini 25.00   45.00   -20.00  45.00

答案 3 :(得分:0)

SELECT ProductName,ListPrice,DiscountPercent,

ListPrice * DiscountPercent / 100 AS DiscountAmount,

ListPrice *(1 - DiscountPercent / 100)AS DiscountPrice

FROM Products

按照DiscountPrice DESC订购