0
我需要從case語句(在SQL select語句中使用它)中獲取輸出,並且還要輸出我想增加局部變量的值的輸出。帶有計算的SQL語句語句SQL server
CASE WHEN convert(int,d.ApplyEscalatorAfterHowManyYears)>0 AND
y.num>=convert(int,d.ApplyEscalatorAfterHowManyYears) THEN ((convert(money,d.AnnualAmount)*(d.Escalator*@counter))/100) else 0 end)
可能有一些語法錯誤。
我不知道下面的查詢是否有助於理解與否。
declare @counter int
set @counter = 1
;WITH myTbl AS (SELECT * FROM(
SELECT src.ForecastAccountID, src.AccountName, src.RepeatNumber,src.AttributeName,src.Value
FROM (SELECT a.ForecastID, a.ForecastAccountID, a.RepeatNumber, a.AccountNumber, c.AccountName, src.AttributeName, fa.Value
FROM CoA c WITH (NOLOCK) INNER JOIN (Forecast_Account a WITH (NOLOCK) INNER JOIN (
(SELECT s.AttributeSetID, s.AttributeSetName, a.AttributeID, a.AttributeName, a.ColumnOrder, a.SignMultiplier
FROM Attribute_Set s INNER JOIN Attribute a ON s.[AttributeSetID] = a.[AttributeSetID]
WHERE (((s.AttributeSetID)=3))) src
INNER JOIN Forecast_Attribute fa WITH (NOLOCK) ON src.[AttributeID] = fa.[AttributeID]) ON a.[ForecastAccountID] = fa.[ForecastAccountID]) ON c.AccountNumber = a.AccountNumber
WHERE (((a.ForecastAccountID)=332))) src
GROUP BY src.ForecastAccountID, src.AccountName, src.RepeatNumber,src.AttributeName,src.Value
) AS t
PIVOT (min(Value) FOR AttributeName IN ([Counterparty],[Memo],[CoverPeriodBegin],[CoverPeriodEnd],[PaymentFrequency],[AdditionalYearsToRepeat],[AnnualAmount],[Escalator],[ApplyEscalatorAfterHowManyYears],[Payment1Date],[Payment1Percent],[Payment2Date],[Payment2Percent],[Payment3Date],[Payment3Percent],[Payment4Date],[Payment4Percent])) AS pvt),
num(num) AS (SELECT 0 UNION ALL SELECT num+1 FROM num WHERE num < 60)
--INSERT INTO Forecast_Data(ForecastAccountID,CashGAAP,TheDate,Amount,LastUpdated,UpdatedBy)
SELECT d.ForecastAccountID, 'GAAP' AS CashGAAP, dateadd(M, (x.num + 12*y.num), convert(datetime,d.CoverPeriodBegin)) AS TheDate,
Round((convert(money,d.AnnualAmount)+ (CASE WHEN convert(int,d.ApplyEscalatorAfterHowManyYears)>0 AND
y.num>=convert(int,d.ApplyEscalatorAfterHowManyYears) THEN ((convert(money,d.AnnualAmount)*(d.Escalator*@counter))/100) else 0 end))/(DATEDIFF(M,d.CoverPeriodBegin,d.CoverPeriodEnd)+1),2) AS Amount,
GETDATE() AS LastUpdated,
'jhogg1' AS UpdatedBy,y.num FROM num x,num y, myTbl AS d
WHERE (x.num BETWEEN 0 AND (datediff(M, convert(datetime,d.CoverPeriodBegin), convert(datetime,d.CoverPeriodEnd)))) AND (y.num BETWEEN 0 AND convert(int,d.AdditionalYearsToRepeat));
我想與計算沿增加@counter的case語句中的值。