Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 query help

Author  Topic 

esthera
Master Smack Fu Yak Hacker

1410 Posts

Posted - 2010-05-12 : 07:59:19
i have a table called log

have firstname,datetime

I want to query the last record (based on datetime) for all records where there are an odd number of entries for todays date

so if i have

joe 20100512 10:59
jane 20100512 10:30
joe 20100512 10:40
jane 20100512 23:59
joe 20100512 10:59
joe 20100512 11:39
john 20100512 11:39
ken 20100512 11:39
ken 20100512 12:39
ken 20100512 15:39

only john will show up
and
ken with the date of 20100512 15:39
as the other have an even amount of entries

can someone help me?

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2010-05-12 : 08:05:51
[code]
select *
from (
select *,
row_no = row_number() over (partition by firstname order by [datetime] desc),
cnt = count(*) over (partition by firstname)
from log
) l
where cnt % 2 = 1
and row_no = 1
[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

asgast
Posting Yak Master

149 Posts

Posted - 2010-05-12 : 08:08:59
you still will need to filter for today
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2010-05-12 : 08:11:42
that should not be an uphill task for esthera right ?


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page
   

- Advertisement -