2013-03-21 82 views
2

我一直在計算工資變化率上的員工年度應納稅額。存儲過程計算應納稅額

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 

我試圖編寫存儲過程以計算應納稅額,但是我無法完成此操作。

有人可以幫忙嗎?

+0

你嘗試過什麼嗎?請發佈您的存儲過程 – DevelopmentIsMyPassion 2013-03-21 14:29:28

+0

本月第一天的分配公式是什麼? – Strawberry 2013-03-21 14:34:15

+0

@AshReva:最好不要在這裏發佈該代碼,它的混亂和它不遵循英文日曆。它基於尼泊爾日曆。 – hsuk 2013-03-22 03:40:34

回答

1

您需要一個將表中的記錄連接到具有下一個工資值的表中的記錄的sql語句......您還需要使用CTE(或其他** MySQL equivalent *存在)來生成所有沒有發生工資變化的月份。 * [感謝@納威的評論]

原諒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) 
+0

它實際上比這更復雜。你還必須在幾個月內增加薪資記錄(這一年的薪水記錄比一年中的月份少)。 – 2013-03-21 14:51:24

+0

http://stackoverflow.com/questions/1382573/how-do-you-use-the-with-clause-in-mysql – 2013-03-21 15:24:12