Consider the following table:
primaryKey id activity template creator created
1 1 3 5 x 2011-10-13
2
I think this should work -
SELECT *
FROM TABLE
WHERE
primaryKey in
(
SELECT min(primarkyKey) from TABLE
group by id, activity, template
)
Here, first distinct is obtain on required columns in the inner query by doing group by. Then the min of primary key of each distinct record is used to get all the columns from the outer query.
This is for MS SQL Server.
Updated, as i made a little mistake!
SELECT DISTINCT
ROW_NUMBER() OVER (ORDER BY
id
, activity
, template
, creator
, created ) PrimaryKey
, id
, activity
, template
, creator
, created
FROM
[TABLE_NAME]
GROUP BY
id
, activity
, template
, creator
, created
SELECT primarykey, id, activity, template, creator, created FROM (
SELECT *, row_number() OVER (partition BY id, activity, template ORDER BY created) as rn FROM table
) a
WHERE rn = 1