表1
Date id salary
01/2012 001 1500
01/2011 001 750
01/2010 001 500
从table1,我想为日期
制作一个百分比预期输出
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
如何进行查询以计算百分比。
需要查询帮助
答案 0 :(得分: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)你可以使用一个subselect,然后你就拥有它。
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
答案 1 :(得分: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
这是简单的部分,简单的百分比数学。
答案 2 :(得分: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 percentage=@percentage where id=@id
end
希望它给你一些粗略的想法...... 一切顺利..
答案 3 :(得分:0)
您可以通过执行以下操作来执行此操作:
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
答案 4 :(得分: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
答案 5 :(得分: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不同的情况下,先前建议的解决方案执行。