Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
289 views
in Technique[技术] by (71.8m points)

sql - Using distinct for specific column in oracle

Followinq query is possible in oracle

select DISTINCT(COLA), COLB 
from TABLEA

In this query,

  1. Will this query returns distinct values for the combination of COLA and COLB?
  2. If this query returns distinct value for the COLA alone, then what should be the COLB value for selected COLA?
  3. Or if we have multiple records with same COLA value, will this query throws error?
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In your example the query returns distinct values for the combination of COLA and COLB.

Examine the syntax: enter image description here

Note, that DISTINCT/UNIQUE/ALL can be only placed after SELECT and before of the first expression in the select list.

The documentation says that:
https://docs.oracle.com/cd/B19306_01/server.102/b14200/statements_10002.htm

DISTINCT | UNIQUE

Specify DISTINCT or UNIQUE if you want the database to return only one copy of each set of duplicate rows selected. These two keywords are synonymous. Duplicate rows are those with matching values for each expression in the select list.

ALL

Specify ALL if you want the database to return all rows selected, including all copies of duplicates. The default is ALL.

The above means, that DISTINCT/UNIQUE is always applied to the whole select list, not to individual columns


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...