0

以下のクエリでは、4 行目の「LettingPercent」で「無効な列名」というエラーが発生します。ステートメントの 4 行目にある賃貸料を計算するために、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
4

5 に答える 5

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 に答える
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 に答える