Solved

SQL group time and location

Posted on 2008-10-06
9
172 Views
Last Modified: 2010-04-21
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
Comment
Question by:cciesliga
  • 6
  • 3
9 Comments
 
LVL 60

Expert Comment

by:chapmandew
ID: 22652022
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
 
LVL 1

Author Comment

by:cciesliga
ID: 22652534
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
 
LVL 60

Expert Comment

by:chapmandew
ID: 22652576
post your exact code that you're using.
0
Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

 
LVL 1

Author Comment

by:cciesliga
ID: 22652624
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
 
LVL 1

Author Comment

by:cciesliga
ID: 22652632
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
 
LVL 60

Accepted Solution

by:
chapmandew earned 500 total points
ID: 22652743
try this:

select ranking=dense_rank() over(partition by person order by date asc), person, edate, punchtime, location
into #temp
from timesheet
0
 
LVL 1

Author Comment

by:cciesliga
ID: 22653125
ok, that seems to have done it.  testing the second part now.
0
 
LVL 1

Author Comment

by:cciesliga
ID: 22653498
looks good.

Thanks chapmandew!!
0
 
LVL 1

Author Closing Comment

by:cciesliga
ID: 31503499
Thanks for the prompt replies!!
0

Featured Post

Free Webinar: AWS Backup & DR

Join our upcoming webinar with experts from AWS, CloudBerry Lab, and the Town of Edgartown IT to discuss best practices for simplifying online backup management and cutting costs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will use…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question