January 31, 2018 at 3:14 am
I have the following table which I need to do the following to:
1. Create a single record for each user.
2. For each user calculate the time difference between the [VisitStartDateTime] and the [ActivityStartDateTime], then the [ActivityStartDateTime] and the [ActivityEndDateTime], and finally the [ActivityEndDateTime] and the [VisitEndDateTime]. Each activity creates a new record in the system.
3. Some users will have multiple activities and each will need to have the times of each activity measured.
4. Some users will have visited but not taken part in any activities in which case they will only have one record and we only need to measure the time difference between the [VisitStartDateTime] and the [VisitEndDateTime].
Thanks in advance.
BO
DROP TABLE #UserActivity
GO
January 31, 2018 at 3:24 am
What are you asking for help with here - calculating time differences, or pivoting information for each user into a single row? What have you already tried?
John
January 31, 2018 at 4:35 am
Hi john
It is actually both of those things. I need a single row for each user and columns measuring the time difference for each activity (if they have no activity then just the time difference between start of the visit and the end of the visit).
I do not want the result to be in a single column (like xml path) but a different column for each timestamp (visit start, activity start, activity end, visit end etc) and measurement of time difference for each of these 'events'.
The script you provided previously used only one date field where this query will need to look at different date fields (the activity fields can also be numerous but probably no more than 10).
BO
January 31, 2018 at 4:51 am
My previous script showed you how to pivot and how to do time differences, so what are you stuck on? It would also be helpful if you would show what results you expect from the sample data you provided.
John
January 31, 2018 at 5:15 am
ByronOne - Wednesday, January 31, 2018 4:35 AMHi johnIt is actually both of those things. I need a single row for each user and columns measuring the time difference for each activity (if they have no activity then just the time difference between start of the visit and the end of the visit).
I do not want the result to be in a single column (like xml path) but a different column for each timestamp (visit start, activity start, activity end, visit end etc) and measurement of time difference for each of these 'events'.
The script you provided previously used only one date field where this query will need to look at different date fields (the activity fields can also be numerous but probably no more than 10).
BO
Please post expected results - *exactly* what you expect from your sample data set. This will reduce the ambiguities which exist in your problem description.
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
January 31, 2018 at 6:21 am
Yes, you are of course right, tweaked and now does what I need.
Thanks guys.
BO
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply