I have a table T. We have multiple records for a particular user_id with match type = "Red Card". I just wanted that user_Id and match_id which has never received a "Red Card" in a entire match.
As per the table Image which is attached I would be getting output :
match_id : 3036 and 3090 and user_id 4 and 6 respectively
If you want to select all fields Use subquery
SELECT DISTINCT user_id, match_id from tbl where match_id NOT IN (
SELECT match_id from tbl where type = 'Red Card'
)
I'd do something like this:
SELECT * FROM T WHERE type <>'Red Card' GROUP BY match_id, user_id
This will select all records from the table where there is not "Red Card" and the group by will give you just one record from each couple match/user
SELECT * FROM `T` where not exists (SELECT * FROM `T` where `type`="Red card")
SELECT MATCH_ID AND USER_ID FROM TABLENAME WHERE TYPE NOT IN('RED CARD')
This Query may help you
Hope this will help.
SELECT DISTINCT user_id, match_id FROM T WHERE type <> 'Red Card' GROUP BY match_id
Because there are duplicate user_id and match_id-s in the table, I've used DISTINCT to select unique ids from the table.
Select match_id,user_id from T where user_id Not IN (Select user_id from T where type = 'Red Card') as k Group By user_id
The above query will remove all the users having red cards by first selecting user_id from T who have red cards in the nested query and then selecting users who are not in the nested statement using "NOT IN" and then grouping by user_id to remove duplicate records.
Related
I have a table "users" which has multiple columns in which column "status" has multiple values like 1,0,3,2,4. There is column "user_id" which doesn't contain unique values since, this is foreign key of another table called "user_master".
so here in "users" table we have multiple values of the one user.
So, Here is my actual query is that i would like to write a sql query to find users has only one entry in table "users" with particular status value.
For e.g. I would like to fetch all such users with status=2 and their entry in table is not more than 1. Like if user has multiple entries with status 2,1,4 in table which should not be return in query.
It should yield those users which has only one entry in table and which is of status = 2
That must be what you use:
Select count(u.user_id) AS cnt, u.*
from user u
where u.status = 2
group by u.user_id, u.status
having cnt = 1;
WITH tmp AS(
SELECT Stud_Id,COUNT(*) AS 'Count' FROM Student_tbl GROUP BY Stud_Id
)
SELECT * FROM tmp WHERE Count = 1 AND Status = 2
You have to add field in GROUP BY Clause whichever you want to use in SELECT clause.
I have researched it and found answer for it.
And query goes like this.
select count(id) as cnt,
user_id,status from users
group by user_id
having cnt < 2 and status=2
First it will group the things having count less than 2 and then which will check for status.
I have the following table:
userID | key | value
1 color green
1 eyes blue
1 hair brunette
2 color red
How can I select all the userIDs that don't have a key 'eyes'?
Using a single query you can just get a count for key = eyes and compare this count to be zero to have userIDs who don't have a key named as eyes
select `userID`,
sum(`key` = 'eyes') `count`
from t
group by `userID`
having `count` = 0
Demo
I prefer the LEFT JOIN ... IS NULL approach:
SELECT DISTINCT userID
FROM table_name tn
LEFT JOIN table_name tn2
ON tn2.userID = tn1.userID
AND tn2.key = eyes
WHERE tn2.userID IS NULL
This tends to outperform other approaches when tables are properly indexed.
You could just do something like this:
SELECT DISTINCT userID
FROM table
WHERE userID NOT IN (SELECT b.userID
FROM table b
WHERE b.key = 'eyes')
You'd be better off to have another Users table from which you could select the userId numbers, and if you did you could just substitute that out for the first FROM table and remove the DISTINCT requirement. In fact, you could even SELECT * FROM users if you had that, if that was what you were going for, to get all the details about any users who didn't have the key.
But this should work, in any event.
I have three tables: a_survey, b_survey and c_survey. Each of these tables have a column_name "user_id". What I want to do is count the actual posted surveys and return that value in a single row. So, if there was found one record with the matching user_id for each of these tables, the count would return 3.
Here's what I've tried:
select count(user_id) AS total_of_surveys
FROM a.survey, b.survey, c.survey
WHERE user_id = 3;
but I'm getting error:
Column 'user_id' in field list is ambiguous
Thanks in advance!
select sum(count(a.survey.user_id)+count(b.survey.user_id)+count(c.survey.user_id)) AS total_of_surveys
FROM a.survey, b.survey, c.survey
WHERE user_id = 3;
What you have tried was a cartesian plan it will multiply the result by the quantity of registries on each table.
The right way to do what you want is to join (with left join) or 'UNION' the tables like:
select count(user_id) from
(select user_id from a_survey where user_id = 3 UNION ALL
select user_id from b_survey where user_id = 3 UNION ALL
select user_id from c_survey where user_id = 3) tables
You will need to count each table seperately and sum it together. You can do this with one (ugly) query if you want. Something like this should work:
Select (select count(user_id) FROM a.survey WHERE user_id = 3) + (select count(user_id) FROM b.survey WHERE user_id = 3) + (select count(user_id) FROM c.survey WHERE user_id = 3) as total_of_surveys;
I have two tables, both having column a device_id column that I want to count. For the purposes of demonstration, the schema looks like:
Table 1: 'id', 'save_val', 'device_id_major'
Table 2: 'id', 'save_val', 'location', 'device_id_team'
Table 1 could have many of the same 'device_id_major'.
I basically want to get the unique device_id's from both tables, then from that result set, get the count of unique device_id's (the same device_id can appear in both tables).
Is this possible in one query?
select distinct aa.device_id, count(*)
from(select distinct device_id from table1
union all
select distinct device_id from table2) as aa
group by device_id
order by device_id
Or something like... As I don't have the schema to hand, I can't fully validate it.
SELECT count(DISTINCT aa.id)
FROM (SELECT DISTINCT major_id AS id FROM `major`
UNION ALL
SELECT DISTINCT team_id AS id FROM `team`)
AS aa
This seems to do the trick.
You could use a query that takes the UNION of both tables, then SELECT the unique values.
Suppose I want to alter the table so that my primary keys are as follows
user_id , round , tournament_id
Currently there are duplicates that I need to clean up. What is the query to find all duplicates?
This is for MySQL and I would like to see duplicate rows
Technically, you don't need such a query; any RDBMS worth its salt will not allow the insertion of a row which would produce a duplicate primary key in the table. Such a thing violates the very definition of a primary key.
However, if you are looking to write a query to find duplicates of these groups of columns before applying a primary key to the table that consists of these columns, then this is what you'd want:
select
t.user_id, t.round, t.tournament_id
from
table as t
group by
t.user_id, t.round, t.tournament_id
having
count(*) > 1
The above will only give you the combination of columns that have more than one row for that combination, if you want to see all of the columns in the rows, then you would do the following:
select
o.*
from
table as o
inner join (
select
t.user_id, t.round, t.tournament_id
from
table as t
group by
t.user_id, t.round, t.tournament_id
having
count(*) > 1
) as t on
t.user_id = o.user_id and
t.round = o.round and
t.tournament_id = o.tournament_id
Note that you could also create a temporary table and join on that if you need to use the results multiple times.
SELECT name, COUNT(*) AS counter
FROM customers
GROUP BY name
HAVING COUNT (*) > 1
That's what you are looking for.
In table:
ID NAME email
-- ---- -----
1 John Doe john#teratrax.com
2 Mark Smith marks#teratrax.com
3 John Doe jdoe#company.com
will return
name counter
---- -------
John Doe 2
Assuming you either have a table with those three columns, or that you can make and populate a table with those three columns, this query will show the duplicates.
select user_id, round, tournament_id
from yourtable
group by user_id, round, tournament_id
having count(*) > 1
This query selects all rows from the customers table that have a duplicate name but also shows the email of each duplicate.
SELECT c.name, c.email FROM customers c, customers d
WHERE c.name = d.name
GROUP BY c.name, c.email
HAVING COUNT(*) > 1
The downside of this is that you have to list all the columns you want to output twice, once in the SELECT and once in the GROUP BY clause. The other approach is to use a subquery or join to filter the table against the list of known duplicate keys.