0

在下面的查询中,我在第四行的“LettingPercent”上出现错误,“列名无效”。我想使用出租百分比中返回的每个结果来计算我的声明第四行的出租费用

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
4

5 回答 5

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
于 2013-09-13T14:23:23.887 回答
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
于 2013-09-13T14:22:24.640 回答
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)
于 2013-09-13T14:23:46.387 回答
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
于 2013-09-13T14:26:19.330 回答
0

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

...作为“让百分比”

于 2013-09-13T14:26:27.077 回答