1

I have a very simple task, but I cannot find any solution. I have two tables, 'articles' and 'categories'

My article table look like this:

id | cat_id | title | content
1      1      Blah     Content 1
2      1      Blah2    Content 2
3      2      Blah3    Content 3

My categories table look like this:

id | title
1     Category 1
2     Category 2

You see I have 2 articles that have the same cat_id. I do not want with duplicate cat_id field. I cannot use DISTINCT, because I will get all articles, because I want all fields out.

so if i use DISTINCT like this:

SELECT DISTINCT a.id, a.cat_id, a.title, a.content FROM articles AS a

I will get everything out, but I want output like this

id | cat_id | title | content
2      1      Blah2    Content 2
3      2      Blah3    Content 3

Can someone help me please !!!

Tjodalv
  • 340
  • 3
  • 15

3 Answers3

0

Not tested, but should work:

; WITH cte AS (
   SELECT
      *
      ,ROW_NUMBER() OVER (PARTITION BY cat_id ORDER BY id DESC) AS RowNum
)
SELECT
   *
FROM cte
WHERE RowNum = 1;
Florian Reischl
  • 3,788
  • 1
  • 24
  • 19
0

This query will select the first article (lowest id) from each category

SELECT a.* FROM Article a LEFT JOIN 
   Article a2 ON a.id<a2.id AND a.cat_id=a2.cat_id
WHERE a2.id IS NULL

It does an outer join with all other articles. The join clause only joins articles with the same category and with a smaller ID. When there are no matches (a2.id is NULL), then we have the article with the lowest ID for that category.

Maks3w
  • 6,014
  • 6
  • 37
  • 42
mdma
  • 56,943
  • 12
  • 94
  • 128
  • I have explained as best I can in the paragraph after the query. I can try to elaborate - which part are you having trouble with? – mdma Jul 26 '10 at 20:57
0

Try this:

select article.*
    from article
        join (select min(id) as id, cat_id from article group by cat_id) a2
            using (id);

or:

select *
    from article
    where id in (select min(id) from article group by cat_id);

Both select one article id (using min()) for each distinct cat_id and select only the records with these ids.

thieger
  • 146
  • 5