mysql query to count number of times each Id has been used - mysql

I have a database table named badges with following structure:
UserId Name
5 reviewer
3 supporter
12 copy editor
5 master
3 master
.... and so on
here name is the name of a tag achieved by the UserId.
Now I want to count for each UserId how many number of tags he has achieved and display as output in decreasing order.
Note: the UserId and Name are not distinct, means a UserId can earn multiple tags and also multiple number of each tags.
I am having writing the mysql query for this.

This is the most basic use of COUNT with GROUP BY:
SELECT UserId, COUNT(*)
FROM badges
GROUP BY UserId
ORDER BY COUNT(*) DESC

Related

MySQL 2 queries combine into 1 query - self referencing

We have build a very simple referral system that tracks userID's without cookies and referrals for social media. I am trying to create something like a 'leader board' so I can show the UserID of the top leaders in the database.
I'm trying to combine these 2 queries into 1 query.
SELECT
Count(users.AffiliateID) AS affiliate,
users.AffiliateID
FROM
users
group by affiliateID
order by affiliate desc
This generates an output where the variable 'affiliate' contains the USERID of the top referring affiliate. In this case the #1 person is affiliateID = 5297dc41331235
What I then do is look up the name of the person with this query.
Select name from users where UserIDHash = "5297dc41331235";
How can I rewrite the query above so that it looks up the value of the name field as a column that references the value of the AffiliateID i.e. where AffiliateID=UserIDHash on each record?
Your help is greatly appreciated.
Thanks!

Get DISTINCT show.name and COUNT of associated seasons

I'm trying to write a SQL statement that outputs a table with the two columns show name and number of seasons. The show column must contain no duplocates and the number of seasons column counts the number of seasons associated with the show entity.
Here are my two tables
Shows Table
id | name
Seasons Table
id | show_id | season_number
Here's what I've tried so far
SELECT DISTINCT shows.name
FROM shows
INNER JOIN seasons on show.id = seasons.show_id;
The above code works for grabbing distinct names but whenever I try adding COUNT(season.id) it breaks.
Any suggestions?
Thanks!
Use group by to aggregate multiple rows with the same name into a group. With count(distinct id) you calculate the number of distinct values of the id column in that group.
SELECT name
, COUNT(DISTINCT seasons.id)
FROM shows
JOIN seasons
ON shows.show_id = seasons.show_id
GROUP BY
name
By the way, I'd expect a season to have a one to many relation to show, not the other way around.

Self inner join to get single record

I have an SQL table data as follow
I want to display single record for product
example
90792 Amlaan-Hi-Power .............. Show only 1 record when there are 2 record
90793 Amlaan-Neutral .............. show only 1 record when there are 2 record
90794 Amlaan-Phosphate free .........show only 1 record when there are 2 record
90801 Acetone .......................show only 1 record when there are 2 record
90901 Acetanilide ...................show only 1 record when there is 1 record
Can I do this using Inner join
I know
select distinct product from product ORDER BY `product`.`product` DESC
will select distinct (unique) product code and that to only one field i.e. product but confused how to get other information using SQL statement
but results in duplicate records or same table...........................
It looks like your duplicate rows vary by the quantity of product in the package.
You can display just the product and name with
SELECT DISTINCT product, name
FROM product
If you want to deal with the quantity as well, that's a little trickier. This might work: it will put all product codes on one line.
SELECT product,
GROUP_CONCAT(product_code ORDER BY product_code) product_codes,
name
FROM product
GROUP BY product, name
Self join doesn't make a whole lot of sense for this application.
Use group by option for such purposes.
SELECT product,GROUP_CONCAT(product_code SEPERATOR '|') AS product_code,name FROM Table GROUP BY NAME
It will show only one record for duplicate names.
The multiple enteries of product code will seperated by | .

MySQL Query Count two Fields

I have a database table (notes) that has three columns: id, ticket, and user.
I need to show how many notes were entered by each user as well as how many unique tickets those notes were added to (grouped by user). For example, John Doe entered 100 notes into 50 unique tickets and Jane Doe entered 70 notes into 65 unique tickets. I'm not interested in which tickets they were added to, just how many.
Here is what I have so far.
select count(id) count
from notes
group by user
Is it possible to grab the number of unique tickets in the same query?
Use distinct inside the count:
select
user,
count(id) note_count,
count(distinct ticket) ticket_count
from notes
group by user
You are probably new to SQL. The query is something like:
select user, count(distinct TicketId) as NumTickets, count(id) as NumNotes
from notes
group by user
You should include the user in the select clause, so you know to whom the numbers apply. When naming things, it is a good idea to avoid reserved words like count, so I named on NumTickets and the other NumNotes.

Using GROUP BY and returning all criteria matching records

I have a table that stores various types of flags. Each flag type has a reasonId column. So you could flag a post as spam and use several reasons; as abusive and use several reasons, etc.
I need a query to return all spam flags (flagTypeId=1) on a single post and, in addition, an extra column to return the number of times a flag reason occurred (reasonId). I need the full record set because I need to tack the user data, thus returning a grouped result is not sufficient by itself:
Assuming I have a flags table with PK id, int flagTypeId, int postId, int reasonId, and userId, I wrote this:
SELECT id, flagTypeId, postId, userId, reasonId, COUNT(reasonId) reasonCount
FROM flags
WHERE flagTypeId = #flagTypeId AND postId = #postId
GROUP BY reasonId
ORDER BY reasonCount DESC
This query does not return the correct number of records. If I have four spam records, and two of those four share the same reasonId, only three records come back. I want all four records to come back with an extra column showing the number of times a reasonId occurred.
Any ideas how I can modify my query to achieve this?
SAMPLE INPUT/OUTPUT
Assuming three peope flagged the same post, and two of them used the same flag reason.
id flagTypeId postid reasonid userid count
1 1 1 1 1 2
2 1 1 1 2 2
3 1 1 2 3 1
Would this work:
SELECT id, flagTypeId, postId, flags.reasonId, x.reasonCount
FROM
flags
JOIN (SELECT reasonid, COUNT(*) AS reasonCount FROM flags WHERE flagTypeId = #flagTypeId AND postId = #postId GROUP BY reasonid) AS X
ON flags.reasonid = x.reasonid
WHERE
flagTypeId = #flagTypeId AND postId = #postId
I think you're going about it a little backwards. Keep in mind that, if you're already retrieving all the information in a collection of records, you already have the count of records, just by getting the size of the returned collection.
Tweak your query to remove the GROUP BY clause and COUNT column. Then, assuming it was something like PHP, and you fetched the results of the modified query into an array $flagReasons, you can just reference count($flagReasons) to get the count.