So I have a table with 5 columns: id, user_id, news_id, comment and date. Every time a user posts something on the news's comment area the data is inserted into this table.
Now I am trying to make a top poster function that will display the people with the mosts comments posted.
I would like to know how to order the result from this table based on how many times that user_id is present in the table.
For example:
1 - 134(user_id) -> 20 posts(that's how many times this user_id was found in the table.)
2 - 123 -> 19
3 - 168 -> 15
and so on.
Thanks in advance.
SELECT user_id, COUNT (*) as comments
FROM table
GROUP BY user_id
ORDER BY comments DESC
TSQL
select userID, count(*)
from userIDtable
group by userID
order by count(*) desc, userID asc
I think you are confusing rows with columns, but never mind. This is easy to do:
SELECT
user_id,
COUNT(*) posts
FROM
comments
GROUP BY
user_id
ORDER BY
posts DESC
SELECT count(user_id) C, user_id FROM RECORDS ORDER BY C LIMIT 5
"So I have a table with 5 rows: id, user_id, news_id, comment and date. Every time a user posts something on the news's comment area the data is inserted into this table. "
In this answer I'll assume you have meant 5 columns above.
select user_id, count(*) num from tableName group by user_id order by num desc;
Related
I need to perform TREAMMEAN in Access, which does not have this function.
In a table I have many Employees, each has many records.
I need to TRIMMEAN Values for each Employee separately.
Following queries perform TOP 10 percent for all records:
qry_data_TOP10_ASC
qry_data_TOP10_DESC
unionqry_TOP10_ASCandDESC
qry_data_ALL_minus_union_qry
After that, I can use Avg (Average).
But I don't know how to do it for each employee.
Visualization:
Note:
This question is edited to simplify problem.
You don't really give information in your pseudo code about your data fields but using your example that DOES have basic field information I can suggest the following should work as you described
It assumes field1 is your unique record ID - but you make no mention of which fields are keys
SELECT AVG(qry_data.field2) FROM qry_data WHERE qry_data.field1 NOT IN
(SELECT * FROM
(SELECT TOP 10 PERCENT qry_data.field1, qry_data.field2
FROM qry_data
ORDER BY qry_data.field2 ASC)
UNION
(SELECT TOP 10 PERCENT qry_data.field1, qry_data.field2
FROM qry_data
ORDER BY qry_data.field2 DESC)
)
This should give you what you want, the two sub-queries should correlate the TOP 10s (ascending and descending) for every employee. The two NOT INs should then remove those from the Table1 records and then you group the Employees and Average the Scores.
SELECT Table1.Employee, AVG(Table1.Score) AS AvgScore
FROM Table1
WHERE ID NOT IN
(
SELECT TOP 10 ID
FROM Table1 a
WHERE a.Employee = Table1.Employee
ORDER BY Score ASC, Employee, ID
)
AND ID NOT IN
(
SELECT TOP 10 ID
FROM Table1 b
WHERE b.Employee = Table1.Employee
ORDER BY Score DESC, Employee, ID
)
GROUP BY Table1.Employee;
I use a sql request to get the number message by user by forum.
SELECT count(idpost), iduser, idforum
FROM post
group by iduser, idforum
And I get this result:
But I want to get the better poster in one forum. How can I get it?
I want to get the user who has the most number of post in ONE forum like this :
According to the edited question:
Please try the query below. What we need is a subquery finding max(idpost) based on idforum. Think the query below:
select max(idpost) as IDPOST,idforum
from post
group by idforum
What this query is supposed to do is to find the number of posts by a user on a forum. So it should present you an output like:
idpost idforum
3 1
2 2
3 4
Then we need to find the related iduser for this rows as:
select p2.IDPOST, p1.iduser, p2.idforum
from post p1 inner join
( --the query above comes here as subquery.
select max(idpost) as IDPOST,idforum
from post
group by idforum
) p2 on p1.idforum = p2.idforum and p1.idpost = p2.IDPOST
What it is doing is to match the data from your main table with the temprorary data coming from your subquery based on idforum and idpost values and adding iduser value from your original table.
idpost iduser idforum
3 2 1
2 6 2
3 2 4
Well assuming you supply the idForum of the forum in question.........just get the top row of your query ordered by the count desc
SELECT TOP 1 * FROM
(
SELECT count(idpost),iduser,idforum FROM post
GROUP BY iduser,idforum
) PostsCount
WHERE PostsCount.idForum = #theForumIdIamLookingFor
ORDER BY count DESC
SELECT MAX(CN),idforum,Max(iduser) iduser
FROM (
SELECT count(idpost) CN,iduser,idforum
FROM post
Group By iduser,idforum
) A Group By idforum
If your DBMS supports RANK/ROW_NUMBER it simple:
select cnt, iduser, idforum
from
(
SELECT count(idpost) as cnt, iduser, idforum,
RANK() OVER (PARTITION BY idforum ORDER BY count(idpost) DESC) as rnk
FROM post
group by iduser, idforum
) dt
where rnk = 1
This might return more than one row per forum if multiple users share the same count. Switch to ROW_NUMBER if you want to return only one (but random) row.
SELECT count(idpost) MAX,iduser,idforum FROM post
ORDER BY count(idpost) DESC group by iduser,idforum
Or, you can do a nested query. But, I prefer this.
You can easily pick the first result as MAX
I have a table which records questions, their answers, and their authors. The column names are as follows:
id, question, answers, author
I would like to get a list of top 10 authors who have written the most questions. So it would need to first count the number of questions each author has written then sort them by the count then return the top 10.
This is in SQLite and I'm not exactly sure how to get the list of counts. The second part should be fairly simple as it's just an ORDER BY and a LIMIT 10. How can I get the counts into a list which I can select from?
SELECT BY COUNT(author)
,author
FROM table_name
GROUP BY author
ORDER BY COUNT(author) DESC LIMIT 10;
You can apply an order by clause to an aggregate query:
SELECT author, COUNT(*)
FROM mytable
GROUP BY author
ORDER BY 2 DESC
LIMIT 10
You could wrap your query as a subquery and then use LIMIT like this:
SELECT *
FROM (
SELECT author
,COUNT(*) AS cnt
FROM mytable
GROUP BY author
) t
ORDER BY t.cnt DESC
LIMIT 10;
Here is my problem: I have a table that stores the results of surveys taken by users. A single user can take multiple surveys. The key columns are: user_id, the user identifier and survey_id, the unique identifier of the survey. survey_id is incremented each time a survey is taken so if I query a specific user_id, order by survey_id descending and limit to top 4 rows I can get the last 4 surveys for a given user. My problem is I would like to query the last 4 surveys for all users in the table. I'm stumped on how to do this but this is what I have so far:
SELECT *
FROM
(
SELECT user_id
FROM
(
SELECT
user_id, count(all_survey_res_id) as numsurveys
FROM
all_survey_res
GROUP BY user_id
ORDER BY count(all_survey_res_id) DESC
) AS T1
WHERE numsurveys >= 4
)
ORDER BY user_id, all_survey_res_id
This gives me all of the records for each user that has more than 4 surveys but I then want to limit the result to just those top 4 surveys. I could solve this with code back in the application but I would rather see if I can just get the query to do this.
I think you can do this with window functions:
select
*
from (
select
user_id,
survey_id,
row_number() over (partition by user_id order by survey_id desc) rn
from
all_survey_res
) x
where
x.rn <= 4
I have a table that looks like this: comment_id, user_id, comment, last_updated.
Comment_id is a key here. Each user may have multiple comments.
How do I get 5 latest comments (SQL query for SQL Server ) for each user?
Output should be similar to the original table, just limit user's comments to 5 most recent for every user.
Assuming at least SQL Server 2005 so you can use the window function (row_number) and the CTE:
;with cteRowNumber as (
select comment_id, user_id, comment, last_updated, ROW_NUMBER() over (partition by user_id order by last_updated desc) as RowNum
from comments
)
select comment_id, user_id, comment, last_updated
from cteRowNumber
where RowNum <= 5
order by user_id, last_updated desc
Joe's answer is the best way to do this in SQL Server (at least, I assume it is, I'm not familiar with CTEs). But here's a solution (not very fast!) using standard SQL:
SELECT * FROM comments c1
WHERE (SELECT COUNT(*) FROM comments c2
WHERE c2.user_id = c1.user_id AND c2.last_updated >= c1.updated) <= 5
In SqlServer 2005, LIMIT is not valid.
Instead, do something like:
SELECT TOP(5) * FROM Comment WHERE user_id = x ORDER BY comment_id ASC
Note that this assumes that comment_id is monotonically increasing, which may not always be a valid assumption for identity fields (if they need to be renumbered for example). You may want to consider an alternate field, but the basic structure would be the same.
Note that if you were ordering by a date field, you would want to sort in descending order rather than ascending order, e.g.
SELECT TOP(5) * FROM Comment WHERE user_id = x ORDER BY last_updated DESC
SELECT TOP 5 * FROM table WHERE user_id = x ORDER BY comment_id ASC
I think that should do it.