PostgreSQL:
SELECT [ ALL | DISTINCT [ ON ( expression [, ...] ) ] ]
[ * | expression [ [ AS ] output_name ] [, ...] ]
, ON , , ON, . , ON, .
, [ON ( [,...])] .
:
CREATE TABLE bar
(foo varchar(3), fub varchar(1), flut timestamp)
;
INSERT INTO bar
(foo, fub, flut)
VALUES
('one', 'a', '2016-01-01 01:01:03'),
('one', 'b', '2016-01-01 01:01:02'),
('one', 'c', '2016-01-01 01:01:01'),
('two', 'd', '2016-01-01 01:01:03'),
('two', 'e', '2016-01-01 01:01:02'),
('two', 'f', '2016-01-01 01:01:01')
;
. ? .
select (foo) from bar;
| foo |
|-----|
| one |
| one |
| one |
| two |
| two |
| two |
, , , foo, , , , . . , DISTINCT?
select distinct(foo) from bar;
| foo |
|
| two |
| one |
select distinct foo from bar;
| foo |
|
| two |
| one |
, , . , . DISTINCT , DISTINCT .
, :
SQL-, , Postgres, "different". :
select distinct(pattern) as pattern, style, ... etc ...
from styleview
where ... etc ...
DISTINCT ! .
[ON ()] .
:
select distinct ON (foo) foo, fub, flut from bar order by foo
| foo | fub | flut |
|-----|-----|---------------------------|
| one | a | January, 01 2016 01:01:03 |
| two | d | January, 01 2016 01:01:03 |
b:
select distinct ON (fub) foo, fub, flut from bar order by fub
| foo | fub | flut |
|-----|-----|---------------------------|
| one | a | January, 01 2016 01:01:03 |
| one | b | January, 01 2016 01:01:02 |
| one | c | January, 01 2016 01:01:01 |
| two | d | January, 01 2016 01:01:03 |
| two | e | January, 01 2016 01:01:02 |
| two | f | January, 01 2016 01:01:01 |
c:
select distinct ON (flut) foo, fub, flut from bar order by flut
| foo | fub | flut |
|-----|-----|---------------------------|
| one | c | January, 01 2016 01:01:01 |
| one | b | January, 01 2016 01:01:02 |
| one | a | January, 01 2016 01:01:03 |
[ON ()] , "" "" " " " " , , ORDER BY, , order by ALSO , SELECT DISTINCT ON PostgreSQL, :
: SELECT DISTINCT ON ORDER BY
sqlfiddle
While I do not want to complicate my answer, it is worth mentioning the wrinkle:
select distinct (foo,fub) from bar;
Now the brackets do something , but what they do is not directly related to the individual. See " complex types "