在一个组中选择不同的值
Selecting distinct values within a a group
我想 select 由另一个变量定义的组中一个变量的不同值。最简单的方法是什么?
我的第一个想法是将 group by 和 distinct 结合起来,但它不起作用。我试过类似的东西:
select distinct col2, col1 from myTable
group by col1
我在这里看过这个,但似乎无法解决我的问题
_
Table 示例
无需分组,只需使用 distinct
select distinct col2, col1 from myTable
如果您的要求是在 col1 和 COL2 的情况下选择不同的组合,则无需分组,只需使用
SELECT DISTINCT COL1, COL2 FROM TABLE1;
但是如果你想自动分组然后每组显示一条记录然后你必须使用其中一列的聚合函数即
SELECT COL1, COUNT(COL2)
FROM TABLE1 GROUP BY COL1;
create table t as
with inputs(val, id) as
(
select 'A', 1 from dual union all
select 'A', 1 from dual union all
select 'A', 2 from dual union all
select 'B', 1 from dual union all
select 'B', 2 from dual union all
select 'C', 3 from dual
)
select * from inputs;
上面创建了你的 table,下面是解决方案(12c 及更高版本):
select * from t
match_recognize
(
partition by val
order by id
all rows per match
pattern ( a {- b* -} )
define b as val = a.val and id = a.id
);
输出:
此致,
拉纳加尔
我想 select 由另一个变量定义的组中一个变量的不同值。最简单的方法是什么? 我的第一个想法是将 group by 和 distinct 结合起来,但它不起作用。我试过类似的东西:
select distinct col2, col1 from myTable
group by col1
我在这里看过这个,但似乎无法解决我的问题
Table 示例
无需分组,只需使用 distinct
select distinct col2, col1 from myTable
如果您的要求是在 col1 和 COL2 的情况下选择不同的组合,则无需分组,只需使用
SELECT DISTINCT COL1, COL2 FROM TABLE1;
但是如果你想自动分组然后每组显示一条记录然后你必须使用其中一列的聚合函数即
SELECT COL1, COUNT(COL2)
FROM TABLE1 GROUP BY COL1;
create table t as
with inputs(val, id) as
(
select 'A', 1 from dual union all
select 'A', 1 from dual union all
select 'A', 2 from dual union all
select 'B', 1 from dual union all
select 'B', 2 from dual union all
select 'C', 3 from dual
)
select * from inputs;
上面创建了你的 table,下面是解决方案(12c 及更高版本):
select * from t
match_recognize
(
partition by val
order by id
all rows per match
pattern ( a {- b* -} )
define b as val = a.val and id = a.id
);
输出:
此致, 拉纳加尔