SQL Show value that appears more times and how many times - sql

I'm trying to show the value that appears more times from a movies table.
For example:
movie_id, tag_id, score
1 1 4
1 3 5
2 1 3
3 2 4
3 3 5
Result:
tag_id, times
1 2
3 2
2 1
That table has the following columns: {movie-id, tag-id, score}.
How I can retrieve the tag-id that appears more times and how many times?
I've tried the following but it shows the same number for each tag-id:
SELECT tagId, COUNT(tagId) AS ocurrence FROM scores GROUP BY tagId ORDER BY ocurrence DESC

I think you're looking for:
SELECT TAGID, COUNT(TAGID)
FROM TABLENAME
GROUP BY TAGID
ORDER BY COUNT(TAGID)
--or you could do a having clause where COUNT(TAG-ID) > 1

SELECT tagId, COUNT(tagId) FROM scores GROUP BY (tagID) HAVING COUNT(tagId) >= ALL
(SELECT COUNT(tagId) FROM scores GROUP BY (tagId))
This will also work.

Related

Postgresql query to filter latest data based on 2 columns

Table Structure First
users table
id
1
2
3
sites table
id
1
2
site_memberships table
site_id
user_id
created_on
1
1
1
1
1
2
1
1
3
2
1
1
2
1
2
1
2
2
1
2
3
Assuming higher the created_on number, latest the record
Expected Output
site_id
user_id
created_on
1
1
3
2
1
2
1
2
3
Expected output: I need latest record for each user for each site membership.
Tried the following query, but this does not seem to work.
select * from users inner join
(
SELECT ROW_NUMBER () OVER (
PARTITION BY sm.user_id,
sm.created_on
), sm.*
from site_memberships sm
inner join sites s on sm.site_id=s.id
) site_memberships
ON site_memberships.user_id = users.user_id where row_number=1```
I think you have overcomplicated the problem you want to solve.
You seem to want aggregation:
select site_id, user_id, max(created_on)
from site_memberships sm
group by site_id, user_id;
If you had additional columns that you wanted, you could use distinct on instead:
select distinct on (site_id, user_id) sm.*
from site_memberships sm
order by site_id, user_id, created_on desc;

How to find the most frequently repeated column?

ID UserID LevelID
1 1 1
2 1 2
3 1 2
4 1 2
5 2 1
6 2 3
7 3 2
8 4 1
9 4 1
The query should return: LevelID: 1 (3 times) - the LevelID column that is most frequently repeated by different Users (UserID).
I have the following query:
SELECT LevelID, COUNT(LevelID) AS 'Occurrence'
FROM
(
SELECT DISTINCT * FROM
(
SELECT UserID, LevelID
FROM SampleTable
) cv
) levels
GROUP BY LevelID
ORDER BY 'Occurrence' DESC
Which returns:
LevelID Occurence
1 3
2 2
3 1
But it doesn't let me to add LIMIT 1; at the bottom to retrieve the first top row of the selection. What's wrong with the query?
There is no need for these several levels of nesting. Consider using aggregation, count(distinct ...), ordering the results and using a row-limiting clause to keep the top record only:
select top(1) levelID, count(distinct userID) cnt
from mytable
group by levelID
order by cnt desc
If you want to allow possible top ties, then use top (1) with ties instead of just top (1).

SQL - Order by amount of occurrences

It's my first question here so I hope I can explain it well enough,
I want to order my data by amount of occurrences in the table.
My table is like this:
id Daynr
1 2
1 4
2 4
2 5
2 6
3 1
4 2
4 5
And I want it to sort it like this:
id Daynr
3 1
1 2
1 4
4 2
4 5
2 4
2 5
2 6
Player #3 has one day in the table, and Player #1 has 2.
My table is named "dayid"
Both id and Daynr are foreign keys, together making it a primary key
I hope this explains my problem enough, Please ask for more information it's my first time here.
Thanks in advance
You can do this by counting the number of times that things occur for each id. Most databases support window functions, so you can do this as:
select id, daynr
from (select t.*, count(*) over (partition by id) as cnt
from table t
) t
order by cnt, id;
You can also express this as a join:
select t.id, t.daynr
from table as t inner join
(select id, count(*) as cnt
from table
group by id
) as tg
on t.id = tg.id
order by tg.cnt, id;
Note that both of these include the id in the order by. That way, if two ids have the same count, all rows for the id will appear together.

Order by a column not included in a group by clause

Here is my data set:
myID MemberID SourceID Acuity
1 5 3 2
2 5 3 1
3 5 3 2
4 5 3 1
I need to return a data set for each distinct MemberID, SourceID and Acuity combination. But I need to order by myID. I am inserting this into a different table and need the rows to get inserted in the proper order. The expected result I want is this:
MemberID SourceID Acuity
5 3 2
5 3 1
The problem is that I can't order by myID unless it is included in the group by clause so what happens is the result is opposite because it orders by each column by default. How can I get the intended result and order by myID?
You problem is ambiguous, because there is more than one row with different ids. You can order by the minimum id, however:
select MemberID, SourceID, Acuity
from your_table
group by MemberID, SourceID, Acuity
order by min(myID)

Active Record select 15 records order by date with different field value using

Here I have some articles:
id text group_id source_id
1 t1 1 1
2 t2 1 1
3 t3 2 2
4 t4 3 4
So I want to have records in result ordered by created_at column (it exists, but I didn't show it in table) and having distinct group id, such as that:
id text group_id source_id
1 t1 1 1
3 t3 2 2
4 t4 3 4
Also, I should be able to filter result with source_id.
I'm stuck with this question for two days and don't even know how to start solve problem.
Assuming you want the minimum values of the non-duplicated columns, try:
select min(id) as id,
min(text) as text,
group_id,
source_id,
min(created_at) as created_at
from articles
where source_id = #your_parameter_value
group by group_id,
source_id
order by 5
Select * from
(Select * from articles
Order by group_id, id) x
Group by group_id