tags:

views:

88

answers:

4

I have 1 table called urltracker that holds information about the number of times a specific url has been clicked. The table is structured into 4 columns id, urlclicked, referrer and timestamp. The urltracker creates another entry in the table every time a url has been clicked.

What i would like to do is display a specific url and the number of times it has been clicked, the user would then be able to drill down into the data by clicking to reveal all the specific times this url has been clicked.

I'm no sql expert but this sounds like i need to initially display all the distinct URL's followed by a sub query which displays all the entries for this specfic url.

Am i right in thinking this or is there an easier way?

+2  A: 

One thing to consider is using two tables - one table that holds the unique urls and another that stores the click information (and either using a unique sequence number to join them, or the url (since it is unique))

But to answer your query question - yes I would have one query to display URLs and a second query to get the data for a specific URL (only if/when the user requests that info).

hamishmcn
+2  A: 

Something like that should do the trick: First:

SELECT urlclicked, 
       COUNT(*)
  FROM urltracker
GROUP BY urlclicked

And then when the user clicks on some url:

SELECT id, 
       urlclicked, 
       referrer,
       timestamp
FROM urltracker
WHERE urlclicked = ?clickedUrl

to display details.

Mr. Brownstone
I've done that before - it works well. But it doesn't scale well. The first query with the group by can take an age once there's a lot of data in there. There's not an easy way around it though ... but it may be worth caching the totals periodically if its to be viewed often.
benlumley
I agree that there could be scaling problems if there's really a lot of data. But the question doesnt't say anything about it so that's not really the subject IMHO.
Mr. Brownstone
A: 

You should be able to do something like this to get the totals for each URL:

SELECT urlclicked, COUNT(urlclicked) AS total
FROM urltracker
GROUP BY urlclicked

Is this what you were after?

Ian Oxley
A: 

Yes, in a way u r right :

SELECT id, 
       tb1.urlclicked, 
       referrer,
       timestamp, tb2.totaltimesclicked
FROM urltracker as tb1
inner join 
(SELECT urlclicked, 
       COUNT(urlclicked) as totaltimesclicked
  FROM urltracker
GROUP BY urlclicked) as tb2
on tb1.urlclicked = tb2.urlclicked

This will display all the information along with howmany times each url is clicked.

Samiksha