我有一个表测量,其数据如下所示。
week hips wrist abs weight
1 26.3 6.3 24.3 100
2 25.2 6.3 23.3 96
我试图从第2周到第1周得到结果
hips wrist abs weight
-1.1 0 -1 -4
我试着加入彼此的表并减去但我一直在重复。我该如何做到这一点?
答案 0 :(得分:2)
你试过这个吗?
select mprev.hips - m.hips as hips,
mprev.wrist - m.wrist as wrist,
mprev.abs - m.abs as abs,
mprev.weight - m.weight as weight
from measurements m join
measurements mprev
on me.week = mprev.week + 1;
答案 1 :(得分:1)
SELECT
a.[hips]-b.hips as Hips
, a.wrist - b.wrist as Wrist
, a.abs - b.abs as Abs
, a.weight - b.weight as Weight
FROM
tableName a
LEFT JOIN
tableName b
ON
a.week-1=b.week
答案 2 :(得分:1)
放手一搏:
SELECT VAR(hips) AS hips, VAR(wrist) AS wrist,
VAR(abs) AS abs, VAR(weight) AS weight
FROM measurements
WHERE week IN (1,2)
答案 3 :(得分:1)
如果出于某种原因,周数不合适,您也可以使用它:
;With Data As
(
Select hips, wrist, [abs], [weight],
Row_Number() Over (Order By Week) RN
From Measurements
)
Select Cur.Hips - Prev.Hips As Hips,
Cur.Wrist - Prev.Wrist As Wrist,
Cur.[Abs] - Prev.[Abs] As [Abs],
Cur.[Weight] - Prev.[Weight] As [Weight]
From Data Prev
Join Data Cur On Cur.RN = Prev.RN + 1