0

I have a table with object show/click statistics.
Table has such columns: id, objectId, pageUrl, event('show' or 'click').
For example:

id, objectId, pageUrl,  event  
1,    1,      /cars/,  show  
2,    1,      /cars/,  show  
3,    1,      /items/, show  
4,    2,      /cars/,  show  
5,    2,      /items/, show  

Can I, in a simple way, get the count for each object having a unique pageUrl?

The result for dataset must be:

objectId, counter  
1,        2  
2,        2  

For object with id 1 there are 3 records, but pageUrl /cars/ appears two times, so counter must be only for unique urls.

4 Answers 4

5

Try this query:

SELECT objectId, COUNT(DISTINCT(pageUrl)) AS 'counter'
FROM Table
GROUP BY objectId
Sign up to request clarification or add additional context in comments.

Comments

0

You can use group by and count like this:

SELECT count(distinct pageUrl) from object group by objectId

2 Comments

You also need to SELECT the objectId per the question.
@TimBiegeleisen Yeah of course. I do assume OP knows how to select other columns. Just a little example. I see your answer has the result the OP wants. +1
0

First, I generated the inner select query to group the records with the same objectId and pageUrl. Lastly, I counted the pageURL per objectId.

SELECT 
      objectId, 
      COUNT(pageUrl) 
FROM (SELECT objectId,pageUrl FROM table GROUP BY objectId,pageUrl) z 
GROUP BY objectId

Comments

0
SELECT objectId, COUNT(*) AS count FROM ( SELECT objectId,pageUrl FROM table GROUP BY objectId,pageUrl ) z GROUP BY objectId

1 Comment

I suggest you to use aliases in order to avoid ambiguity

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.