如何 select 不同的记录在 oracle 中?
how to select distinct records in oracle?
我想在某些情况下 select 不同的行,但是当我在 select 语句中添加 id 列时,它 returns 所有行???
下面的查询工作正常
select distinct dst_bnk_acnt_id
,dst_cust_id
,org_cust_id
,dst_pos_id
,pmt_typ_cd
from tb_cm_t_pmt_coll
where org_pos_id = 8 OR dst_pos_id = 8 OR dst_bnk_acnt_id = 1 ;
但是当我想 select 使用 order by id(desc) 的最新记录时 returns 所有行!
SELECT distinct id
,dst_bnk_acnt_id
,dst_cust_id
,org_cust_id
,dst_pos_id
,pmt_typ_cd
FROM tb_cm_t_pmt_coll
WHERE org_pos_id = 8 OR dst_pos_id = 8 OR dst_bnk_acnt_id = 1
ORDER BY id DESC;
我知道 'id' 列是主键,它的所有值都是唯一的,因此所有行都变得唯一。
我只想 select 使用这些 [dst_bnk_acnt_id,dst_cust_id,org_cust_id,dst_pos_id,pmt_typ_cd]
列的不同行,但我也想使用 id 按降序排列它们。
请帮忙。
我没有你的表,所以我将使用 Scott 的示例架构。
不同的部门和职位是:
SQL> select distinct deptno, job from emp;
DEPTNO JOB
---------- ---------
20 CLERK
30 SALESMAN
20 MANAGER
30 CLERK
10 PRESIDENT
30 MANAGER
10 CLERK
10 MANAGER
20 ANALYST
9 rows selected.
我们希望按 EMPNO
(类似于您的 ID
)对数据进行排序:
SQL> select distinct deptno, job from emp order by empno;
select distinct deptno, job from emp order by empno
*
ERROR at line 1:
ORA-01791: not a SELECTed expression
SQL>
它不会起作用(如您所知)。但是,如果您使用子查询(或 CTE),那么您会得到:
SQL> with temp as
2 (select min(empno) id, deptno, job
3 from emp
4 group by deptno, job
5 order by 1 desc
6 )
7 select deptno, job
8 From temp
9 order by id desc;
DEPTNO JOB
---------- ---------
10 CLERK
30 CLERK
10 PRESIDENT
20 ANALYST
10 MANAGER
30 MANAGER
20 MANAGER
30 SALESMAN
20 CLERK
9 rows selected.
SQL>
这意味着您的查询可能如下所示:
WITH temp
AS ( SELECT MIN (id) id,
dst_bnk_acnt_id,
dst_cust_id,
org_cust_id,
dst_pos_id,
pmt_typ_cd
FROM tb_cm_t_pmt_coll
WHERE org_pos_id = 8
OR dst_pos_id = 8
OR dst_bnk_acnt_id = 1
GROUP BY dst_bnk_acnt_id,
dst_cust_id,
org_cust_id,
dst_pos_id,
pmt_typ_cd)
SELECT dst_bnk_acnt_id,
dst_cust_id,
org_cust_id,
dst_pos_id,
pmt_typ_cd
FROM temp
ORDER BY id DESC;
我会为此使用 window 函数:
select id,
dst_bnk_acnt_id,
dst_cust_id,
org_cust_id,
dst_pos_id,
pmt_typ_cd
FROM (
SELECT id,
dst_bnk_acnt_id,
dst_cust_id,
org_cust_id,
dst_pos_id,
pmt_typ_cd,
row_number() over (partition by dst_bnk_acnt_id,dst_cust_id,org_cust_id,dst_pos_id,pmt_typ_cd
order by id desc) as rn
FROM tb_cm_t_pmt_coll
WHERE org_pos_id = 8
OR dst_pos_id = 8
OR dst_bnk_acnt_id = 1
)
WHERE rn = 1
ORDER BY id;
window 函数中的 order by id desc
为具有最高 id
的行提供行号 1,为不同列的每个组合选择最新的 id
.
我想在某些情况下 select 不同的行,但是当我在 select 语句中添加 id 列时,它 returns 所有行???
下面的查询工作正常
select distinct dst_bnk_acnt_id
,dst_cust_id
,org_cust_id
,dst_pos_id
,pmt_typ_cd
from tb_cm_t_pmt_coll
where org_pos_id = 8 OR dst_pos_id = 8 OR dst_bnk_acnt_id = 1 ;
但是当我想 select 使用 order by id(desc) 的最新记录时 returns 所有行!
SELECT distinct id
,dst_bnk_acnt_id
,dst_cust_id
,org_cust_id
,dst_pos_id
,pmt_typ_cd
FROM tb_cm_t_pmt_coll
WHERE org_pos_id = 8 OR dst_pos_id = 8 OR dst_bnk_acnt_id = 1
ORDER BY id DESC;
我知道 'id' 列是主键,它的所有值都是唯一的,因此所有行都变得唯一。
我只想 select 使用这些 [dst_bnk_acnt_id,dst_cust_id,org_cust_id,dst_pos_id,pmt_typ_cd]
列的不同行,但我也想使用 id 按降序排列它们。
请帮忙。
我没有你的表,所以我将使用 Scott 的示例架构。
不同的部门和职位是:
SQL> select distinct deptno, job from emp;
DEPTNO JOB
---------- ---------
20 CLERK
30 SALESMAN
20 MANAGER
30 CLERK
10 PRESIDENT
30 MANAGER
10 CLERK
10 MANAGER
20 ANALYST
9 rows selected.
我们希望按 EMPNO
(类似于您的 ID
)对数据进行排序:
SQL> select distinct deptno, job from emp order by empno;
select distinct deptno, job from emp order by empno
*
ERROR at line 1:
ORA-01791: not a SELECTed expression
SQL>
它不会起作用(如您所知)。但是,如果您使用子查询(或 CTE),那么您会得到:
SQL> with temp as
2 (select min(empno) id, deptno, job
3 from emp
4 group by deptno, job
5 order by 1 desc
6 )
7 select deptno, job
8 From temp
9 order by id desc;
DEPTNO JOB
---------- ---------
10 CLERK
30 CLERK
10 PRESIDENT
20 ANALYST
10 MANAGER
30 MANAGER
20 MANAGER
30 SALESMAN
20 CLERK
9 rows selected.
SQL>
这意味着您的查询可能如下所示:
WITH temp
AS ( SELECT MIN (id) id,
dst_bnk_acnt_id,
dst_cust_id,
org_cust_id,
dst_pos_id,
pmt_typ_cd
FROM tb_cm_t_pmt_coll
WHERE org_pos_id = 8
OR dst_pos_id = 8
OR dst_bnk_acnt_id = 1
GROUP BY dst_bnk_acnt_id,
dst_cust_id,
org_cust_id,
dst_pos_id,
pmt_typ_cd)
SELECT dst_bnk_acnt_id,
dst_cust_id,
org_cust_id,
dst_pos_id,
pmt_typ_cd
FROM temp
ORDER BY id DESC;
我会为此使用 window 函数:
select id,
dst_bnk_acnt_id,
dst_cust_id,
org_cust_id,
dst_pos_id,
pmt_typ_cd
FROM (
SELECT id,
dst_bnk_acnt_id,
dst_cust_id,
org_cust_id,
dst_pos_id,
pmt_typ_cd,
row_number() over (partition by dst_bnk_acnt_id,dst_cust_id,org_cust_id,dst_pos_id,pmt_typ_cd
order by id desc) as rn
FROM tb_cm_t_pmt_coll
WHERE org_pos_id = 8
OR dst_pos_id = 8
OR dst_bnk_acnt_id = 1
)
WHERE rn = 1
ORDER BY id;
window 函数中的 order by id desc
为具有最高 id
的行提供行号 1,为不同列的每个组合选择最新的 id
.