• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 180
  • Last Modified:

SQL group time and location

Hello all,

I have the following data: person, date, punchtime, location
Example set:
Person, date, punchtime, location
384, 10/1/2008, 10/1/2008 8:00, 180
384, 10/1/2008, 10/1/2008 12:30, 180
384, 10/1/2008, 10/1/2008 13:30, 220

I need to have the output show me only combinations where the location has changed.
For example:
384, 10/1/2008, 12:30, 180, 13:30, 220

The initial query I wrote is returning the following:
384, 10/1/2008, 8:00, 180, 13:30, 220
384, 10/1/2008, 12:30, 180, 13:30, 220

Here is the original query I put together:
select t1.person, t1.date, t1.punchtime, t1.location, t2.punchtime, t2.location
from punches t1
left join punches t2 on t2.person = t1.person
        and t1.date = t2.date
        and t1.punchtime < t2.punchtime
        and t1.location <> t2.location
where t1.date = '10/1/2008'


I can't figure out how to exclude the first transaction.  Since it's the same location as the second transaction, it should be skipped.  Then, the second and third transactions should be returned since they are an actual location change.
0
cciesliga
Asked:
cciesliga
  • 6
  • 3
1 Solution
 
chapmandewCommented:
here is what you can do...

1.  add a meaningless sequential number to your output:

select id = ranking = dense_rank() over(partition by person order by date asc), person, date, punchtime, location
into #temp
from yourtable

2.  use that output to compare the locations

select * from temp o
join temp i on o.ranking = i.ranking + 1
where o.location <> i.location
0
 
cciesligaAuthor Commented:
I tried the query provided and it returns: incorrect syntax near '='.

I removed id=ranking= and ran it and each of the rank entries returned as the value 1.

I'm not familiar with the dense_rank command, so I'm not sure what needs to be changed to make the first part work.
0
 
chapmandewCommented:
post your exact code that you're using.
0
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
cciesligaAuthor Commented:
select id=ranking=dense_rank() over(partition by person order by date asc), person, date, punchtime, location
into #temp
from timesheet

Error:
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near '='
0
 
cciesligaAuthor Commented:
sorry,
select id=ranking=dense_rank() over(partition by person order by date asc), person, edate, punchtime, location
into #temp
from timesheet


date is actually edate.
0
 
chapmandewCommented:
try this:

select ranking=dense_rank() over(partition by person order by date asc), person, edate, punchtime, location
into #temp
from timesheet
0
 
cciesligaAuthor Commented:
ok, that seems to have done it.  testing the second part now.
0
 
cciesligaAuthor Commented:
looks good.

Thanks chapmandew!!
0
 
cciesligaAuthor Commented:
Thanks for the prompt replies!!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now