I'm looking for a little SQL help please

Associate
Joined
17 Nov 2003
Posts
1,913
Location
Gateshead
Hi,

I'm not much of a SQLer so looking for a little help.

Say I have the following table in SQL...

Question.jpg


I'm trying to build a sql string that returns the first instance of each [Username] based on the lowest [Index].

The resulting table would be...

Answer.jpg


Many thanks :)
 
Associate
Joined
10 Nov 2013
Posts
1,808
Think this should do the trick...

SELECT Username, String, MIN(Index)
FROM Tablename
GROUP BY Username

GROUP BY will return a row for each username and when used with MIN it will return the record with the lowest index.


EDIT: beaten to it :)
 
Associate
OP
Joined
17 Nov 2003
Posts
1,913
Location
Gateshead
Afraid not...

Error...

"Column 'String' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause."
 
Associate
Joined
14 May 2006
Posts
1,287
Afraid not...

Error...

"Column 'String' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause."

You'll need to include the other column that isn't an aggregate function within your group statement (which shouldn't matter unless you have multiple entries for the same username with different string values):

Code:
SELECT Username, String, MIN(Index) 
FROM Tablename
GROUP BY Username, String


/Edit Just properly read your opening post...disregard the above! Will update shortly..
 
Last edited:
Soldato
Joined
1 May 2003
Posts
3,207
Location
Bucks
You'll need to include the other column that isn't an aggregate function within your group statement (which shouldn't matter unless you have multiple entries for the same username with different string values):

Code:
SELECT Username, String, MIN(Index) 
FROM Tablename
GROUP BY Username, String


/Edit Just properly read your opening post...disregard the above! Will update shortly..
Not sure that will work either, he will now get one row per username, per string, so the results will look the same as the starting table.

You will need to use this:

SELECT Username, MIN(String), MIN(Index)
FROM Tablename
GROUP BY Username
 
Associate
Joined
24 Jun 2008
Posts
1,168
Will that work though? Will the min(string) override the min(index)?

how about

Code:
select username, string, index
from tablename
where index in (select min(index) from tablename group by username)
 
Associate
Joined
10 Nov 2013
Posts
1,808
Code:
SELECT Tablename.username, Tablename.String, Tablename.Index 
FROM (
SELECT username, Min(Index) AS MinIndex 
FROM Tablename Group BY username) MinFiltered
INNER JOIN Entity ON Tablename.Index = MinFiltered.MinIndex;

This way you are filtering your min values and then joining on the index with the unfiltered table to get the correct string value (assuming index is unique).
 
Associate
OP
Joined
17 Nov 2003
Posts
1,913
Location
Gateshead
Can't use MIN(String) as I can't (don't want to) test against the value of a string.

Another way would be

SELECT * FROM Table A
WHERE A.index=(SELECT MIN(A1.index) FROM Table A1 WHERE A.Username=A1.Username))

This seems to do the trick :)

Cheers everyone for their help.
 
Back
Top Bottom