Combine rows and put their values in seperate column in mysql - mysql

I want to combine two or more rows which have the same name and put their values in two separate column. The problem can be clearer with the following images:
My expected output is:
You can check the fiddle here: fiddle
What I have tried so far is with the MySQL code:
Select subjects, mark_score, activity
FROM(
SELECT subjects, mark_score,
(SELECT regd, subjects, mark_score
FROM exo_i WHERE entry='7' and regd='19') as activity
FROM exo_i WHERE regd='19' GROUP BY subjects)t

As discussed in the comment, the requirement is to display "marks" for the "FA1" entry and "activity" for the "SA1" activity. Assuming there can't be multiple rows with these values (i.e., the combination of regd, subjectsa and activity is unique), you could have a subquery for each of these activities, and join them:
SELECT a.regd, a.subjects, a.marks, b.activity
FROM (SELECT regd, subjects, marks
FROM mytable
WHERE entry = 'FA1') a
JOIN (SELECT regd, subjects, marks AS activity
FROM mytable
WHERE entry = 'SA1') b ON a.regd = b.regd AND a.subjects = b.subjects

Related

Join mysql table with distinct value from another table

I encountered a problem on a database I am working with. I have a table of counsels which may hold repeating values, but their is an enrolment number filed which is unique and can be used to fetch them. However, I want to join from a cases_counsel table on the "first" unique value of the counsel table that matches that column on the cases counsel table.
I want to list the cases belonging to a particular counsel using the enrolment_number as the counsel_id on the cp_cases_counsel table. That means I want to pick just a distinct value of a counsel, then use it to join the cp_cases_counsel table and also return the count for such.
However, I keep getting duplicates. This was the mysql query I tried
SELECT T.suitno, T.counsel_id, COUNT(*) as total from cp_cases_counsel T
INNER JOIN (SELECT
enrolment_number as id, MIN(counsel)
FROM
cp_counsel
GROUP BY
enrolment_number
) A
ON A.id = T.counsel_id
GROUP BY T.suitno, T.counsel_id
and
SELECT enrolment_number as id, MIN(counsel) as counsel, COUNT(*) as total FROM cp_counsel
JOIN cp_cases_counsel ON cp_cases_counsel.counsel_id = cp_counsel.enrolment_number
GROUP BY enrolment_number
For the second query, it's joining twice and I am having like double of what I am supposed to get.
The columns that you want in the results are councel (actually only one of all its values) from cp_counsel and counsel_id from cp_cases_counsel, so you must group by them and select them:
SELECT a.counsel, t.counsel_id, COUNT(*) AS total
FROM cp_cases_counsel t
INNER JOIN (
SELECT enrolment_number, MIN(counsel) AS counsel
FROM cp_counsel
GROUP BY enrolment_number
) a ON a.enrolment_number = t.counsel_id
GROUP BY a.counsel, t.counsel_id;

How can you divide the count of unique ids resulting from an inner join of two tables by the count of unique ids in just one of the tables

This question seems like it should be simple so I am sure it must have been asked before but I searched and could not find an answer.
I have two tables, one containing a master list of members with demographic data, the other containing a list of members with claims. They both have a common ID field. I need to be able to calculate the percentage of unique members in the demographics table with one or more claims against the total number of unique members in the demographics table in a single SQL statement (I am using MariaDB and SQLite).
Essentially, I need to be able to:
select count (distinct demo.ID)
from demo
inner join claims on demo.ID = claims.ID
divided by
select count (distinct ID)
from demo
but calculate it as a percentage number within a single SQL statement
How about just
select count(distinct demo.ID) / (select count (distinct ID) from demo)
from demo
join claims on demo.ID = claims.ID
Assuming that the ids in the claims table are all valid, you can use:
select (select count(distinct id) from claims) / (select count(distinct id) from demo) as ratio

SQL select users that belong to two groups

I have a list of persons in a table. I then have another table where I correlate each person to one or more groups. Some persons have only one entry in the groups table but some have multiple.
I am now trying to SELECT list of persons that are in two specific groups. Person must be in BOTH groups in order to qualify.
My table with the basic information on the persons is base and the table with the group correlation is groups_registration. In fact I also have a third table where the groups names and further information are stored but it is not required for this query.
The groups I am trying to gather in this example are 4 and 11.
What I tried initially was:
SELECT base.*, groups_registration.person_id, groups_registration.group_id
FROM base
INNER JOIN groups_registration
ON base.id = groups_registration.person_id
WHERE (groups_registration.group_id = '4' AND groups_registration.group_id = '11')
ORDER BY base.name
This did not get my any response, I assume because no single row contains both group_id = 4 and group_id 11.
I have been searching through stackoverflow with no joy. Do you guys have any ideas?
Obviously, no row has both values. Use group by:
SELECT gr.person_id, groups_registration.group_id
FROM groups_registration gr
WHERE gr.group_id IN (4, 11)
GROUP BY gr.person_id
HAVING COUNT(DISTINCT gr.group_id) = 2;
I'll let you figure out how to join in the additional information from base.
Notes:
Use table aliases to make it easier to write and read queries.
Presumably, the ids are numbers. Compare numbers to numbers. Only use single quotes for date and string constants.
IN is better than long chains of OR/=.
You can use joins as shown below:
SELECT A.*, B.person_id, B.group_id
FROM base A
INNER JOIN
(SELECT gr.person_id, groups_registration.group_id
FROM groups_registration gr
WHERE gr.group_id IN (4, 11)
GROUP BY gr.person_id
HAVING COUNT(DISTINCT gr.group_id) = 2) B
ON A.id = B.person_id;
This will give you all the desired fields.

Find the id of students who take every course, in mysql

I have 3 tables:
Student (Id, Name, Country)
Course (CrsCode, CrsName, Type, Instructor)
Results(Id, CrsCode, Grade)
I have to solve below q's by using SQL query. Id and CrsCode are key fields in every table. The Type field specifies the course type, e.g. MATH, STAT, SYSC, TTMG, ELEC, etc.
Find the Id of students who take TTMG or SYSC course.
Find the Id of students who take every course.
Find the id of students who take every TTMG course or every SYSC course.
Below are the sample data for part 3. 1st image is the Course Table and 2nd image is the Results table
I am able to solve the 1st question by using the following SQL query: SELECT R.ID FROM RESULTS R JOIN COURSE C ON C.CRSCODE = R.CRSCODE WHERE C.TYPE="TTMG" OR C.TYPE ='SYSC
For the 2nd question, I believe we have to again relate 2nd (Course) and 3rd (Results) table in order to get the result. We have to relate a specific case here. We have to consider a case that there is one specific student who is taking all courses(CrsCode) and we have to find the id of that student.
I believe the query will still be the same as in question 1 but this time there will be little bit changes:
SELECT R.ID FROM RESULTS R JOIN COURSE C
I am not including anything after COURSE C because I am not sure the answer after that. Any pointers will be highly appreciated.
Find the Id of students who take every course.
Assuming course table contains all the courses a student can take, you can group by the id column in the results table and check if the count is equal to the row count in course table.
SELECT ID
FROM RESULTS
GROUP BY ID
HAVING COUNT(DISTINCT CRSCODE) = (SELECT COUNT(*) FROM COURSE)
Edit: Based on OP's comment
Find the id of students who take every TTMG or SYSC course
SELECT r.id
FROM course c
JOIN RESULTS r on c.CRSCODE=r.CRSCODE
GROUP BY r.ID
HAVING COUNT(case when type = 'SYSC' then r.CRSCODE end) = (SELECT COUNT(*) FROM COURSE WHERE type = 'SYSC')
OR COUNT(case when type = 'TTMG' then r.CRSCODE end) = (SELECT COUNT(*) FROM COURSE WHERE type = 'TTMG')
Sample Demo

MySQL: Selecting from 3 tables

I have the following query:
SELECT
DISTINCT sites.site_id,
sites.site_name,
sites.site_url,
earnings.cust_id
FROM
sites,
earnings
WHERE sites.site_id = earnings.site_id AND sites.site_id IN('8', '1666')
That query gives me very well the information asked. It returns two rows, one for site 8 and another for site 1666, with the information on them from those tables.
Now, I want that the cust_id number be used to select from another table (let's say table customers) where they are stored by id and where other info is such as name, last name, etc.
Basically what I need is to expand that query to extract customer name and last name from the table customers, using the ids obtained.
Same way you got the info from two tables. Add a comma, add the third table name, and add the relationship to your WHERE clause like you did with the first two tables.
SELECT
DISTINCT sites.site_id,
sites.site_name,
sites.site_url,
earnings.cust_id,
customers.name,
customers.last_name
FROM
sites,
earnings,
customers
WHERE sites.site_id = earnings.site_id AND sites.site_id IN('8', '1666') AND customers.id = earnings.cust_id
I think it's clearer to write out the JOINs though:
SELECT
sites.site_id,
sites.site_name,
sites.site_url,
earnings.cust_id,
customers.name,
customers.last_name
FROM
sites
INNER JOIN
earnings
ON
earnings.site_id = sites.site_id
INNER JOIN
customers
ON
customers.id = earnings.cust_id
WHERE
sites.site_id IN (8, 1666)
GROUP BY
sites.site_id