views:

64

answers:

2

I have this SQL

DECLARE @url varchar(100)
SET @url = 'http://mysite.com/%'

SELECT
[UserSessionSequenceID]
      ,[SiteID]
      ,[Referer]
      ,[Timestamp]
,ROW_NUMBER() over (PARTITION BY [Referer] ORDER BY referer DESC) AS sort 
  FROM [tblSequence]
WHERE [Referer] IS NOT NULL AND [Referer] NOT LIKE @url AND siteID = 15

Want to count unique referer - problem is that this SQL returns ALL matches and count those one by one. I only want the count for each unique referer (and still exsluding the @url with like).

How to do that?

+3  A: 
SELECT
      [Referer], Count([Referer]) as RefCount
  FROM [tblSequence]
WHERE [Referer] IS NOT NULL AND [Referer] NOT LIKE @url AND siteID = 15
GROUP BY [Referer]

Putting Order back in (should that really be required) depends on your exact requirements.

Tobiasopdenbrouw
Looks good - what if I want to add the [timestamp] in the overall where clause?AND ([Timestamp] > DATEADD(dd, -1, (LEFT(GETDATE(),12))))
seo20
This was my first thought, but then I realise OP wants unique referer count and unaggregated row data in one call. Sorry everyone for my previous typo...
gbn
Seo20 - please indicate whether you're looking for an answer like gbn has indicated, or aggregation like I'm doing. I suspect gbn's method is more what you want. But what's holding you back from just adding the WHERE statement you just gave in your comment? (In either solution)
Tobiasopdenbrouw
+3  A: 

Use DENSE_RANK not ROW_NUMBER if you require row data instead of an aggregate

gbn
Change "ROW_NUMBER()" to "DENSE_RANK()" in the query you posted
gbn