|
I have a table "freq" with the following fields: ActionDateTime, Action, ClientID. Each of our client's action is recorded in this table. The first row for a client will be: "2009-12-16 08:55:45.554", "Arrive", "abc". here the datetime indicates the date and time the action(arrive) was performed. (So client "abc" arrived in the store on Dec 16 2009 @ 8:55:45). The last row for the same client will be as follows: "2009-12-16 14:25:33.519","Leave","abc". I need to create a query which displays all clients and the duration of their stay in the store. ((ActionDateTime when Action="Leave") - (ActionDateTime when Action = "Arrive")) I was able to figure this part out. The problem is when the same client visits the store more than once on the same day.In other words the same ClientID has more than one "Arrive" and "Leave" in the "Action" field. When I have duplicate ClientIDs I need to query only their first visit, and ignore all other subsequent visits. I've tried using min and subqueries, but i am not getting the correct results. Could you please help me find a solution for this problem? I appreciate any input.
(comments are locked)
|
|
here's one (quickly thrown together and not very elegant) way: Thank you Mr. Riley. This works perfect. I appreciate your time and help. Many Many Thanks
Dec 16 '09 at 03:02 PM
Shiyam
Nice work Kev. I thought I had a better answer and then, by the time I'd fixed my mistakes I realised I got back to where you were in the first place!
Dec 17 '09 at 06:26 AM
David Wimbush
Thank you. I could have made it slicker, but I like to show people how to do it almost step by step, so 'get the arrive times', 'get the leave times', join them, and hey presto! It's how I learned to do SQL - break it down into chunks then once you've got an answer, tune it! I'm sure there's a one-line CTE cross apply fancy-schmancy solution, but I feel that the OP could learn more from this.
Dec 17 '09 at 06:57 AM
Kev Riley ♦♦
(comments are locked)
|

