涉及 ALIAS 时计算列

问题描述 投票:0回答:5

问题:

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

ProductName - 产品名称列

ListPrice - 列表价格列

DiscountPercent - DiscountPercent 列

DiscountAmount - 根据前两列计算得出的列

DiscountPrice - 根据前三列计算得出的列

将结果集按DiscountPrice降序排序。

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

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

SELECT ProductName, 
       ListPrice, 
       DiscountPercent, 
       ListPrice - DiscountPercent AS DiscountAmount
FROM Products;
sql sql-server alias
5个回答
2
投票

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

;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 的计算是错误的。由于这是一次学习经历,我建议您仔细检查该部分。请注意,尽管我认为这部分代码是错误的,但我没有更正这部分代码。


2
投票

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

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

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

0
投票

选择产品名称、列表价格、折扣百分比,

列表价格 * 折扣百分比 / 100 AS 折扣金额,

列表价格 * (1 - 折扣百分比 / 100) AS 折扣价格

来自产品

按折扣价格排序


0
投票
1. For Future Inquiries: The DiscountAmount and the 
   DiscountPrice are mixed up. It should be:
    SELECT ProductName, ListPrice, DiscountPercent,
     ListPrice - DiscountPercent AS DiscountPrice,
     ListPrice - (ListPrice - DiscountPercent) AS 
     DiscountAmount
    FROM Products
2.[1]

You see the discount amount is the actual discount and the discount Price is the price one pays after discount.  This may seem inconsequential for a class, but for any business, this would be the meaning of each term.

© www.soinside.com 2019 - 2024. All rights reserved.