I have a table with fields utm
and tracking_id
. There may be many
tracking_id
s for each
utm
.
So I need to select distinct utm and any one
tracking id for each utm (let`s say thefirst
one).
For example, we got the
table:
utm |
tracking_id
-------------------
ddff | 1
ddff |
2
llzz | 3
ddff | 4
ohoh | 5
ohoh |
6
And as an output i
want to get:
utm |
tracking_id
-------------------
ddff | 1
llzz |
3
ohoh | 5
I
use PostgreSQL 9.1.
Is there a way to do it with SQL?
Answer
if you have only one column, than simple
aggregate is what you want, go with Clodoaldo Neto's advice.
If you have more
than one columns, you can use href="http://www.postgresql.org/docs/current/static/sql-select.html"
rel="nofollow">dictinst on
syntax:
select distinct
on(utm)
utm, tracking_id, column1, column2, ...
from
t
order by utm,
tracking_id
href="http://sqlfiddle.com/#!12/1e933/2" rel="nofollow">sql fiddle
demo
No comments:
Post a Comment