我需要一起返回前4列的select distinct,并返回剩下的所有其他列。例如,如果我有:
DEPTNO LOC JOB SAL ENAME
---------- ------------- --------- ---------- ----------
10 NEW YORK CLERK 1300 MILLER
10 NEW YORK MANAGER 2450 CLARK
10 NEW YORK CLERK 1300 KING
20 DALLAS ANALYST 3000 FORD
20 DALLAS ANALYST 3000 SCOTT
20 DALLAS CLERK 800 SMITH
20 DALLAS CLERK 1100 ADAMS
20 DALLAS MANAGER 2975 JONES
30 CHICAGO CLERK 950 JAMES
30 CHICAGO MANAGER 2850 BLAKE
30 CHICAGO SALESMAN 1250 MARTIN
30 CHICAGO SALESMAN 1250 WARD
30 CHICAGO SALESMAN 1500 TURNER
30 CHICAGO SALESMAN 1600 ALLEN
我需要回复:
DEPTNO LOC JOB SAL ENAME
---------- ------------- --------- ---------- ----------
10 NEW YORK CLERK 1300 MILLER
10 NEW YORK MANAGER 1300 CLARK
20 DALLAS ANALYST 3000 SCOTT
20 DALLAS CLERK 800 SMITH
20 DALLAS CLERK 1100 ADAMS
20 DALLAS MANAGER 2975 JONES
30 CHICAGO CLERK 950 JAMES
30 CHICAGO MANAGER 2850 BLAKE
30 CHICAGO SALESMAN 1250 MARTIN
30 CHICAGO SALESMAN 1500 TURNER
30 CHICAGO SALESMAN 1600 ALLEN
换句话说,如果前4列是唯一的(togheter),我将返回整行。
我尝试使用select distinct
,但我只获得了我在命令中写的列:
select distinct DEPTNO,LOC,JOB,SAL from my_table
不会返回ENAME。
我尝试使用内连接,但是从表中获取所有列。
感谢名单
答案 0 :(得分:2)
您尚未指定希望如何在多个ENAME之间进行选择。在此示例中,它按字母顺序返回第一个:
SELECT DISTINCT
deptno
,loc
,job
,sal
,MIN(ename)
OVER (PARTITION BY deptno, loc, job, sal)
AS ename
FROM my_table;
答案 1 :(得分:0)
你也可以这样试试
Example :
create table ttt
(
DEPTNO int ,
LOC varchar(20),
JOB varchar(20),
SAL int,
ENAME varchar(20)
)
-
insert into
ttt
values(10,'NEW YORK','CLERK',1300,'MILLER'),
(10,'NEW YORK','MANAGER',2450,'CLARK'),
(10,'NEW YORK','MANAGER',2450,'KING')
-
SELECT
DISTINCT
DEPTNO,
LOC,
JOB,
SAL,
MAX(ENAME) from ttt
GROUP BY
DEPTNO,
LOC,
JOB,
SAL
这是
的SQL小提琴