sql server - SQL First Time In Last Time Out (Transact-SQL..preferrably) -


i have following table contains time in , time out of people:

 create table test (        timecardid int      , trandate date      , employeeid int      , trantime time      , trantype varchar(1)      , projcode varchar(3) ) 

the task earliest trantime trantype (perhaps using min) , latest trantime trantype z (using max), of in trandate (ie. trantype july 17 8:00 , trantype z july 17 7:00pm).

the problem is, output should in same format table it's coming from, meaning have leave data , filter out rest (that aren't earliest , latest in/out date, per employee)

my current solution use 2 different select commands earliest, latest. combine them both.

i wondering though, there simpler, single string solution?

thank much.

edit (i apologize, here sample. server sql server 2008):

 timecardid | trandate | employeeid | trantime | trantype | projcode        1      2013-04-01      1        8:00:00              sample1       2      2013-04-01      1        9:00:00              sample1       3      2013-04-01      2        7:00:00              sample1       4      2013-04-01      2        6:59:59              sample1       5      2013-04-01      1       17:00:00        z       sample1       6      2013-04-01      1       17:19:00        z       sample1       7      2013-04-01      2        17:00:00       z       sample1        8      2013-04-02      1        8:00:00              sample1       9      2013-04-02      1        9:00:00              sample1      10      2013-04-02      2        7:00:58              sample1      11      2013-04-02      2       18:00:00        z       sample1      12      2013-04-02      2       18:00:01        z       sample1      13      2013-04-02      1       20:00:00        z       sample1 

expected results (the earliest in , latest out per day, per employee, in select command):

  timecardid | trandate | employeeid | trantime | trantype | projcode        1      2013-04-01      1        8:00:00              sample1        4      2013-04-01      2        6:59:59              sample1          6      2013-04-01      1       17:19:00        z       sample1       7      2013-04-01      2        17:00:00       z       sample1       8      2013-04-02      1        8:00:00              sample1      10      2013-04-02      2        7:00:58              sample1      12      2013-04-02      2       18:00:01        z       sample1      13      2013-04-02      1       20:00:00        z       sample1 

thank much

perhaps you're looking for:

select      t.*       test t      trantime in (         (select min(trantime) test t1 t1.trandate = t.trandate , trantype = 'a'),         (select max(trantime) test t2 t2.trandate = t.trandate , trantype = 'z')     ) 

changing answer account "per employee" requirement:

;with earliestin (     select trandate, employeeid, min(trantime) earliesttimein     test      trantype = 'a'     group trandate, employeeid  ),  latestout (     select trandate, employeeid, max(trantime) latesttimeout     test      trantype = 'z'     group trandate, employeeid  ) select *  test t      exists (select * earliestin t.trandate = earliestin.trandate , t.employeeid = earliestin.employeeid , t.trantime = earliestin.earliesttimein)      or exists (select * latestout t.trandate = latestout.trandate , t.employeeid = latestout.employeeid , t.trantime = latestout.latesttimeout) 

Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -