views:

701

answers:

6

Hello I have some data like this but more than 1500000 records and more than 700 users:

usercolumn   ,       datecolumn\     
a1            ,      1998/2/11\
a2            ,      1998/3/11\
a1            ,      1998/2/15\
a4            ,      1998/4/14\
a3            ,      1999/1/15\
a2            ,      1998/11/12\
a2            ,      1999/2/11\
a3            ,      2000/2/9\
a1            ,      1998/6/5\
a3            ,      1998/7/7\
a1            ,      1998/3/11\
a5            ,      1998/3/18\
a2            ,      1998/2/8\
a1            ,      1998/12/11\
a4            ,      1998/12/1\
a5            ,      1998/2/11\
....

I would like to have distinct data from usercolumn and minimum value of date for each user like this:

usercolumn      ,    datecolumn \        
a1              ,    1998/2/11\
a2              ,    1998/2/8\
a3              ,    1998/7/7\
a4              ,    1998/4/14\
a5              ,    1998/2/11\
....

please help me to write an SQL command to do this for oledb adapter in c#, thanks.

+1  A: 

Something like this should do the tick

    SELECT usercolumn
         , MIN(datecolumn)
      FROM YouTable
     GROUP BY usercolumn
         , MIN(datecolumn)
Dmitri Kouminov
"SQL error: aggregate functions are not allowed in the GROUP BY clause" (at least from decent SQL engines!-). Just remove the last line, grouping by min(datecolumn) makes no sense and the answer is fine without it anyway!-)
Alex Martelli
Sorry. Just mistypes.Thanks for correction.
Dmitri Kouminov
A: 

If you have more than just those two columns, the best SQL to use depends a bit on what server you have at the other end of that OleDB adapter, but here's something that will work well with many (alas, not all!) such possible servers:

SELECT t.*
FROM thetable t
LEFT JOIN thetable taux
  ON(t.usercolumn=taux.usercolumn
     AND t.datecolumn>taux.datecolumn)
WHERE taux.usecolumn IS NULL

which you could read as "emit those rows of thetable such that there is no other row of the table with the same user and a strictly-less date". If minimum date for a given user can occur multiple times this will give as many rows for that user -- if that's a problem for you, there are solutions to it, too... but I'll wait for you to clarify your question more before I work any more on this!-)

Alex Martelli
A: 

This will work for SQLServer 2008 and DB2:

with temp as (
    select *, row_number() over (partition by usercolumn order by datecolumn) as rownum
    from table)
select * from temp 
where rownum = 1

It will give proper results even if you need to include multiple columns in the select.

Rashmi Pandit
+6  A: 

SELECT usercolumn, MIN(datecolumn)FROM tablename GROUP BY usercolumn;

Note that if you want other columns they must either appear in the GROUP BY clause or be constant across rows. Otherwise the result will be non-deterministic.

+1 The simplest and correct answer
gbn
A: 

you could try this:

SELECT DISTINCT a.username, a.date
FROM tablename AS a INNER JOIN tablename AS b
ON(a.username = b.username AND a.date < b.date)

As for C#, cant help you there

mrt181
A: 

SELECT DISTINCT USERNAME, DATE FROM TABLENAME AS A WHERE A.DATE=(SELECT MIN(DATE) FROM TABLENAME WHERE USERNAME=A.USERNAME)