我有一张桌子如下
fab_id x y z m
12 14 10 3 5
12 10 10 3 4在这里,我在id .Now上使用group子句,我想减去那些具有类似id的列值。例如,由id (12)组成的群。现在减去(14-10)X,(10-10)Y,(3-3)z,(5-4)m
我知道这里有一个加和的函数和,但是有什么函数可以用来减去这个值吗?
或者有没有其他方法来达到这个结果。
注意-ve中可能会出现值的变化。任何函数都能处理这件事?
还有一个例子-( correction_date desc的订单结果将首先显示最近的修正)
fab_id x y z m correction_date
14 20 12 4 4 2014-05-05 09:03
14 24 12 4 3 2014-05-05 08:05
14 26 12 4 6 2014-05-05 07:12结果用on id (14)实现组。现在减去(26-20)X,(12-12)Y,(4-4)z,(6-4)m
发布于 2014-05-05 14:51:59
现在,您已经提供了关于如何处理更多记录的更多信息,并且您已经发现了一个涉及时间列的内容,下面是一个可能的解决方案。查询为每个fab_id选择第一条和最后一条记录,并减去以下值:
select
fab_info.fab_id,
earliest_fab.x - latest_fab.x,
earliest_fab.y - latest_fab.y,
earliest_fab.z - latest_fab.z,
earliest_fab.m - latest_fab.m
from
(
select
fab_id,
min(correction_date) as min_correction_date,
max(correction_date) as max_correction_date
from fab
group by fab_id
) as fab_info
inner join fab as earliest_fab on
earliest_fab.fab_id = fab_info.fab_id and
earliest_fab.min_correction_date = fab_info.min_correction_date
inner join fab as latest_fab on
latest_fab.fab_id = fab_info.fab_id and
latest_fab.min_correction_date = fab_info.max_correction_date;发布于 2014-05-05 11:29:20
如果您始终希望从最大值中减去最小值,则为:
select
fab_id,
max(x) - min(x),
max(y) - min(y),
max(z) - min(z),
max(m) - min(m)
from fab
group by fab_id;发布于 2014-05-05 11:52:35
正如您所说的,总是有两行,您可以简单地做一个‘自我连接’,并从对方中减去值:
SELECT t1.fab_id, t1.x - t2.x as diffx, t1.y - t2.y as diffy, <remainder columns here>
from <table> t1
inner join <table> t2 on t1.fab_id = t2.fab_id and t1.correctiondate > t2.correctiondate如果有超过两行,则需要进行子查询或使用窗口排序函数来计算每个fab_id的最大和最小校正日期,然后通过将这两个子查询连接在一起而不是
https://stackoverflow.com/questions/23471242
复制相似问题