我有问题。我有一个包含三列(用户,值,日期)的表。像这样:
User VALUE DATE
U1 23 30-07-2015
U2 47 30-07-2015
U3 43 30-07-2015
U1 21 29-07-2015
U2 22 29-07-2015
U3 56 29-07-2015
我想以这种方式从列Value中减去值:
23-21; 47-22; 43-56;
(相同的用户但不同的日期)
我不知道我怎么能这样做...请帮助我! :)
答案 0 :(得分:0)
试试这个。
select T1.value-T2.value from your_table T1
INNER JOIN
your_table T2 ON T1.USER=T2.USER
WHERE T1.date<>T2.date
答案 1 :(得分:0)
试试这个
with cte as
(
select RN=row_number() over(partition by user order by [date] desc),
[User],
VALUE
from yourtable
)
select [User],
DIFF=MAX(case when rn= 1 then VALUE end)-MAX(case when rn= 2 then VALUE end)
from cte
Group by [user]