Sql 蜂巢滞后窗口分区
这是我的桌子:Sql 蜂巢滞后窗口分区,sql,hive,hiveql,window-functions,partitioning,Sql,Hive,Hiveql,Window Functions,Partitioning,这是我的桌子: sensor_name, ext_value, int_value, growth 47ACXVMACSENS01, 238, 157, 1 47ACXVMACSENS01, 157, 256, 2 47ACXVMACSENS01, 895, 345, 3 47ACXVMACSENS01, 79, 861, 3 91DKCVMACSENS02, 904, 858, 1 91DKCVMACSENS02, 925, 588, 1 91DKCVMACSENS02, 15, 738, 1
sensor_name, ext_value, int_value, growth
47ACXVMACSENS01, 238, 157, 1
47ACXVMACSENS01, 157, 256, 2
47ACXVMACSENS01, 895, 345, 3
47ACXVMACSENS01, 79, 861, 3
91DKCVMACSENS02, 904, 858, 1
91DKCVMACSENS02, 925, 588, 1
91DKCVMACSENS02, 15, 738, 1
91DKCVMACSENS02, 77, 38, 2
前三列(传感器名称、外部值、内部值)是给定的数据,第四列是我想要的计算列,此增长列基于每组传感器名称的列值(外部值、内部值)
生长柱的结果计算如下:
对于每组传感器_名称,将每行的int_值与前一行的ext_值进行比较,如果没有前一行,则其ext_值为0,如果当前行的int_值高于前一行的ext_值,则增长值增加1。如果当前的int_值低于上一行的ext_值,则增长保持与上一行的增长值相同的值
在上面的例子中
for the very first row, 157 is compared with the previous row ext_value that doesn't exist so it's 0,
157 > 0 then growth value increase of 1 from 0.
on the 2nd row, 256 > 238 then growth = 1+1=2
on the 3rd row, 345 > 159 then growth = 2+1=3
on the 4th row, 861 < 895 then growth remains at the same previous value, so 3.
then the logic is re-applied to the second set of sensor_name :
1st row, 858 > 0 (because there is now previous row for this sensor_name) then growth = 1
2nd row, 588 < 904 then growth = 1
3rd row, 738 < 925 then growth = 1
4th row, 38 > 15 then growth = 1+1=2
对于第一行,157与不存在的前一行ext_值进行比较,因此为0,
157>0,则增长值从0增加1。
在第二行,256>238,然后增长=1+1=2
在第三行,345>159,然后增长=2+1=3
在第4行,861<895,则增长保持与先前的值相同,因此为3。
然后,将该逻辑重新应用于第二组传感器名称:
第1行,858>0(因为此传感器名称现在有上一行),然后增长=1
第二排,588<904,然后增长=1
第三排,738<925,然后增长=1
第四排,38>15,然后增长=1+1=2
我尝试过在sensor_name分区上使用滞后窗口,但直到现在它才给出正确的结果
如何解决此问题?使用lag获取上一个ext_值,计算增长标志并使用运行计数计算增长。 正如您在评论中所说,我添加了rcv_时间栏:
with your_table as ( --use your table instead of this
select stack(8,
'47ACXVMACSENS01', 238, 157, '2019-11-01 10:10:01',
'47ACXVMACSENS01', 157, 256, '2019-11-01 10:10:02',
'47ACXVMACSENS01', 895, 345, '2019-11-01 10:10:03',
'47ACXVMACSENS01', 79, 861, '2019-11-01 10:10:04',
'91DKCVMACSENS02', 904, 858, '2019-11-01 10:10:05',
'91DKCVMACSENS02', 925, 588, '2019-11-01 10:10:06',
'91DKCVMACSENS02', 15, 738, '2019-11-01 10:10:07',
'91DKCVMACSENS02', 77, 38, '2019-11-01 10:10:08'
) as (sensor_name, ext_value, int_value, rcv_time )
)
select sensor_name, ext_value, int_value,
count(case when int_value>prev_ext_value then true end) over(partition by sensor_name order by rcv_time) growth
from
(
select sensor_name, ext_value, int_value, rcv_time,
lag(ext_value,1,0) over(partition by sensor_name order by rcv_time) prev_ext_value
from your_table
)s;
结果:
47ACXVMACSENS01 238 157 1
47ACXVMACSENS01 157 256 2
47ACXVMACSENS01 895 345 3
47ACXVMACSENS01 79 861 3
91DKCVMACSENS02 904 858 1
91DKCVMACSENS02 925 588 1
91DKCVMACSENS02 15 738 1
91DKCVMACSENS02 77 38 2
生成的结果与示例中的结果完全相同数据的问题在于无法定义“前一行”,因为似乎没有任何列定义行的顺序。为了能够使用LAG分析函数,您应该按列指定顺序,以便该函数能够正确地拾取前一行。例如,示例中的最后三行:为什么它们的顺序是这样的?如果依赖文件中的顺序,这是个坏主意,因为它是并行读取的,而不是顺序读取的。表未在配置单元中排序。您需要一些序列或时间戳才能选择上一行。请阅读关于行顺序的回答:数据是这样排序的,因为有一列rcv_time指示接收这些数据的时间,格式为yyyy-MM-dd HH:MM:ss,我在这里不表示从最近到最近的时间,可以吗将其添加到数据示例中