Can calculated column be used in another calculated column?

白昼怎懂夜的黑 提交于 2021-01-28 14:50:35

问题


SELECT ExchangeRatePrice = CASE pp.Price
     WHEN NULL THEN 0
       ELSE (CASE WHEN c.CurrencyId = 1 THEN pp.Price 
       ELSE CONVERT(DECIMAL(9, 2), (pp.Price * c.ExchangeRate)) END)
     END , 

     price as OriginalPriceInDB,
     10 * Price as CalculatedPrice,
    c.currencyid as Currency

FROM ProductPrice pp, currency c

alt text http://img682.imageshack.us/img682/3692/exchangerate.png

I want calculated column (ExchangeRatePrice) to use in CalculatedPrice. Can I use it straight of I need to convert it again?

I have used 10 * Price just to show you the example because if I use 'ExchangeRatePrice' here it will give error 'Invalid Column name'


回答1:


To reference pre-calculate a value in SQL usually means performing the calculation in an inner query (AKA inline view):

SELECT x.exchangerateprice,
       x.OriginalPriceInDB,
       10 * x.exchangerateprice AS CalculatedPrice,
       x.currencyid
  FROM (SELECT CASE 
                 WHEN pp.price IS NULL THEN
                   0
                 WHEN c.CurrencyId = 1 THEN 
                   pp.Price
                 ELSE 
                   CONVERT(DECIMAL(9, 2), (pp.Price * c.ExchangeRate)) 
               END AS ExchangeRatePrice,
              price as OriginalPriceInDB,
              c.currencyid as Currency
         FROM PRODUCTPRICE pp, 
              CURRENCY c) x

This is the equivalent to using the WITH clause (available in SQL Server 2005+) - there's no difference in performance.

You could duplicate the logic:

SELECT CASE 
         WHEN pp.price IS NULL THEN
           0
         WHEN c.CurrencyId = 1 THEN 
           pp.Price
         ELSE 
           CONVERT(DECIMAL(9, 2), (pp.Price * c.ExchangeRate)) 
       END AS ExchangeRatePrice,
       price as OriginalPriceInDB,
       CASE 
          WHEN pp.price IS NULL THEN
            0
          WHEN c.CurrencyId = 1 THEN 
            pp.Price
          ELSE 
            CONVERT(DECIMAL(9, 2), (pp.Price * c.ExchangeRate)) 
        END * 10 AS CalculatedPrice,
        c.currencyid as Currency
   FROM PRODUCTPRICE pp, 
        CURRENCY c

..but that means duplication & inherent risk of someone not updating both instances to keep in sync.




回答2:


Yes. Use "WITH" (Common Table Expressions)

With MainT as (
    SELECT CASE pp.Price
        WHEN NULL 
        THEN 0
        ELSE (
            CASE WHEN c.CurrencyId = 1 THEN pp.Price
            ELSE CONVERT(DECIMAL(9, 2), (pp.Price * c.ExchangeRate)) END
            )
        END ExchangeRatePrice,
        price as OriginalPriceInDB,
        c.currencyid as Currency
    FROM ProductPrice pp, currency c
)
select *,  10*ExchangeRatePrice as CalculatedPrice
from MainT



回答3:


You can't use column aliases until the ORDER BY clause.

But you can use column aliases defined in a subquery.

SELECT x * 10 AS y
FROM (SELECT 123 AS x) tbl


来源:https://stackoverflow.com/questions/1694889/can-calculated-column-be-used-in-another-calculated-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!