SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Is Calculating time with a single logDateTime Column and unique ID's possible


Is Calculating time with a single logDateTime Column and unique ID's possible

Author
Message
SQLTestUser
SQLTestUser
SSCommitted
SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)

Group: General Forum Members
Points: 1924 Visits: 565
CREATE TABLE [dbo].[TransLog](
[TransactionID] [int] IDENTITY(1,1) NOT NULL,
[ID] [int] NULL CONSTRAINT [SomeContraint] DEFAULT ((0)),
[Type] [nvarchar](5) NULL,
[Name] [nvarchar](800) NULL,
[ActionType] [nvarchar](800) NULL,
[UserID] [nvarchar](15) NULL,
[LogDateTime] [datetime] NULL,
CONSTRAINT [Trans_PK] PRIMARY KEY NONCLUSTERED
(
[TransactionID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

Data on the table is in the format below.

111630 1345 SomeType Page1 Display User1 2013-01-23 13:11:15.000
111631 1345 SomeType Page2 Save User1 2013-01-23 13:12:05.000
111630 1345 SomeType Page1 Display User1 2013-01-23 16:11:15.000
111631 1345 SomeType Page2 Save User1 2013-01-23 18:12:05.000

Expected Results
User1 Spent X number of minutes on a page1 and User1 spent x number of minutes on ID 1345


Is calculating time with a single column possible
Bert-701015
Bert-701015
SSC-Addicted
SSC-Addicted (499 reputation)SSC-Addicted (499 reputation)SSC-Addicted (499 reputation)SSC-Addicted (499 reputation)SSC-Addicted (499 reputation)SSC-Addicted (499 reputation)SSC-Addicted (499 reputation)SSC-Addicted (499 reputation)

Group: General Forum Members
Points: 499 Visits: 800
What version of SQL Server are you using? SQL 2012 and above have the functions LEAD & LAG which you could use:
select
*,
DATEDIFF(MINUTE, LAG(LogDateTime, 1) OVER (PARTITION BY [ID], [TransactionID] ORDER BY [ID], [TransactionID], LogDateTime), LogDateTime) AS [MinuteCnt]
from [dbo].[TransLog]

From here you can GROUP and SUM however you like.

FYI, the data you provided violates your primary key constraint.
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