在查询中计算错误的金额

时间:2013-12-26 11:18:38

标签: sql sql-server sql-server-2008 tsql sql-server-2005

我有一些表有一些记录现在想用一些逻辑重复这个表内容。我有两个日期开始日期和终止日期,意味着记录从start_date开始到结束终止日期,它会正常工作,但问题是计算金额, 逻辑是金额计算公式

basesalary / 12 * ( SUTARate / 100 ) * ( x.num+1) 

如果这个数量少于SUTAMaximumAmount这个数量被使用,否则为0.还有一件事,如果金额将保留,年份完成,然后从明年重新开始计算.. x.num是临时表保持90到1的数字

 BaseSalary|  S_Date  |  T_Date     | SUTARate| SUTAMaximumAmount |A_S_Percent
   48000   | 7-1-2013 |  3-15-2015  | 1.1     | 300               | 5

我的结果是

     DAte                     amount
2013-07-01 00:00:00.000         44
2013-08-01 00:00:00.000         44
2013-09-01 00:00:00.000         44
2013-10-01 00:00:00.000         44
2013-11-01 00:00:00.000         44
2013-12-01 00:00:00.000         44
2014-01-01 00:00:00.000         36
2014-02-01 00:00:00.000         -8
2014-03-01 00:00:00.000        -52
2014-04-01 00:00:00.000        -96
2014-05-01 00:00:00.000       -140
2014-06-01 00:00:00.000       -184
2014-07-01 00:00:00.000       -228
2014-08-01 00:00:00.000       -272
2014-09-01 00:00:00.000       -316
2014-10-01 00:00:00.000       -360
2014-11-01 00:00:00.000       -404
2014-12-01 00:00:00.000       -448
2015-01-01 00:00:00.000       -492
2015-02-01 00:00:00.000       -536
2015-03-01 00:00:00.000       -580

我想要这样的结果

Date       |  Amount
7-1-2013       44
8-1-2013       44
9-1-2013       44
10-1-2013      44
11-1-2013      44
12-1-2013      44
1-1-2014       44
2-1-2014       44
3-1-2014       44
4-1-2014       44
5-1-2014       44
6-1-2014       44
7-1-2014       36
1-1-2015       44
2-1-2015       44
3-1-2015       44

查询

SELECT dateadd(M, (x.num),d.StartDate) AS TheDate,            

Round( case when ((convert(float,d.SUTARate)/100* convert(integer,d.BaseSalary) / 12)*(x.num+1)) <=CONVERT(money,d.SUTAMaximumAmount)       
then (convert(float,d.SUTARate)/100* convert(integer,d.BaseSalary)* / 12)       
else (CONVERT(money,d.SUTAMaximumAmount)-((convert(float,d.SUTARate)/100* (convert(integer,d.BaseSalary) / 12)*x.num)))*Power((1+convert(float,d.AnnualSalaryIncreasePercent)/100),Convert(int,x.num/12)) end, 2) AS Amount,         
FROM #Table AS x,  myTbl AS d            
WHERE (x.num >= 0) AND (x.num <= (DateDiff(M, d.StartDate, d.TerminationDate)) ) 

临时表

create TABLE #Table (               
num int NOT NULL,              
);               
;WITH Nbrs ( n ) AS (              
    SELECT 0 UNION ALL              
    SELECT 1 + n FROM Nbrs WHERE n < 99 )              
    INSERT #Table(num)              
    SELECT n FROM Nbrs              
OPTION ( MAXRECURSION 99 )  

此表在上面的查询中用作x

1 个答案:

答案 0 :(得分:0)

我创建了this SQLFiddle

-- Numbers table is probably a good idea
WITH Nbrs ( num ) AS 
(              
    SELECT 0 UNION ALL              
    SELECT 1 + num FROM Nbrs WHERE num < 99 
)

-- All columns, except for 'num' come from myTbl
SELECT dateadd(M, (num),S_Date) AS TheDate,
Round( 
  CASE 
  WHEN (SUTARate / 100) * (BaseSalary / 12) <= SUTAMaximumAmount
  THEN (SUTARate / 100) * (BaseSalary / 12)
  ELSE 0
  END
, 2) As Amount
-- This may be the number you were trying to multiply
,DatePart(Month, dateadd(M, (num),S_Date)) As PotentialMultiiplier
FROM Nbrs AS x,  myTbl AS d            
WHERE (num >= 0) 
AND (num <= (DateDiff(M, S_Date, T_Date)) ) 

我不完全确定你的目标是什么,但你可能在数字表的正确轨道上。因为您要使用的结果不会随着时间的推移而发生太大变化(即,几乎每个月的金额都为44美元),因此很难确定查询的正确代码。因此,我建议您提供一组不同的数据,以便更好地进行结果检查。

如果您在提供的链接中使用SQL,您可以使用更好的代码重新发布,然后我们可以更好地解决您的问题。