Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


minimum Time-In and Time-Out Date based on user and Date.


minimum Time-In and Time-Out Date based on user and Date.

Author
Message
uzwa ul haque
uzwa ul haque
SSC Journeyman
SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)

Group: General Forum Members
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
ChrisM@Work
ChrisM@Work
SSCrazy Eights
SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)

Group: General Forum Members
Points: 8957 Visits: 19014
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
uzwa ul haque
uzwa ul haque
SSC Journeyman
SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)

Group: General Forum Members
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
ChrisM@Work
ChrisM@Work
SSCrazy Eights
SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)

Group: General Forum Members
Points: 8957 Visits: 19014
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
dwain.c
dwain.c
SSCarpal Tunnel
SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)

Group: General Forum Members
Points: 4249 Visits: 6431
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!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search