Count the number of unique occurences in a MySQL column - mysql

I have a table with a column status:
1 row with status_1
3 rows with status_2
2 rows with status_3
7 rows with status_4
I want to write a query that gets all available statuses, groups them, and get the number of occurences then fetch the data as follows:
Status 1: 1
Status 2: 3
Status 3: 2
Status 4: 7
NB: The statuses are not already known. And we are not talking about DISTINCT or GROUP BY to count the number of unique values (it must be a separate number for each status)

SELECT status, count(*) count
FROM your_table
GROUP BY status;
See MySQL Group BY tutorial

According to data you seem want GROUP BY with COUNT() only
select status, count(*) as noofoccurences
from table t
group by status;

Related

How to fetch records from mysql database in sorted order with pagination along with group by status

I have one table e.g. Employee, which has columns (id, name, is_active).
I want to fetch the records from employee table sorted by name along with pagination.
SELECT * from employee ORDER BY name ASC LIMIT 5, 10;
In above query 5 count to skip first 5 records and take next 10 records.
I want to make a query which will return all records which has value is_active=1, followed by records which has value is_active = 0 with pagination.
e.g: I have 50 records out of them 40 records has value is_active=1, and 10 records which has value is_active=0.
So as I am using pagination and page size is suppose 10 records per page.
So query will return all active records first with order by name and at last page will return 10 inactive records order by name.
Does anyone have any suggestions? Any help would be greatly appreciated.
Thank you!
You just need descendingly sort by is_active column, and no need an OFFSET value depending on the explanation such as
SELECT *
FROM employee
ORDER BY is_active DESC, name
LIMIT 10

Count and sum up all duplicate records in MySQL

I have table with, following structure.
id name
1 john
2 ana
3 john
4 ana
5 peter
6 ana
7 Abrar
8 Raju
Duplicate entries in the table are as follows
john(2) duplicate
ana(3) duplicate
The names which are duplicates are john and ana.
My question is how would I count the records in total which are duplicate here it is '5' records
Note : I also followed the similar question in community but it explains how we can add the number of duplicates exists for that particular name in the table and adds up the third column in table representing the duplicates records with same name but in my case I wanted to know the number of all duplicates exist in the table (here the result of the query is just number "5") irrespective of the names.
Just take a count subquery on the query you already have in mind (or perhaps have already written):
SELECT SUM(cnt) AS total_duplicates
FROM
(
SELECT COUNT(*) AS cnt
FROM yourTable
GROUP BY name
HAVING COUNT(*) > 1
) t;
Demo

mysql highly selective query

I have a data set like this:
User Date Status
Eric 1/1/2015 4
Eric 2/1/2015 2
Eric 3/1/2015 4
Mike 1/1/2015 4
Mike 2/1/2015 4
Mike 3/1/2015 2
I'm trying to write a query in which I will retrieve users whose MOST RECENT transaction status is a 4. If it's not a 4 I don't want to see that user in the results. This dataset could have 2 potential results, one for Eric and one for Mike. However, Mike's most recent transaction was not a 4, therefore:
The return result would be:
User Date Status
Eric 3/1/2015 4
As this record is the only record for Eric that has a 4 as his latest transaction date.
Here's what I've tried so far:
SELECT
user, MAX(date) as dates, status
FROM
orders
GROUP BY
status,
user
This would get me to a unqiue record for every user for every status type. This would be a subquery, and the parent query would look like:
SELECT
user, dates, status
WHERE
status = 4
GROUP BY
user
However, this is clearly flawed as I don't want status = 4 records IF their most recent record is not a 4. I only want status = 4 when the latest date is a 4. Any thoughts?
SELECT user, date
, actualOrders.status
FROM (
SELECT user, MAX(date) as date
FROM orders
GROUP BY user) AS lastOrderDates
INNER JOIN orders AS actualOrders USING (user, date)
WHERE actualOrders.status = 4
;
-- Since USING is being used, there is not a need to specify source of the
-- user and date fields in the SELECT clause; however, if an ON clause was
-- used instead, either table could be used as the source of those fields.
Also, you may want to rethink the field names used if it is not too late and user and date are both found here.
SELECT user, date, status FROM
(
SELECT user, MAX(date) as date, status FROM orders GROUP BY user
)
WHERE status = 4
The easiest way is to include your order table a second time in a subquery in your from clause in order to retrieve the last date for each user. Then you can add a where clause to match the most recent date per user, and finally filter on the status.
select orders.*
from orders,
(
select ord_user, max(ord_date) ord_date
from orders
group by ord_user
) latestdate
where orders.ord_status = 4
and orders.ord_user = latestdate.ord_user
and orders.ord_date = latestdate.ord_date
Another option is to use the over partition clause:
Oracle SQL query: Retrieve latest values per group based on time
Regards,

Display value and number of duplicates in MySQL

I have a MySQL database row named ID and it goes
ID
18464762
3936573
3936573
3936573
374749502
374749502
374749502
374749502
374749502
3746325
9705732
9705732
9705732
9705732
476870382
476870382
3746574
37264
37264
And I want to make a MySQL Query that displays the information in two columns, the ID, and how many occurrences of the ID exist. That way I can sort it by number of occurrences.
The ideal output would be
id occurences
374749502 5
9705732 4
3936573 3
32764 2
476870382 2
18464762 1
3746325 1
3746574 1
This is just a small example as I have thousands of entries.
Everything I have already found from searching online tells me how to find which ids have duplicates, or the number of id's that have duplicates, but I have been unable to display the information like this.
Thank you in advance
You need to GROUP BY id, use aggregate function COUNT for counting occurences, and at the end order by second column.
SELECT id, COUNT(*) AS occurences
FROM your_tab
GROUP BY id
ORDER BY 2 DESC

How to check multiple values in a column MySQL

I have a column with values i.e.
number id
1 111
1 111
3 222
4 222
5 333
I'm checking if the I.D has more than one instance and if the number is the same as the second or more instance. So here, it would return 1 because there is 2x 1's with id 111, but 222 doesn't return because 3 and 4 are not the same.
How would I do such a query?
I was told that I could do two queries or more and on the second or more query I would ignore the first instance.
select id, number
from the_table
group by id, number
having count(*)>1
you should have index defined on both id, and number column
ps: the order can be number, id ... depends on your composite index key
you should try using group by clause and see whether the output is what you expect:
select number,id from table_name group by number;
GROUP BY DOCS
Select number,id from table_name group by number;