比较表中的2行

5ssjco0h  于 2021-06-26  发布在  Hive
关注(0)|答案(1)|浏览(126)

我有一张如下的表

accountNum  date  status  action qty time
----------  ----  ------  ------ --- ----
1234        2017   filled  B      10  11:20
1234        2017   filled  S      10  11:20
2345        2017   filled  B      20  12:00
2345        2017   filled  B      10  12:00
4444        2017   filled  B       5  01:00
4444        2017   filled  S       5  02:00

在这里,我想比较两行与行动“b”,然后行动“s”。如果在这些记录中发现两行的第一行是b,然后是s,我必须检查accountnum,date,time,status是否相同。
所以基于上面的测试数据,我应该只得到前2行

accountNum  date  status  action qty time
----------  ----  ------  ------ --- ----
1234        2017   filled  B      10  11:20
1234        2017   filled  S      10   11:20

对于这个问题,我应该写什么类型的查询?

li9yvcax

li9yvcax1#

我会先数一数你的钥匙

select  accountNum, date, status, time
from    yourTable
where   action in ('B', 'S')
group by accountNum, date, status, time
having  count(distinct action) = 2

然后,您可以将上面的表与初始表连接起来,以便只过滤所需的行

select  t1.*
from    yourTable t1
join    (
            select  accountNum, date, status, time
            from    yourTable
            where   action in ('B', 'S')
            group by accountNum, date, status, time
            having  count(distinct action) = 2
        ) t2
on      t1.accountNum = t2.accountNum and
        t1.date = t2.date and
        t1.status = t2.status and
        t1.time = t2.time

编辑
我不是HiveMaven,但如果 distinct 以及 having 不允许在子查询中使用,您可以这样编写查询

select  t1.*
from    yourTable t1
join    (
            select  accountNum, date, status, time, count(action) as cnt
            from    yourTable
            where   action in ('B', 'S')
            group by accountNum, date, status, time
        ) t2
on      t1.accountNum = t2.accountNum and
        t1.date = t2.date and
        t1.status = t2.status and
        t1.time = t2.time
where   t2.cnt = 2

你可以摆脱 distinct 如果相同的话 accountNum / date / time / status 组合不能有同一操作的多个示例。
这个 having 子句可以作为 where 条件。

相关问题