Thursday, March 22, 2012

Can't Figure out SQL Statement

Perhaps someone can help me on this SQL statement. Let's say my table
has only 2 fields:
RowId Name
-- --
1 A
2 A
3 A
4 A
5 B
6 B
7 B
8 B
I want to return a dataset containing the rows with the lowest RowId
for each DISTINCT Name. Result:
RowId Name
-- --
1 A
5 B
Can anybody help me?
Thanks,
JasonOne method:
SELECT
MIN(RowId) AS RowId,
Name
FROM MyTable
GROUP BY
Name
Hope this helps.
Dan Guzman
SQL Server MVP
"daokfella" <jjbutera@.hotmail.com> wrote in message
news:1126619489.331236.145060@.g49g2000cwa.googlegroups.com...
> Perhaps someone can help me on this SQL statement. Let's say my table
> has only 2 fields:
> RowId Name
> -- --
> 1 A
> 2 A
> 3 A
> 4 A
> 5 B
> 6 B
> 7 B
> 8 B
> I want to return a dataset containing the rows with the lowest RowId
> for each DISTINCT Name. Result:
> RowId Name
> -- --
> 1 A
> 5 B
> Can anybody help me?
> Thanks,
> Jason
>|||SELECT MIN(RowID), Name FROM [my table] GROUP BY Name
"daokfella" <jjbutera@.hotmail.com> wrote in message
news:1126619489.331236.145060@.g49g2000cwa.googlegroups.com...
> Perhaps someone can help me on this SQL statement. Let's say my table
> has only 2 fields:
> RowId Name
> -- --
> 1 A
> 2 A
> 3 A
> 4 A
> 5 B
> 6 B
> 7 B
> 8 B
> I want to return a dataset containing the rows with the lowest RowId
> for each DISTINCT Name. Result:
> RowId Name
> -- --
> 1 A
> 5 B
> Can anybody help me?
> Thanks,
> Jason
>

No comments:

Post a Comment