2012-03-05 247 views
2

表1如何計算百分比

Date  id salary 

01/2012 001 1500 
01/2011 001 750 
01/2010 001 500 

從表1,我想打一個百分比爲datewise

預計輸出

Date id salary percentage 

01/2012 001 1500 100% 
01/2011 001 750 50% 
01/2010 001 500 0% 

條件

01/2010 salary is 500, then 01/2011 salary is 750, so 50 percentage increased from 01/2010 salary 
01/2011 salary is 750, then 01/2012 salary is 1500, so 100 percentage increase from 02/2011 salary 

如何製作一個查詢計算百分比。

需要查詢幫助

回答

3

正如@richardtallent所說,有兩個部分:(1)找到以前的工資,(2)計算百分比。但是由於您使用的是sqlserver 2k,CTE不受支持。我用做下列方式:

對於(2)我通常有兩個功能創建:百分比和增量:

CREATE FUNCTION [dbo].[percentage] (@a float(53), @b float(53)) 
RETURNS float(53) 
BEGIN 
    RETURN (CASE WHEN COALESCE(@b,0)=0 THEN NULL ELSE @a/@b END)*100. 
END 

而且

CREATE FUNCTION [dbo].[increment] (@a float(53), @b float(53)) 
RETURNS float(53) 
BEGIN 
    RETURN dbo.percentage(@a,@b)-100. 
END 

:(1)你可以使用然後你有它。

SELECT S.date, S.id, S.salary, 
     dbo.increment((SELECT TOP 1 salary 
         FROM table1 
         WHERE id=S.id AND date<S.date 
         ORDER BY date DESC), 
        salary) AS percentage 
FROM table1 S 
0

有兩個部分對這個問題:(1)尋找對於任何給定的工資以前的薪水,和(2)計算百分比。

第一部分:

WITH salarieswithprevious AS (
    SELECT date, id, salary, 
     COALESCE(SELECT salary FROM table t2 WHERE t1.id=t2.id AND t1.date > t2.date 
     AND NOT EXISTS(SELECT NULL FROM table t3 WHERE t3.id=t1.id 
      AND t3.date > t2.date AND t3.date < t1.date), salary) AS previoussalary 
    FROM 
     table t1 
    ) 

這與薪水之前最近的每個薪金匹配。如果沒有以前的薪水,它會將價值與當前的工資相結合,這意味着該年的「加薪」將爲0%。

第二部分:

SELECT date, id, salary, 
    ((salary - previoussalary)/previoussalary * 100) AS PctIncrease 
FROM 
    salarieswithprevious 

這確實比較容易的部分,簡單的百分比數學。

+0

不能與SQL Server 2000 – 2012-03-05 12:13:01

0

在我看來,更好的你去光標,先通過改變表中創建列,並通過光標表

declare @date datetime,@id int,@salary numeric,@percentage decimal(5,2) 
declare SalCursor cursor static 
    FOR SELECT date,id,Salary FROM table1 
    open SalCursor 
    fetch last FROM SalCursor into @date,@id,@salary 
    while @@fetch_status<>-1 
    begin 
    --code to calculate percentage goes here 
    Update table1 set [email protected] where [email protected] 
    end 

希望它可以給你一些粗略的想法讀取最後一行... 所有最好的..

0

你可以通過執行類似的CTE做到這一點:

WITH CTETable (Row, Date, id, salary) AS 
(
    SELECT Row_Number() over (order by id,Date) AS Row , 
    Date, 
     id, 
    salary 
    FROM sourceTable 
) 
SELECT 
    T1.Row, 
    T1.Date, 
     T1.id 
    T1.salary, 
    CASE 
        -- First row in table, return 0 for Pct 
     WHEN T1.Row = 1 THEN 0 
        -- Switching ids, return 0 for Pct 
        WHEN T1.id <> (SELECT T2.id FROM CTETable AS T2 
           WHERE T2.Row = T1.Row - 1) THEN 0 
        -- else subtract the prior row's salary from the current row 
        -- and divide by the prior row's salary 
     ELSE (T1.salary - (SELECT T2.salary FROM CTETable AS T2 
       WHERE T2.Row = T1.Row - 1) * 1.0)/
      ((SELECT T3.salary FROM CTETable AS T3 
       WHERE T3.Row = T1.Row - 1) * 1.0) 
    END * 100 AS Pct 
FROM CTETable AS T1 
+0

做到這一點不能使用這個在SQL服務器2000 – 2012-03-05 12:09:06

0

試試這個

create table #tr(dates datetime,id int,salary int) 
insert into #tr values('01/01/2012' , 001 , 1500) 
insert into #tr values('01/01/2011' ,001, 750) 
insert into #tr values('01/01/2010' , 001 , 500); 

WITH tablename as 
(

SELECT ROW_NUMBER()over(PARTITION by t1.dates order by t1.dates)rno, T1.*,isnull((cast((T1.salary - isnull(T2.salary,T1.salary))as decimal(16,3))/T2.salary),0)*100 as 'Percentage' FROM #tr T1 
left JOIN #tr T2 on T1.id = T2.ID and T1.dates >T2.dates 

) 

SELECT * FROM tablename where rno = 1  

drop table #tr 
+0

在SQL服務器不能使用ROW_NUMBER 2000 – 2012-03-05 12:08:31

+0

然後用哈希表嘗試添加一個額外的領域 – Nighil 2012-03-06 04:21:02

+0

聽說表,臨時表和表變量。但我從來沒有聽說過MSSQL中的散列表 – 2012-03-06 08:07:05

0

試試這個:它更加動態:

create table #tr(dates datetime,id int,salary int) 
insert into #tr values('01/01/2012' , 001 , 1500) 
insert into #tr values('01/01/2011' ,001, 750) 
insert into #tr values('01/01/2010' , 001 , 500) 
insert into #tr values('01/01/2011' ,002, 750) 
insert into #tr values('01/01/2010' , 002 , 500) 

with T as (
select ID, dates incDate, Salary, ROW_NUMBER() over(partition by id order by id, Dates) AS rn from #tr) 
select T.id,T1.salary ,T.incdate,(T1.Salary-T.salary)*100/T.Salary as PercHike from T join T T1 on T.id=T1.id and T.rn=(T1.rn-1) 
UNION 
select id,min(Salary) as Salary ,min(dates) as Incdate , 0 as PercHike from #tr group by Id 
ORDER BY ID , incDate 

drop table #tr 

在場景中ID不同,早期的建議解決方案進行行的事。

+0

不能在sql-server 2000中使用row_number – 2012-03-05 12:07:24