select语句中的错误

时间:2013-09-13 14:19:30

标签: sql sql-server select

在下面的查询中,我在第四行的“LettingPercent”上收到错误,“无效的列名称”。我想使用lettingpercentage中返回的每个结果来计算我的陈述第四行的租赁费用

declare @let varchar(50)
select 
CONVERT(varchar(50), InstructionLettingFee.percentage)+'%' as 'LettingPercent',
CONVERT(decimal(18,2), LettingPercent / (100 * DealFees.pddrl_TermRent)) as LettingFee
from tableOne
left outer join tableTwo LettingInstruction on LettingInstruction.ColumnOne=     tableOne.ColumnOne
left outer join TableThree InstructionLettingFee on InstructionLettingFee.ColumnForOne = LettingInstruction.ColumTwo
left outer join TableFour DealFees on DealFees.ColumnOne = pDeal.ColumnOne

5 个答案:

答案 0 :(得分:1)

单引号是不必要的。但问题是你指的是下一行的价值。而是返回原始数据:

declare @let varchar(50)
select CONVERT(varchar(50), InstructionLettingFee.percentage)+'%') as LettingPercent,
       CONVERT(decimal(18,2), InstructionLettingFee.percentage / (100 * DealFees.pddrl_TermRent)) as LettingFee
from tableOne
left outer join tableTwo LettingInstruction on LettingInstruction.ColumnOne=     tableOne.ColumnOne
left outer join TableThree InstructionLettingFee on InstructionLettingFee.ColumnForOne = LettingInstruction.ColumTwo
left outer join TableFour DealFees on DealFees.ColumnOne = pDeal.ColumnOne;

编辑:

要防止除以0,请将case放在计算周围:

       (case when DealFees.pddrl_TermRent > 0
             then CONVERT(decimal(18,2), InstructionLettingFee.percentage / (100 * DealFees.pddrl_TermRent))
        end) as LettingFee

答案 1 :(得分:1)

你不能像这样引用别名,你需要重复这个陈述。 即使你可以将LettingPercent变成一个字符串。 尝试:

    declare @let varchar(50)
    select 
    CONVERT(varchar(50), InstructionLettingFee.percentage)+'%' as 'LettingPercent',
LettingFee =
case InstructionLettingFee.percentage
when 0 then cast( 0 as decimal(18,2) )
else  CONVERT(decimal(18,2), InstructionLettingFee.percentage / (100 * DealFees.pddrl_TermRent)) 
end
    from tableOne
    left outer join tableTwo LettingInstruction on LettingInstruction.ColumnOne=     tableOne.ColumnOne
    left outer join TableThree InstructionLettingFee on InstructionLettingFee.ColumnForOne = LettingInstruction.ColumTwo
    left outer join TableFour DealFees on DealFees.ColumnOne = pDeal.ColumnOne

答案 2 :(得分:0)

您在查询中引用别名是错误的,请尝试以下查询:

select CONVERT(varchar(50), LettingPercent)+'% LettingPercent',
CONVERT(decimal(18,2), LettingPercent / (100 * pddrl_TermRent)) as LettingFee
from
(select 
InstructionLettingFee.percentage as LettingPercent,
DealFees.pddrl_TermRent
from tableOne
left outer join tableTwo LettingInstruction on LettingInstruction.ColumnOne=     tableOne.ColumnOne
left outer join TableThree InstructionLettingFee on InstructionLettingFee.ColumnForOne = LettingInstruction.ColumTwo
left outer join TableFour DealFees on DealFees.ColumnOne = pDeal.ColumnOne)

答案 3 :(得分:0)

不幸的是,同一级别的其他列无法访问动态创建的列。

您可以使用派生表来实现此目的:

SELECT 
    CONVERT(varchar(50), LettingPercent)+ '%' as LettingPercent,
    CONVERT(decimal(18,2), LettingPercent / (100 * TermRent)) as LettingFee
FROM (
    SELECT 
        InstructionLettingFee.percentage AS LettingPercent,
        DealFees.pddrl_TermRent AS TermRent
    from tableOne
    left outer join tableTwo LettingInstruction on LettingInstruction.ColumnOne = tableOne.ColumnOne
    left outer join TableThree InstructionLettingFee on InstructionLettingFee.ColumnForOne = LettingInstruction.ColumTwo
    left outer join TableFour DealFees on DealFees.ColumnOne = pDeal.ColumnOne
) A

答案 4 :(得分:0)

当使用AS关键字并且在您的alises中没有任何空格时,不需要引用该字符串。但是如果你想在别名中添加引号,那么你应该使用双引号,而不是单引号,即。

... AS“LettingPercent”