简体   繁体   English

如何在SQL的每个组中测试列的值顺序(基于时间戳)?

[英]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 byhaving

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.

相关问题 如何根据SQL中的条件选择组中的行? - How can I select a row within a group based on a condition in SQL? 如何有效地选择基于SQL中不同时间戳计算的多个总和的平均总和? - How do I effectively select the average sum of several sums being calculated based on different timestamps in SQL? MySql如何在该表中基于分组依据更新表? - MySql how do I update table based on group by within that table? 您如何根据不同列的值对一列中的值进行算术运算并按三分之一分组? - How do you run arithmetic on values in one column based on the values of a different column and group by a third? 如何创建SQL查询以获取另一列的每个GROUP&#39;ed BY值的列的唯一值 - How to create SQL query to get unique values of column for each GROUP'ed BY value of another column 如何根据另一列的值过滤一列? - How do I filter one column based on the values of another column? 如何为 MySQL 在 group by 中连接列的唯一值? - How do I concat a column's unique values in a group by for MySQL? 如何根据发生顺序对SQL查询中的数据进行分组 - How to group data in SQL query based on the sequence of occurance SQL - 如何在现有查询中相互减去两个时间戳 - SQL - How to subtract two timestamps from each other within an existing query 如何在 SQL 中将一列堆叠在一起? - How do I stack a column next to each other in SQL?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM