【问题标题】:How to find the desired output using hive/sql如何使用 hive/sql 找到所需的输出
【发布时间】:2021-06-25 23:12:24
【问题描述】:

我有一张这样的桌子

col1 col2
First row
Second a
First b
Second row
First c
Second row

所需的输出如下:

col1 col2 col3
First row 1
Second a 1
First b 1
Second row 2
First c 2
Second row 3

逻辑是,每当我们在 col2 中获取值“row”时,col3 将递增计数器,否则将跳过它。 请帮忙。

【问题讨论】:

    标签: mysql sql hive


    【解决方案1】:

    您需要一个指定排序的列,因为 SQL 表表示无序集。

    有了这样的列,只要使用条件聚合即可:

    select t.*,
           sum(case when col2 = 'myval' then 1 else 0 end) over (order by <ordering col>) as col3
    from t;
    

    【讨论】:

    • 非常感谢,它就像一个魅力。
    • @N_A 。 . .如果这回答了您的问题,您可以接受答案。
    猜你喜欢
    • 1970-01-01
    • 2022-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多