【问题标题】:Calculate wrong amount in query在查询中计算错误的金额
【发布时间】:2023-03-21 14:09:02
【问题描述】:

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

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

如果此金额小于SUTAMaximumAmount,则使用此金额,否则为 0。还有一件事,如果金额将保留且年份已完成,则从明年重新开始计算.. x.num 是保存 90 的临时表从 1 到 90 的数字

表格

 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 条记录?
  • 这只是我拥有的数据,我只从这个查询中得到结果@nrathaus
  • 什么意思?只需向我们展示您在 SQL 中作为#TablemyTbl 使用的表中的原始数据
  • 使用 convert(varchar(10), Date, 120) 来获取 YYYY-MM-DD 格式的日期。
  • 存在某种误解,您的问题是什么?正在显示的数据?返回值?无论如何,没有样本数据很难帮助你

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


【解决方案1】:

我创建了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,您可以用更好的代码重新发布,然后我们可以更好地解决您的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    • 2012-01-11
    • 2014-01-14
    • 2021-08-23
    • 1970-01-01
    相关资源
    最近更新 更多