我在Access SQL语句上挣扎。我试着在这里找到一些答案,但似乎没有任何问题可以涵盖我的意思:
我需要通过某个标题对记录进行分组,然后在每个组中我只想选择1个(第一个)出现的部分,如:
Title | Part
--------------
one A
one A
one B
one B
one C
one C
-------------
two A
two A
two B
two B
two C
two C
Wished Result:
Title | Part
--------------
one A
one B
one C
-------------
two A
two B
two C
我对任何帮助感到高兴:)
到目前为止,我尝试了几种GROUP BY,ORDER BY,DISTINCT的组合,但没有任何结果导致结果。
答案 0 :(得分:1)
SELECT title,part FROM t GROUP BY title,part
答案 1 :(得分:1)
select distinct title, part from table
答案 2 :(得分:0)
希望我能正确理解问题。
试图创造类似的情况。请查看以下查询。
Rextester Link - http://rextester.com/TNSME52397
create table test
(
col1 varchar(100),
col2 varchar(100)
)
insert into test values('one','A');
insert into test values('one','A');
insert into test values('one','B');
insert into test values('one','B');
insert into test values('one','C');
insert into test values('one','C');
insert into test values('two','A');
insert into test values('two','A');
insert into test values('two','B');
insert into test values('two','B');
insert into test values('two','C');
insert into test values('two','C');
select col1 , col2 from
(
select col1 , col2 ,
row_number()over (partition by col1,col2 order by col1,col2) rn
from test
) a
where rn =1
;
答案 3 :(得分:0)
create table test
(
col1 varchar(100),
col2 varchar(100)
)
insert into test values('one','A');
insert into test values('one','A');
insert into test values('one','B');
insert into test values('one','B');
insert into test values('one','C');
insert into test values('one','C');
insert into test values('two','A');
insert into test values('two','A');
insert into test values('two','B');
insert into test values('two','B');
insert into test values('two','C');
insert into test values('two','C');
select unique col1 , col2,rn from
(
select col1 , col2 ,
dense_rank()over (partition by col1,col2 order by col1,col2) rn
from test
) a
where rn =1
;
答案 4 :(得分:0)
SELECT * FROM (
SELECT col1,col2,
RANK() over(partition by col1,col2
ORDER BY ROWNUM) R
FROM test)
where R=1;
SELECT * FROM test
WHERE ROWID IN (
SELECT MIN(ROWID)
FROM test
GROUP BY col1,col2)
ORDER BY col1,col2;
SELECt * FROM test A
WHERE 1 = ( SELECT COUNT(1)
FROM test B
WHERE A.col1 = B.col1
AND A.col2 = B.col2
AND A.ROWID >= B.ROWID);
SELECT col1,col2
FROM test
UNION
SELECT NULL,NULL
FROM DUAL WHERE 1=2;