mysql - 存储过程计算应纳税额

标签 mysql sql database stored-procedures

我一直在根据不断变化的工资率计算员工的年度应纳税额。

salary_assigned_date | salary
-------------------------------
    2011-12-06          5000
    2012-01-05          10000
    2012-02-10          15000
    2012-04-08          20000
    2012-08-01          28000

现在,我 2012 年的应税金额(按月计算)应该是这样的:

我假设没有。一个月中的天数为 30。

month   |   taxable_amount
-----------------------------------------------
  01            833.33 + 8333.33   /* Since salary has been changed 
                                      at 6th of month, 
                                      for 5 days, 
                                      taxable amount = 5000/30*5 
                                      => 833.33 
                                      and for remaining 25 days
                                      = 10000/30*25=> 8333.33
                                      and same case for remaining months.*/
  02            3000 + 10500
  03            15000
  04            4666.67 + 15333.33
  05            20000
  06            20000
  07            20000
  08            933.33 + 27066.67
  09            28000
  10            28000
  11            28000
  12            28000

我试图编写一个存储过程来计算应纳税额,但我无法完成。

有人可以帮忙吗?

最佳答案

您需要一个 sql 语句,将表中的记录连接到表中具有下一个薪水值的记录...您还需要使用 CTE(或任何存在的 ** MySQL equivalent * )来生成工资没有变化的所有月份。 * [感谢@Neville 的评论]

请原谅 SQL 服务器语法,我不会为您查找 MySQL 等价物...意图应该很清楚。我知道 MySQL 有自己的函数,等同于 SQL 服务器的日期函数 getdate()DateDiff()DateAdd()Day().

 With Dates(dt) As
 ( Select min(salary_assigned_date) 
   From yourTable 
   Union All
   Select DateAdd(month,1, dt)
   from dates
   where dt < getdate())  -- replace getdate() with parameter for max date to calculate

  -- If MySQL has no equivalent to CTE, you need to generate a temp table with 
  -- these dates in it and use that instead of the [Dates] construction

   Select t.dt, t.salary/30.0 * (day(t.dt)-1) +
        + n.salary/30.0 * (31 - day(t.dt))
   From Dates d 
      join yourTable t On t.salary_assigned_date = 
                    (Select Min(salary_assigned_date) 
                     From test where salary_assigned_date >= d.dt)
      join yourTable n On n.salary_assigned_date = 
                    (Select Min(salary_assigned_date) 
                      From test where salary_assigned_date > d.dt)

   Select t.salary/30.0 * (day(t.salary_assigned_date)-1) +
        + n.salary/30.0 * (31 - day(t.salary_assigned_date))
   From table t
       join table n On n.salary_assigned_date =
                    (Select Min(salary_assigned_date) From table
                     Where salary_assigned_date > t.salary_assigned_date)

关于mysql - 存储过程计算应纳税额,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15549936/

相关文章:

mysql - 使用 docker-compose 自动构建(应用程序+mySQL 服务器)

c# - 动态生成 IQueryable 的查找键

mysql - SQL:按多个字段分组

sql - 如何在 SQL Server 中进行 10% 45% 45% 拆分

MySQL - 循环而

php - MySQL更改系统日期没有达到预期的结果

mysql - 数据库中最近的自由坐标

sql - MySQL 使用哪个版本?

MySQL查询以获取范围内的计数

python-3.x - Dataframe序列检测: Find groups where three rows in a row have negative values