0

I have this table:

subscriberID | date | segmentID | Counter
------------------------------------------
1            | 1.1  |    2      |    3 
1            | 2.1  |    4      |    2 
1            | 3.1  |    4      |    5 
2            | 1.1  |    1      |    12 
2            | 2.1  |    1      |    1
2            | 3.1  |    2      |    10 
3            | 1.1  |    2      |    4

I have to write SQL Query that does:

Get the top 3 most common segmentID's (by counter) for a given subscriberID.

can anyone help me with that? Thanks.

1 Answer 1

1
select segmentID
from your_table
where subscriberID = 123
group by segmentID
order by sum(counter) desc

To get only 3 records you have to limit your result. Depending on your DB engine that could be top 3 or limit 3 or rownum <= 3.

Sign up to request clarification or add additional context in comments.

1 Comment

I just changed it using sum(counter). Not sure if that is what you want instead.

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.