count of diferent values with the same id - mysql

I have ID_employe in one column (the same ID can be on more than 2 rows) and in other column I have ID_job.
I need MYSQL to find same values in the first column and then check, if there are everytime the same values in the second column.
If there is any difference, I need to give me number of ID_employe what has it different.
So example:
from this example I need SQL to give me result: 2
(because ID_employe 1 and 3 has different ID_job)
Thank you very much!

With EXISTS:
select count(distinct t.ID_employe) counter
from tablename t
where exists (select 1 from tablename where ID_employe = t.ID_employe and ID_job <> t.ID_job)

You can use a having clause and compare the minimum and maximum id_job per id_employee to exhibit those that have at least two jobs. Then you can count in another level of aggregation:
select count(*) cnt
from (
select id_employee
from mytable
group by id_employee
having min(id_job) <> max(id_job)
) t

Related

Mysql query to find all in row when value in certain column appears

In my database table, I have a value denoted userID. The value user ID may appear multiple times. I am trying to write a query that will either return all data in the row of that table if the value in userID appears 3 or more times. Alternatively, a query that return a list of all userIDs that appear 3 or more times.
Here is what I have now:
SELECT
*
FROM
myTable
WHERE
userID IN (SELECT
userID
FROM
myTable
GROUP BY userID
HAVING COUNT(*) > 2)
GROUP BY userID
This query kinda works, but some of the rows returned only have a single or double occurrence.
Any ideas on how I can modify this query so that it works?
You made things harder tnen they are, it's all simple:
SELECT
userID
FROM
myTable
GROUP BY userID
HAVING COUNT(userID) > 2)
This is if you only need ID's, else you should use this in place of SELECT statement in your WHERE part
Add the rest of your columns in the GROUP BY userID for all of the rows and youre super close with the list of userID's. This'll give you the UserID's:
SELECT userID FROM your_table
GROUP BY userID
HAVING COUNT(*) >= 3
and this will be for all:
SELECT
*
FROM
your_table
WHERE
userID IN (SELECT
userID
FROM
your_table
GROUP BY userID
HAVING COUNT(*) >= 3)
GROUP BY UserID, column_name1, column_name2

Mysql how to group by multiple columns, and select a flag value based on which group matched

Suppose I want to take all duplicate values of a table by following matched criteria's:
1- matched by (first_name,last_name,father_name).
2- matched by (nationalId).
3- matched by (phone_number).
Suppose I want to get all users whose registered them self more than one using above conditions.
Now i want to take those duplicate records, along the other columns a i need to select a a column matched_type that should has one of these 1,2,3 values based on what matched occurred.
Now i have separate query for each matching criteria, is it possible to do it with one query?
I have these queries:
SELECT u.first_name,u.last_name,u.father_name,u.national_id,u.phone,COUNT(u.id) AS total,'1' AS match_type
FROM users
GROUP BY CONCAT(u.first_name,u.last_name,u.father_name)
HAVING total > 1
and for second matching criteria:
SELECT u.first_name,u.last_name,u.father_name,u.national_id,u.phone,COUNT(u.id) AS total, '2' AS match_type
FROM users
GROUP BY u.national_id
HAVING total > 1
and for the last one:
SELECT u.first_name,u.last_name,u.father_name,u.national_id,u.phone,COUNT(u.id) AS total, '3' AS match_type
FROM users
GROUP BY u.phone
HAVING total > 1
And then i have like this:
SELECT src.* FROM (first_query UNION ALL second_query UNION ALL third_query)
Pls check is this ok?
SELECT first_name,last_name, father_name, nationalId, phone_number ,CASE
WHEN first_name=[Value] AND last_name=[Value] AND father_name = [VALUE] THEN 1
WHEN nationalId=[VALUE] THEN 2
WHEN phone_number=[VALUE] THEN 3
ELSE 0 END as matched_type FROM [TABLE_NAME] WHERE
(first_name=[Value] AND last_name=[Value] AND father_name = [VALUE])
OR nationalId=[VALUE] OR phone_number=[VALUE]

find first row count and second row count using mysql

I have a table in mySql. I need to find how much entry in table which have entered only one time and another records which are enter for second time. please see the screenshot. count is based on shg_id.
if I correctly understand, you need this:
select entered, count(*) from (
select shg_id, count(*) as entered
FROM mytable
group by shg_id
having count(*) between 1 and 2
)t
group by entered
The following should do if it has an id attribute:
SELECT * FROM Table
HAVING COUNT(shg_id) = 1 -- Record equal to 1
Or
SELECT * FROM Table
HAVING COUNT(shg_id) = 2 -- Record equal to 2
Updated - This works well on my side:
SELECT COUNT(shg_id) AS Total
FROM Table
WHERE shg_id= 4
GROUP BY shg_idHAVING COUNT(shg_id) = 1
Another one - Slightly taken from OTARIKI:
SELECT shg_id, COUNT(*) AS Total FROM Table
GROUP BY shg_id
HAVING COUNT(shg_id) BETWEEN 1 and 2

MYSQL: Count the number of times a specific integer appears in a column then making a count of how many of these are present?

I have a table with figures like this
Report used UserID
1 2
1 2
1 2
2 2
In this case I'm looking to count the 1's in the 'Report used' column, which would give me the value 3. I might find a few of these in this column for different users, so I'd want to count how many times I found 3 1's.
I've tried using SELECT COUNT to count specific numbers but I'm not sure how to count this count, if you follow me.
Try this:
SELECT userid, COUNT(reportused) onescount
FROM tablename
WHERE reportused = 1
GROUP BY userid
Also check this:
SELECT COUNT(userid)
FROM (SELECT userid, COUNT(reportused) onescount
FROM tablename
WHERE reportused = 1
GROUP BY userid) a
WHERE onescount = 3
If I've got it right:
select Report_used,RU_count,count(*)
from
(select Report_used, UserID, count(*) RU_Count
from t
group by Report_used, UserID) t1
group by Report_used,RU_count;

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