Tuesday, 15 June 2010

sql - Query returning users, first record and last record -



sql - Query returning users, first record and last record -

i attempting write query showing our unique users, date of first recorded entry in scheme , date of lastly recorded entry. these split between 2 tables: users table , log table.

users:

| userid | username | |--------|-------------| |20 | tom smith | |21 | jim jones | |22 | sandy brownish |

log:

| logid | userid | date | value | --------|---------|------------|--------------| | 1 | 21 | 01/03/2013 | login | | 2 | 22 | 01/04/2013 | login | | 3 | 21 | 01/05/2013 | edit | | 4 | 20 | 01/06/2013 | login | | 5 | 20 | 01/07/2013 | search | | 6 | 22 | 01/08/2013 | login | | 7 | 21 | 01/09/2013 | close | | 8 | 21 | 01/11/2013 | login | | 9 | 20 | 01/12/2013 | edit | | 10 | 22 | 01/13/2013 | search |

this desired result of query attempting write:

|userid | username | first log date | lastly log date | |-------|-------------|----------------|---------------| | 20 | tom smith | 01/06/2013 | 01/12/2013 | | 21 | jim jones | 01/03/2013 | 01/11/2013 | | 22 | sandy brownish | 01/04/2013 | 01/13/2013 |

so far have first 2 columns, cannot figure out first , lastly date columns, below query far:

select distinct(u.userid1) 'userid', u.username 'username' users u, log l u.userid = l.userid

i using sql server 2008. love help.

just min , max of date each record.

select u.userid [userid], u.username [username], min([date]) [first log date], max([date]) [last log date] users u inner bring together log l on u.userid = l.userid grouping u.userid, u.username

sql sql-server sql-server-2008

No comments:

Post a Comment