Count value in SQL [duplicate] - mysql

This question already has answers here:
How to count occurrences of a column value efficiently in SQL?
(7 answers)
Closed 5 years ago.
I have got a table with ID's:
Id
===
1
2
2
3
1
2
And I want to create a table with two columns like this:
Id COUNT
=============
1 2
2 3
3 1
How can I do that?

Let's say you called your table 'user', you can try this :
SELECT user.Id as ID, count(user.Id) as COUNT_ID
FROM user
GROUP BY ID;
Hope it helps,
WaLinke

You have to group by your id.
Select id, count(1) as COUNT
from yourtable
group by ID
order by id
That way you tell your sql, that you want to count the number of rows per id.
If you need more examples feel free to google sql count.
There are many good examples out there.
Or check this stackoverflow question:
How to use count and group by at the same select statement

You can use GROUP BY and Count(columnname) functions like this
SELECT
Id,
Count(Id) AS COUNT
FROM
tablename
GROUP BY Id

Something like this should work
SELECT id, COUNT(id) AS Expr1 FROM dbo.Table1 GROUP BY id

Related

How To Get the first row of each group by [duplicate]

This question already has answers here:
Retrieving the last record in each group - MySQL
(33 answers)
Get top n records for each group of grouped results
(12 answers)
Closed 3 months ago.
I have created a table that has category id and a name and the table contains multiple matching category id so i would like to get the first data of each matching category id
based on the example table above i would like to get just the name alex and brown
Here is what i have tried
SELECT * FROM tailors
WHERE id IN(
SELECT min(id)
FROM tailors
GROUP BY cat_id,id,name,status
)
but i am getting all the record when i am just trying to get the first data of each matching category id
You just need to take out id and name from your group by clause -
SELECT * FROM tailors
WHERE id IN (SELECT min(id)
FROM tailors
GROUP BY cat_id, status
);
If the logic remains same throughout the table, and the version of the DB is 8.0+, then use such an aggregation :
SELECT name
FROM( SELECT t.*, MIN(id) OVER (PARTITION BY cat_id) AS min
FROM tailors AS t ) AS tt
WHERE id = min
assuming id is a primary key column, there will be only one minimum value per each cat_id.
GROUP BY cat_id is handled by PARTITION BY cat_id and the other columns(name and status) following it should be removed.
To return only one row use LIMIT 1:
SELECT * FROM tailors
WHERE id IN(
SELECT min(id)
FROM tailors
GROUP BY cat_id,id,name,status
) LIMIT 1

NOT able to do MAX , COUNT into a single Mysql query [duplicate]

This question already has answers here:
How to find the maximum count using mysql?
(9 answers)
Closed 1 year ago.
I've been trying to get the maximum number from a set of different values.
suppose,
i have table with values,
id val
A 1
A 2
A 3
B 4
B 5
C 6
so i want to get the maximum number of id and first i did get the count and tried to get maximum
my sql query is,
SELECT MAX (ID_COUNT) id_count
FROM (SELECT id,COUNT(id) ID_COUNT
FROM tab1
GROUP BY id)
I want an ouput 3 since A is repeated 3 times and is the maximum.
it was supposed to work. but now im getting an error saying derived table value should have an alias. but i did give an alias.
what should i do?
thanks in advance...
You need to specify an alias for the sub select
SELECT MAX (ID_COUNT) id_count
FROM (SELECT id,COUNT(id) ID_COUNT
FROM tab1
GROUP BY id) tab2
You can try the below -
SELECT id,COUNT(id) ID_COUNT
FROM tab1
GROUP BY id
order by ID_COUNT desc
limit 1

Calculate Average of Text Value Mysql [duplicate]

Using PHP and MySQL, I want to query a table of postings my users have made to find the person who has posted the most entries.
What would be the correct query for this?
Sample table structure:
[id] [UserID]
1 johnnietheblack
2 johnnietheblack
3 dannyrottenegg
4 marywhite
5 marywhite
6 johnnietheblack
I would like to see that "johnnietheblack" is the top poster, "marywhite" is second to best, and "dannyrottenegg" has the least
Something like:
SELECT COUNT(*) AS `Rows`, UserID
FROM `postings`
GROUP BY UserID
ORDER BY `Rows` DESC
LIMIT 1
This gets the number of rows posted by a particular ID, then sorts though the count to find the highest value, outputting it, and the ID of the person. You'll need to replace the 'UserID' and 'postings' with the appropriate column and field though.
I believe this should work...
SELECT user_id, COUNT(*) FROM postings ORDER BY COUNT(*) GROUP BY user_id LIMIT 1
Assuming posting is a tuple (user_id, recipient_user_id), where each row represents one posting, from user_id to recipient_user_id:
select user_id, count(*) as posts
from postings
group by user_id
having count(*) = max(count(*)) ;

How to GROUP BY 2 different columns together

I have 2 columns having users id participating in a transaction, source_id and destination_id. I'm building a function to sum all transactions grouped by any user participating on it, either as source or as destination.
The problem is, when I do:
select count (*) from transactions group by source_id, destination_id
it will first group by source, then by destination, I want to group them together. Is it possible using only SQL?
Sample Data
source_user_id destination_user_id
1 4
3 4
4 1
3 2
Desired result:
Id Count
4 - 3 (4 appears 3 times in any of the columns)
3 - 2 (3 appears 2 times in any of the columns)
1 - 2 (1 appear 2 times in any of the columns)
2 - 1 (1 appear 1 time in any of the columns)
As you can see on the example result, I want to know the number of times an id will appear in any of the 2 fields.
Use union all to get the id's into one column and get the counts.
select id,count(*)
from (select source_id as id from tbl
union all
select destination_id from tbl
) t
group by id
order by count(*) desc,id
edited to add: Thank you for clarifying your question. The following isn't what you need.
Sounds like you want to use the concatenate function.
https://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_concat
GROUP BY CONCAT(source_id,"_",destination_id)
The underscore is intended to distinguish "source_id=1, destination_id=11" from "source_id=11, destination_id=1". (We want them to be 1_11 and 11_1 respectively.) If you expect these IDs to contain underscores, you'd have to handle this differently, but I assume they're integers.
It may look like this.
Select id, count(total ) from
(select source_id as id, count (destination_user_id) as total from transactions group by source_id
union
select destination_user_id as id , count (source_id) as total from transactions group by destination_user_id ) q group by id

Condition for counting distinct rows in an SQL query

I have a table in a MySQL database with an ID column. This is not a key of the table and several rows can have the same ID.
I don't really know SQL but I already figured out how to obtain the number of distinct IDs:
SELECT COUNT(DISTINCT ID) FROM mytable;
Now I want to count only those IDs which appear more than 2 times in the table.
So if the ID column contains the values
3 4 4 5 5 5 6 7 7 7
the query should return 2.
I have no idea how to do this. I hope someone can help me!
Btw, my table contains a huge number of rows. So if there are several possibilities I would also be happy to know which solution is the most efficient.
Try this:
SELECT COUNT(ID) FROM (
SELECT ID FROM mytable
GROUP BY ID
HAVING COUNT(ID) > 2) p
select count(*) from
(select count(id) as cnt,id from mytable group by id) da
where da.cnt>2
The inner query will give you how many elements does each id have. And the outer query will filter this.
SELECT
COUNT(ids)
FROM
(SELECT
COUNT(ID)AS ids
FROM
mytable
GROUP BY
ID
HAVING
ids>2
)AS tbl1
Updated :
SELECT count(ID)
FROM (
SELECT ID FROM mytable
GROUP BY ID
HAVING count(ID) > 2
) p
should do what you need