简体   繁体   English

如何使用 hive/sql 找到所需的输出

[英]How to find the desired output using hive/sql

I have a table like this我有一张这样的桌子

col1第 1 列 col2列2
First第一的 row
Second第二 a一种
First第一的 b
Second第二 row
First第一的 c C
Second第二 row

The output required is like below:所需的输出如下所示:

col1第 1 列 col2列2 col3第 3 列
First第一的 row 1 1
Second第二 a一种 1 1
First第一的 b 1 1
Second第二 row 2 2
First第一的 c C 2 2
Second第二 row 3 3

The logic is , whenever we are getting the value "row" in col2, the col3 will increment the counter, otherwise will skip it.逻辑是,每当我们在 col2 中获取值“row”时,col3 将增加计数器,否则将跳过它。 Please help.请帮忙。

You need a column that specifies ordering because SQL tables represent unordered sets.您需要一个指定排序的列,因为 SQL 表表示无序集合。

With such a column, just use conditional aggregation:对于这样的列,只需使用条件聚合:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM