mezamorphic -5 years ago 95
SQL Question

# Selecting first row per group

I have a table of 10 rows (apologies but i cannot seem to format examples of tables on SO) and there are three columns-

`A,B,C`
.

The values are:

``````{1,-10,5},
{1,0,5},
{1,10,5},
{2,10,12},
{2,0,12},
{3,-10,14},
{4,0,8},
{4,10,8},
{5,0,6},
{5,1,6},
{5,-5,6}
``````

If I first order the data by Col 1, then ColB, then ColC (although i did make all Col C values the same per Col A value) how would I select the "first row" per Col A?

So this should result in:

``````{1,-10,5},
{2,0,12},
{3-10,14},
{4,0,8},
{5,-5,6}
``````

``````SELECT  a, b, c
FROM    (
SELECT  *, ROW_NUMBER() OVER (PARTITION BY a ORDER BY b, c) rn
FROM    mytable
) q
WHERE   rn = 1
ORDER BY
a
``````

or

``````SELECT  mi.*
FROM    (
SELECT  DISTINCT  a
FROM    mytable
) md
CROSS APPLY
(
SELECT  TOP 1 *
FROM    mytable mi
WHERE   mi.a = md.a
ORDER BY
b, c
) mi
ORDER BY
a
``````

Create a composite index on `(a, b, c)` for the queries to work faster.

Which one is more efficient depends on your data distribution.

If you have few distinct values of `a` but lots of records within each `a`, the second query would be better.

You could improve it even more by creating an indexed view:

``````CREATE VIEW v_mytable_da
WITH   SCHEMABINDING
AS
SELECT  a, COUNT_BIG(*) cnt
FROM    dbo.mytable
GROUP BY
a

GO

CREATE UNIQUE CLUSTERED INDEX
pk_vmytableda_a
ON     v_mytable_da (a)

GO

SELECT  mi.*
FROM    v_mytable_da md
CROSS APPLY
(
SELECT  TOP 1 *
FROM    mytable mi
WHERE   mi.a = md.a
ORDER BY
b, c
) mi
ORDER BY
a
``````
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download