>我有一个包含以下记录的表格:
Column A Column B
1 XX
2 XX
3 XX
4 XX
如何仅使用ColumnB
而不使用ColumnA
来显示1 条记录。 就像我应该说的
select from Table T where ColumnB ='XX'
并且只应返回1 行。
如果您真的不在乎返回哪一条匹配记录,只需使用rownum
伪列:
select * from Table T where ColumnB ='XX'
and rownum = 1;
此查询仅返回结果集中的第一行。这是获得一排的最便宜的方式。结果是不确定的,因为没有排序顺序,也不可能有:rownum
与 ORDER BY 配合不好,这就是为什么你不在乎哪一行返回很重要的原因。
您可以使用以下其中一项:
with t(colA,ColB) as
(
select 1,'XX' from dual union all
select 2,'XX' from dual union all
select 3,'XX' from dual union all
select 4,'XX' from dual
)
select ColB
from t
where ColumnB = 'XX' and rownum = 1;
或
select ColB from
(
with t(colA,ColB) as
(
select 1,'XX' from dual union all
select 2,'XX' from dual union all
select 3,'XX' from dual union all
select 4,'XX' from dual
)
select ColB,
row_number() over (order by ColB) as rn
from t
)
where ColumnB = 'XX' and rn=1;
或者如果你的数据库版本是12c
,这个也可以:
with t(colA,ColB) as
(
select 1,'XX' from dual union all
select 2,'XX' from dual union all
select 3,'XX' from dual union all
select 4,'XX' from dual
)
select ColB
from t
where ColumnB ='XX'
fetch {first|next} 1 {row|rows} only;
应首选关键字之一first
或next
和row
或rows
。
你可以这样做
select from Table T where ColumnB ='XX' group by ColunmB
既然你真的不在乎返回哪个a
,那么
SQL> with test (a, b) as
2 (select 1, 'xx' from dual union all
3 select 2, 'xx' from dual union all
4 select 3, 'yy' from dual
5 )
6 select min(a) a, min(b) b
7 from test
8 where b = 'xx';
A B
---------- --
1 xx
SQL>
谢谢大家的想法。
我试穿了下面,它对我有用,
从 A tale_name中选择a.column_name 其中 ROWID 在 ( 选择"从以下位置清除" ( 选择 ROWID RID,ROW_NUMBER(( 结束(按 a.column_name 按 ROWID 排序( RN 从 table_name a 哪里column_name (从diff_table_name中选择TO_CHAR(column_name( ( 其中 RN = 1