[英]How do I test the sequence of values of a column (based on timestamps) within each group in SQL?
I have colA, colB, colC in tableA where colC is a timestamp variable. 我在tableA中有colA,colB,colC,其中colC是一个时间戳变量。
there are 5 possible values for colB. colB有5个可能的值。
select distinct(colB) from tableA;
x
y
z
I want to make sure that for each value of colA, the record(s) with colB='a' has earlier timestamps than record(s) with colB='b' and so on. 我想确保对于colA的每个值,具有colB ='a'的记录比具有colB ='b'的记录具有更早的时间戳,依此类推。 So, colB='b' records should come after colB='a' and colB='c' records should come after colB='b' for each value of colA 因此,对于colA的每个值,colB ='b'记录应在colB ='a'之后,而colB ='c'记录应在colB ='b'之后
I need a SQL query for the same 我需要相同的SQL查询
If I understand correctly, you can use group by
and having
: 如果我理解正确,则可以使用group by
并having
:
select cola
from tableA
group by cola
having max(case when colb = 'a' then timestamp end) < min(case when colb = 'b' then timestamp end) and
max(case when colb = 'b' then timestamp end) < min(case when colb = 'c' then timestamp end) and
. . .
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.