I want to find the difference between two rows on the same column group by id
我想通过id找出同一列组中的两行之间的差异
ID Value1 Value2
a 500 200
b 300 200
a 100 300
b 300 400
....
Expected output
预期的输出
ID Value1 Value2
a 400 -100
b 0 -200
....
How to make a query for the above condition.
如何对上述条件进行查询。
4 个解决方案
#1
3
You can use following:
您可以使用:
SELECT
ID,
MAX(Value1) - MIN(Value1),
MIN(Value2) - MAX(Value2)
FROM
myTableName
GROUP BY
ID
But there is one assumption: the second row has always greater Value1
and lower Value2
than first one.
但是有一个假设:第二行总是比第一行有更大的值1,更低的值2。
#2
1
You can try:
你可以尝试:
SELECT t1.ID, max(t2.VALUE1 - t1.VALUE1)
FROM TABLE1 t1
left join TABLE1 t2 on t1.id = t2.id
group by t1.id
SQL小提琴演示:
#3
0
This query will give a absolute difference between max value of ID and min value of same ID:
此查询将给出ID的最大值与相同ID的最小值之间的绝对值:
SELECT ID
, ABS(MAX(VALUE1) - MIN(VALUE1)) AS v1Diff
, ABS(MAX(VALUE2) - MIN(VALUE2)) AS v2Diff
FROM TABLE1
GROUP BY ID
Sql Fiidle
But if you want get a real difference(negative diff) then we need to know which row is first and which row is next. Then we can count difference like firstRowValue - nextRowValue
.
但是如果你想要得到一个真实的差值(- diff)那么我们需要知道哪一行是第一行,哪一行是下一行。然后我们可以像firstRowValue - nextRowValue那样计算差异。
Maybe your table has some RowID
or DateTime
column from where we can ordering a rows from same ID
. What column/columns are Primery Key
in your table?
也许您的表有一些RowID或DateTime列,我们可以在其中从相同的ID排序行。
#4
0
Use option with CTE and ROW_NUMBER() ranking function
使用CTE和ROW_NUMBER()排序函数的选项
;WITH cte AS
(
SELECT ID,
CASE ROW_NUMBER() OVER(PARTITION BY ID ORDER BY 1/0) % 2
WHEN 1 THEN Value1
WHEN 0 THEN -1 * Value1 END AS Value1,
CASE ROW_NUMBER() OVER(PARTITION BY ID ORDER BY 1/0) % 2
WHEN 1 THEN Value2
WHEN 0 THEN -1 * Value2 END AS Value2
FROM dbo.test22
)
SELECT ID, SUM(Value1) AS Value1, SUM(Value2) AS Value2
FROM cte
GROUP BY ID
Demo on SQLFiddle
演示在SQLFiddle
#1
3
You can use following:
您可以使用:
SELECT
ID,
MAX(Value1) - MIN(Value1),
MIN(Value2) - MAX(Value2)
FROM
myTableName
GROUP BY
ID
But there is one assumption: the second row has always greater Value1
and lower Value2
than first one.
但是有一个假设:第二行总是比第一行有更大的值1,更低的值2。
#2
1
You can try:
你可以尝试:
SELECT t1.ID, max(t2.VALUE1 - t1.VALUE1)
FROM TABLE1 t1
left join TABLE1 t2 on t1.id = t2.id
group by t1.id
SQL小提琴演示:
#3
0
This query will give a absolute difference between max value of ID and min value of same ID:
此查询将给出ID的最大值与相同ID的最小值之间的绝对值:
SELECT ID
, ABS(MAX(VALUE1) - MIN(VALUE1)) AS v1Diff
, ABS(MAX(VALUE2) - MIN(VALUE2)) AS v2Diff
FROM TABLE1
GROUP BY ID
Sql Fiidle
But if you want get a real difference(negative diff) then we need to know which row is first and which row is next. Then we can count difference like firstRowValue - nextRowValue
.
但是如果你想要得到一个真实的差值(- diff)那么我们需要知道哪一行是第一行,哪一行是下一行。然后我们可以像firstRowValue - nextRowValue那样计算差异。
Maybe your table has some RowID
or DateTime
column from where we can ordering a rows from same ID
. What column/columns are Primery Key
in your table?
也许您的表有一些RowID或DateTime列,我们可以在其中从相同的ID排序行。
#4
0
Use option with CTE and ROW_NUMBER() ranking function
使用CTE和ROW_NUMBER()排序函数的选项
;WITH cte AS
(
SELECT ID,
CASE ROW_NUMBER() OVER(PARTITION BY ID ORDER BY 1/0) % 2
WHEN 1 THEN Value1
WHEN 0 THEN -1 * Value1 END AS Value1,
CASE ROW_NUMBER() OVER(PARTITION BY ID ORDER BY 1/0) % 2
WHEN 1 THEN Value2
WHEN 0 THEN -1 * Value2 END AS Value2
FROM dbo.test22
)
SELECT ID, SUM(Value1) AS Value1, SUM(Value2) AS Value2
FROM cte
GROUP BY ID
Demo on SQLFiddle
演示在SQLFiddle