2013-12-13 84 views
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語句中的值。

回答

1

我知道這個邏輯,但它不會在你的榜樣幫助:

declare @mytest table (val1 int) 

declare @test int = 60 
declare @cur int = 0 
declare @counter int = 1 

while (@cur < @test) 
BEGIN 
    insert into @mytest select @cur 
    set @cur = @cur + 1 
END 

select @counter = @counter + case when val1%2=0 then 1 else 0 end) 
from @mytest 

select @counter 

對於我來說,你不能這樣做,那麼容易。 Sql服務器將數據作爲一個包進行管理,他無法想象做一個迭代。你有其他軟件可以做到這一點:)而不是如此複雜的邏輯。

也許你可以嘗試用dense_rank或rank_number來執行你的代碼。

如果表現不那麼糟糕,我會做這樣的:

declare @mytest table (val1 int) 

declare @test int = 60 
declare @cur int = 0 

while (@cur < @test) 
BEGIN 
    insert into @mytest select @cur 
    set @cur = @cur + 1 
END 

select case when b.val1%2=0 then b.val1 else 0 end + ISNULL(p.counters,0) 
from @mytest b 
left outer join (select val1, ROW_NUMBER() over (order by val1) as counters from @mytest where val1%2 =0) p on p.val1 = b.val1 

當然這是一個簡單的例子,但它應該適合你想要更復雜的連接和where子句做什麼: )