0

I have a table that looks like this:

id | group | title | description | added | modified
---------------------------------------------------
1  |   1   |  ...  |     ...     |  ...  |   ...
2  |   1   |  ...  |     ...     |  ...  |   ...
3  |   2   |  ...  |     ...     |  ...  |   ...
4  |   2   |  ...  |     ...     |  ...  |   ...
5  |   3   |  ...  |     ...     |  ...  |   ...

Now I need to select the first row of every group value. I think using the DISTINCT keyword is kind of the right direction here. But using:

SELECT DISTINCT group FROM table;

obviously would return only the group values, but not the remaining fields:

group
-----
  1
  2
  3

Does anyone know how to accomplish this?

What I'm looking for is - in regard to the above example - this kind of output:

id | group | title | description | added | modified
---------------------------------------------------
1  |   1   |  ...  |     ...     |  ...  |   ...
3  |   2   |  ...  |     ...     |  ...  |   ...
5  |   3   |  ...  |     ...     |  ...  |   ...

Thanks in advance!!

4
  • 1
    Please note that group is a reserved word. Commented Jul 17, 2013 at 17:32
  • Yeah I actually changed it to "member". My mistake! Commented Jul 17, 2013 at 17:45
  • I changed it to zgroup (to be pronounced with a German accent ;-) Commented Jul 17, 2013 at 17:46
  • Oddly enough I am german ;) Commented Jul 17, 2013 at 18:00

3 Answers 3

2

You could use something like;

SELECT "id", "group", "title", "description", "added", "modified"
FROM (
  SELECT *,ROW_NUMBER() OVER (PARTITION BY "group" ORDER BY "id") rn
  FROM Table1
) this_could_be_called_anything
WHERE rn=1;

An SQLfiddle to test with.

Sign up to request clarification or add additional context in comments.

2 Comments

If you don't mind, could you elaborate what happens here? Especially what "a" does?
@tiiv a is just an alias for the sub-expression (one is required, you could call it anything) ROW_NUMBER() assigns a row number sequence (1..n) per partition (group), I just grab all rows that have a row number of 1, ie the first row in every group.
0
SELECT mk.* FROM meuk mk
JOIN ( SELECT zgroup, MIN(id) AS id
        FROM meuk
        GROUP BY zgroup
        ) agg ON agg.id = mk.id
        ;

Or this one:

SELECT * FROM meuk mk
WHERE NOT EXISTS ( SELECT  *
        FROM meuk nx
        WHERE nx.zgroup = mk.zgroup
        AND nx.id < mk.id
        )
        ;

2 Comments

Your second solution works like a charm. I can't really say that I understand your first solution though =\
Well is is a joined (aggregating) subquery, just like the a in Joachim's solution and the tg in collapsar's answer.
0

try this One:

    select t.*
      from table t
inner join (
                select min(te.id)   id_pivot
                     , te.grp
                  from table te
              group by te.grp 
           ) tg
        on tg.id_pivot = t.id
         ;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.