Solved

SQL group time and location

Posted on 2008-10-06
9
170 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Get row count of current SQL query 8 55
Pivot Query Problem 9 42
Help with stripping out character in SQL LEFT/RIGHT/REPLACE 2 46
Help Required 2 33
by Mark Wills PIVOT is a great facility and solves many an EAV (Entity - Attribute - Value) type transformation where we need the information held as data within a column to become columns in their own right. Now, in some cases that is relatively…
by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

810 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