select dependent on counting two columns [closed] - mysql

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have this kind of table selected from database:
user project role
XXX p1 admin
XXX p2 developer
xxx p1 developer
yyy p3 tester
yyy p1 developer
yyy p1 admin
zzz p2 developer
and I need to add to this query filter: if user has at lease two roles in one project.
How can I do that? What MySQL functions can help me in this case?

GROUP BY project and add an HAVING clause to restrict to those with COUNT > 1
SELECT user, project, count(*) AS number_of_roles
FROM thetable
GROUP BY project
HAVING count(*) > 1
Bonus: a column with all roles aggregated
SELECT user, project, count(*) AS number_of_roles, GROUP_CONCAT(role) AS roles
FROM thetable
GROUP BY project
HAVING count(*) > 1

Related

how do I select records that are missing a particular type of row [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have a payment table. Type 1 is new/first payment. Type 11 is renew payment. So each user should have one record of type 1. But due to bad coding, right now, some records have only renew records. I want to find all the records that are missing the type 1 record. In the example below, it should return user 3.
user type amount
1 1 10
1 11 10
2 1 10
3 11 10
How should I write the query?
One way to find all users in the table that don't have a record of Type 1 is a NOT EXISTS using a subquery, as follows:
SELECT p.[user]
FROM payment p
WHERE NOT EXISTS (
SELECT 1
FROM payment
WHERE `user` = p.[user]
AND type = 1)
Alternatively, this can be done with a LEFT JOIN and only selecting the non-matching rows:
SELECT p.[user]
FROM payment p
LEFT JOIN payment p2
ON p2.[user] = p.[user]
AND p2.type = 1
WHERE p2.[user] IS NULL

Why is my average query showing incorrect values? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I asked a question a few hours ago that was marked as closed and referred me a link that did not clear up my confusion.
I am trying to query a sports database in MySQL to list the names of players who are above average age compared to their teammates. Ideally, I want to group by team, find the average of each team, and compare that to each respective player on that team.
My results from this query seem to be comparing players to the entire databases' average, rather than the average of the team. Can anyone correct my query or propose an alternate query to get the correct data? A friend of mine suggested perhaps using two copies of the tables, but that is beyond the scope of my limited MySQL skills.
My relational schema are as follows:
player(player_name, age, position)
plays_for (player_name, team_name)
SELECT player.player_name, player.age
FROM
plays_for
INNER JOIN player ON player.player_name=plays_for.player_name
WHERE (SELECT AVG(age) FROM player
GROUP BY plays_for.team_name1)< player.age
Your WHERE statement does not include the team grouping. I personally like WITH statements which seems to be the direction your friend was going.
> WITH average_ages AS ( SELECT AVG(p.age) AS average_age, pf.team_name
> FROM player p join plays_for pf on p.player_name = pf.player_name
> GROUP BY pf.team_name) aa
> SELECT player.player_name, player.age
> FROM plays_for
> INNER JOIN player ON player.player_name=plays_for.player_name
> INNER JOIN average_ages ON plays_for.team_name = average_ages.team_name
> WHERE player.age > average_ages.average_age;
The WITH statement at the top creates a temporary table of average ages and then joins it to the plays_for table.
The first few rows of the entire SELECT query before the WHERE statement would look like this
Name Age Team Average_age
Tara 51 KOs 25
Bomb 45 KOs 25
Jess 20 BES 30
Buster 40 BES 30

MYSQL how to increment column count if different combination row exist [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
MYSQL how to increment column count if different combination row exist
for example if table have
count user certNo
0 a 001
0 b 886
if same user with different certNo comes then i need to increment count by 1,
like
count user certNo
0 a 001
0 b 886
1 a 673
For legacy MySQL version you can use next query:
select
t.*,
count(t1.certNo) as count
from test t
left join test t1 on t1.user = t.user and t1.certNo < t.certNo
group by t.user, t.certNo;
Test it on SQLize.online
You can use row number():
select row_number() over (partition by user order by certNo) - 1 as count,
user, certNo
from t;

SQL to find mutual friends from the same table [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I am trying to find the mutual friends with SQL from a single table
I have a simple table with 2 fields user, friends as int. Data values as are as below
Expected Output
user friend mutual friends
1 2 2
1 3 1
This is the query i tried
select ex1.user,ex2.friend, count(distinct ex3.friend) from (select distinct user from exer1) ex1
join exer1 ex2, exer1 ex3
where ex1.user=ex2.user and
ex2.user<>ex3.user and ex2.friend<>ex3.friend
group by ex1.user,ex2.friend order by 1,2
The output i got was
enter image description here
The desired output I am looking for is
enter image description here
You can use a self-join and aggregation:
select f1.user, f2.user, count(*)
from friends f1 join
friends f2
on f1.friend = f2.friend and
f1.user < f2.user
group by f1.user, f2.user;

SQL Display student record having maximum marks from other table [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
student(sID,sNAME,sCLASS);
result(sID,subMARKS);
Actually, in MS-ACCESS, i am trying to this with equi join but getting wrong result. i'm writting my query like
SELECT stud.sID
, stud.sNAME
, stud.sCLASS
, result.sID
FROM student
, result
WHERE(SELECT MAX(subMARKS) FROM result)
It should display Ali record only because he is having maximum marks. but i am getting this kind of output as shown in picture below.
sID sNAME sCLASS
1 Ali BSC
2 Ahmad FSC
3 Asgar ICS
4 Akram BSC
SELECT T1.SID, T1.sname FROM student T1
LEFT JOIN resultT2 ON t1.sid=t2.sid
WHERE t2.submarks = (SELECT Max(submarks) FROM result);