Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

minimum Time-In and Time-Out Date based on user and Date. Expand / Collapse
Author
Message
Posted Thursday, December 19, 2013 1:43 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Sunday, March 2, 2014 3:01 AM
Points: 85, Visits: 42
Hi

here is my data structure:

Check-In-Type:
I = Check-in
O = Check-out

Userid | Date | Time | Check-In-Type

001|1-Jan-2013|12:30|I
001|1-Jan-2013|12:31|I
001|1-Jan-2013|12:35|I
002|1-Jan-2013|12:30|I
002|1-Jan-2013|12:31|I
002|1-Jan-2013|12:35|I
002|1-Jan-2013|12:31|O
002|1-Jan-2013|12:35|O

As you can see I have multiple users doing multiple check-In in the attendance device. I need to know best time-in and best-time-out based on user and date. I am unable to get the best-time-in and out.

select userid, date, time,
row_number() over (partition by userid order by date desc) as bubu
into #temp2
from #temp

Post #1524482
Posted Thursday, December 19, 2013 2:08 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 7:42 AM
Points: 7,127, Visits: 13,500
Best for the business or best for the user?

“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Post #1524485
Posted Thursday, December 19, 2013 2:16 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Sunday, March 2, 2014 3:01 AM
Points: 85, Visits: 42
The Attendance System devices are a separate software. The data in there table is just like this. Some people do time-in and no time outs. I have to manage it in my reporting system and the report template is just like this.

Any help is much appreciated.

Thanks

Post #1524488
Posted Thursday, December 19, 2013 6:39 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 7:42 AM
Points: 7,127, Visits: 13,500
uzwa ul haque (12/19/2013)
The Attendance System devices are a separate software. The data in there table is just like this. Some people do time-in and no time outs. I have to manage it in my reporting system and the report template is just like this.

Any help is much appreciated.

Thanks



There's not enough information to provide sensible help yet.
What do you want to do if there's a time in with no time out? A time out with no time in?
Writing the code won't be difficult if you can tell us the business rules.


“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Post #1524548
Posted Monday, December 23, 2013 5:58 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 4:04 AM
Points: 3,614, Visits: 5,228
They say at Christmas it is better to give than to receive.

So I will give you back your sample data in a manner that is consumable and thereby the helpers on this forum can give you better help.

WITH SampleData (Userid, [DateTime], [Check-In-Type]) AS
(
SELECT '001',CAST('2013-01-01 12:30' AS DATETIME),'I'
UNION ALL SELECT '001','2013-01-01 12:31','I'
UNION ALL SELECT '001','2013-01-01 12:35','I'
UNION ALL SELECT '002','2013-01-01 12:30','I'
UNION ALL SELECT '002','2013-01-01 12:31','I'
UNION ALL SELECT '002','2013-01-01 12:35','I'
UNION ALL SELECT '002','2013-01-01 12:31','O'
UNION ALL SELECT '002','2013-01-01 12:35','O'
)
SELECT Userid
,DateOfCheckInOut=DATEADD(day, DATEDIFF(day, 0, [DateTime]), 0)
,MinCheckIn=MIN(CASE [Check-In-Type] WHEN 'I' THEN [DateTime] END)
,MaxCheckOut=MAX(CASE [Check-In-Type] WHEN 'O' THEN [DateTime] END)
FROM SampleData
GROUP BY DATEADD(day, DATEDIFF(day, 0, [DateTime]), 0), Userid;


I've also proposed a solution that may be close to what you're looking for. As Chris said though, you need to be more clear on your business rules before we can be sure of what you're trying to do.

In particular, I was quite unsure of the data types of your columns, so I converted the Date/Time to a single column of DATETIME data type. If yours are truly separate, you can combine them and use what I gave you. Better if your table already has them combined. Keeping them separate will just cause you no end to headaches later on.

Merry Christmas!



My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1525662
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse