I have this query that is optimized for speed (that's why it might look a bit odd - got some help a while back).
Want the exact result as this BUT I ONLY want results from within the LAST minute not older.
This query returns the last 100 no matter what and not only results from the last minute.
SessionGuid
is not unique - in fact it's sort of a key on multiple rows in this table.
Thanks for the help
SELECT TOP(@resultCount) * FROM
(
SELECT
[UserSessionSequenceID]
,[SessionGuid]
,SiteID
,IP
,UrlTitle
,Url
,Referer
,[Timestamp]
,ROW_NUMBER() over (
PARTITION BY [SessionGuid]
ORDER BY UserSessionSequenceID DESC)
AS sort
FROM [tblSequence]
where SiteID = @siteID
and [Timestamp] > DATEADD(mi, -@minutes, (LEFT(GETDATE(),12)))
) AS new
WHERE sort = 1
and not exists (
select SessionGuid
from tblSequence
where SiteID = @siteID
and SessionGuid = new.SessionGuid
and [TimeStamp] < DATEADD(mi, -@minutes, (LEFT(GETDATE(),12)))
)
ORDER BY [UserSessionSequenceID] DESC
My table looks like this
CREATE TABLE [dbo].[tblSequence](
[UserSessionSequenceID] [bigint] IDENTITY(1,1) NOT NULL,
[IP] [nvarchar](50) NULL,
[SessionGuid] [nvarchar](50) NOT NULL,
[Url] [nvarchar](1550) NULL,
[UrlTitle] [nvarchar](1550) NULL,
[Cords] [nvarchar](2550) NULL,
[SiteID] [int] NOT NULL,
[BrowserWidth] [int] NULL,
[BrowserHeight] [int] NULL,
[Browser] [nvarchar](550) NULL,
[BrowserVersion] [nvarchar](50) NULL,
[IsCrawler] [bit] NOT NULL,
[Referer] [nvarchar](1550) NULL,
[Timestamp] [datetime] NULL
) ON [PRIMARY]