我需要在一个语句中进行多次计算

时间:2011-12-15 10:12:17

标签: sql-server sql-server-2008

SELECT PrisID, 
       Navn, 
       [Vægt (g)], 
       [RåvarePris(øre)], 
       Produktionsomk, 
       [RåvarePris(øre)] + Produktionsomk AS NettoPris, 
       NettoPris * 25 AS Moms, 
       NettoPris + Moms AS PrisInclMoms, 
       PrisInclMoms * 100 AS [Pris(100gr)]
FROM PrislisteBolsjer  

1 个答案:

答案 0 :(得分:3)

您不能在创建的同一个字段列表中使用别名。

您可以使用cross apply进行需要重复使用的计算。

SELECT PrisID, 
       Navn, 
       [Vægt (g)], 
       [RåvarePris(øre)], 
       Produktionsomk, 
       T1.NettoPris, 
       T2.Moms, 
       T3.PrisInclMoms, 
       T3.PrisInclMoms * 100 AS [Pris(100gr)]
FROM PrislisteBolsjer
  CROSS APPLY (SELECT [RåvarePris(øre)] + Produktionsomk) AS T1(NettoPris)
  CROSS APPLY (SELECT T1.NettoPris * 25) AS T2(Moms)
  CROSS APPLY (SELECT T1.NettoPris + T2.Moms) AS T3(PrisInclMoms)