Viewing Most Popular Links - Revisited 

A little over a month ago I posted a SQL Query that can be used to find the most popular links on a given portal. In that example if you had links that were being tracked that were files it would only show the fileid for the file and not the file name. Now since there has been a fair amount of reader demand I have created a new version of this query to actually show the URL for URL based links and to show the file name for file based links.

To successfully parse the "fileid=xx" portion of the results when needed I use the substring function to grab just the id so I can join to the files table to grab the file name. Due to the different methods needed for files and links I have done this via two queries and then used the "UNION" statement to combine the results for display. For you to use this on your portal you will need to be sure to modify the portalid value to match that of your portal.

--Get standard links
SELECT m.ModuleTitle,
        t.url,
        t.clicks,
        t.LastClick
FROM urltracking t
    
INNER JOIN Modules m
        
ON (t.ModuleId m.ModuleId)
WHERE t.PortalId 0
    
AND t.trackclicks 1
    
AND t.urltype <> 'F'
--Combine with the others
UNION
--Get File links
SELECT m.ModuleTitle,
        f.[FileName] 
as url,
        t.clicks,
        t.lastclick
FROM urltracking t
    
INNER JOIN Modules m
        
ON (t.moduleid m.moduleid)
    
INNER JOIN Files f
        
--Grab the file id from the url field for the join
        
ON (CAST(SUBSTRING(t.url, 8LEN(t.url)) as intf.fileid)
WHERE t.urltype 'F'
ORDER BY t.clicks DESC, url

Please post any comments/questions below

Posted by IowaComputerGurus on Thursday, July 19, 2007
 

Comments

Instead of the union statement you should do a left outer join onto the Files table (with the t.UrlType = 'f' as an additional join clause, then for the Url return field you can use IsNull(f.FileName, t.Url)...

By Andrew on Friday, August 22, 2008 at 7:46 AM

Actually that isn't really valid, as the cast to int will fail in the situation with a unified query. It might be possible to cast the fileid to a string and compare that way, but I'm not really sure that it gains much.

By mitchel.sellers@gmail.com on Friday, August 22, 2008 at 8:50 AM
Click here to post a comment

Donate

Show your appreciation for the content/modules made available by MitchelSellers.com by making a donation. Donations are used to assist with dedicating time to creating free content.