I need help on how to set up an QUERY that will result in different outputs based on the results that it achieves on the way and I'm completely stuck!
I'll give you some more details, first of all, here's my current database setup:
#USERS
id username etc..
1 alex123
2 bonnie9
3 clyde_x
#COURSES
id course_name visibility etc..
1 Name 1 1
2 Name 2 0
3 Name 3 1
#COURSE_ENROLMENT
id user_id course_id
1 1 1
2 1 2
3 3 1
The scenario is as following..
I need to list the courses to the users that are enrolled to it, which is quite easily done by something like:
SELECT
*
FROM COURSES C
JOIN COURSE_ENROLMENT E ON C.ID = E.COURSE_ID
However. If the course visibility (Database: Course, Column: visibility) is set to be visible for everyone = 1, then it will override or just ignore the enrolment and show the course to all users anyway.
How can I achieve something like this? I've tried to research CASE but can't really figure out how to proceed. Greatest thanks for any help!
To answer this you're going to need to LEFT JOIN your COURSE and COURSE_ENROLLMENT tables using an OR, so either the person is enrolled in the subject OR the visibility is set to 0.
If you change your JOIN to a LEFT JOIN, that will give you all courses, regardless of whether someone has enrolled in them or not. You can then filter out the courses which have not had anyone enrolled and are not visible by checking for visibility = 1:
SELECT *
FROM Courses C
LEFT JOIN Course_Enrolment E ON C.id = E.course_id
WHERE C.visibility = 1 OR E.id IS NOT NULL
Output:
id course_name visibility id user_id course_id
1 Name 1 1 1 1 1
2 Name 2 0 2 1 2
1 Name 1 1 3 3 1
3 Name 3 1 null null null
Demo on dbfiddle
If visibility=1 means that the course will be returned for all users, then you can do it with a cross join for that case and UNION ALL:
select c.course_name, u.username
from users u
inner join course_enrolment e on e.user_id = u.id
inner join (
select * from courses where visibility = 0
) c on c.id = e.course_id
union all
select c.course_name, u.username
from users u
cross join (
select * from courses where visibility = 1
) c
order by course_name, username
Related
I have two tables with one-to-one relations like: people has one to one relation with status table
**people table**
ID Name Status
1 Mick 1
2 Rohit null
3 Virat 1
4 Viru null
5 Gilly 2
6 Shann null
7 Mitch 3
**status table**
ID Status
1 started
2 not-started
3 pending
4 waiting
I need to get the people with status "null" and "started"(from ex: Mick, Rohit, Virat, Viru, Shann).
I tried with SQL query
select p.id, p.name
from people p
inner join status s on s.id = p.status
where (s.name IS NULL OR s.name = 'started')
this is giving only names which have a relation I mean "Mick, Virat" (skips nulls).
I don't know what I am missing here. thanks in advance
In order to keep people records where no status matches you need an OUTER join. In this case, a LEFT OUTER JOIN, which is often shortened to just LEFT JOIN:
select p.id, p.name
from people p
left join status s on s.id = p.status
where coalesce(s.name, 'started') = 'started'
I have the following tables:
Users
user_id course_id completion_rate
1 2 0.4
1 23 0.6
1 49 0.5
... ... ...
Courses
course_id title
1 Intro to Python
2 Intro to R
... ...
70 Intro to Flask
Each entry in the user table represents a course that the user took. However, it is rare that users have taken every course.
What I need is a result set with user_id, course_id, completion_rate. In the case that the user has taken the course, the existing completion_rate should be used, but if not then the completion_rate should be set to 0. That is, there would be 70 rows for each user_id, one for each course.
I don't have a lot of experience with SQL, and I'm not sure where to start. Would it be easier to do this in something like R?
Thank you.
You should first cross join the courses with distinct users. Then left join on this to get the desired result. If the user hasn't taken a course the completion_rate would be null and we use coalesce to default a 0.
select c.course_id,cu.user_id,coalesce(u.completion_rate,0) as completion_rate
from courses c
cross join (select distinct user_id from users) cu
left join users u on u.course_id=c.course_id and cu.user_id=u.user_id
Step1: Take the distinct client_id from client_data (abc) and do 1 on 1 merge with the course data (abc1) . 1 on 1 merge helps up write all the courses against each client_id
Step2: Merge the above dataset with the client info on client_id as well as course
create table ans as
select p.*,case when q.completion_rate is not null then q.completion_rate else 0
end as completion_rate
from
(
select a.client_id,b.course from
(select distinct client_id from abc) a
left join
abc1 b
on 1=1
) p
left join
abc q
on p.client_id = q.client_id and p.course = q.course
order by client_id,course;
Let me know in case of any queries.
MEMBERS_TABLE
member_id
---------------------------------------------
1
ACCOUNTS_TABLE
account_id member_id
---------------------------------------------
1 1
INVESTMENTS_TABLE
investment_id account_id
---------------------------------------------
1 1
2 1
FUNDS_TABLE
fund_id investment_id
---------------------------------------------
1 1
2 2
This is my current query:
SELECT
m.member_id,
a.account_id,
i.investment_id,
f.fund_id,
COUNT(a.account_id) AS member_accounts_total,
COUNT(i.investment_id) AS member_investments_total,
COUNT(f.fund_id) AS member_funds_total
FROM members AS m
LEFT JOIN accounts AS a ON m.member_id = a.member_id
LEFT JOIN investments AS i ON a.account_id = i.account_id
LEFT JOIN funds AS f ON f.fund_id = i.fund_id
I would like to see the following results:
member_accounts_total: 1
member_investments_total: 2
member_funds_total: 2
Instead, I am getting these results:
member_accounts_total: 2
member_investments_total: 2
member_funds_total: 2
I really don't want to write multiple queries for this.
Just need to change
COUNT(a.account_id) AS member_accounts_total,
to
COUNT( distinct a.account_id) AS member_accounts_total,
The reason you're getting 2 is because the left join on accounts to investments results in 2 records. To get a distinct count of members you need to add well... distinct.
Note you may have problems with the other totals as well (Distinct may be needed there as well in the long run...) say if a member had multiple accounts. you may get odd counts as well (if each account had the same investment... would you want to see the count only once or twice?
Not sure if I have phrased the title properly, but here it goes. I have these two tables:
table:staff
id Name groupId Status
1 John Smith 1 1
2 John Doe 1 1
3 Jane Smith 2 1
4 Jerry Smith 1 1
table:jobqueue
id job_id staff_id jobStatus
1 1 1 1
2 2 1 1
3 5 2 1
4 7 3 0
Now, what I need to do is to find the staff with the least amount of job assigned to him which I am able to do by querying the jobqueue table.
SELECT min(cstaff),tmp.staff_id FROM (SELECT t.staff_id, count(staff_id) cstaff from jobqueue t join staff s on t.staff_id=s.id join group g on s.groupId=g.id where g.id=26 GROUP BY t.id ) tmp
This works fine, but the problem is if a staff is not assigned to any job at all, this query wont get them, because it only queries the jobqueue table, where that particular staff won't have any entry. I need to modify the query to include the staff table and if a staff is not assigned any job in the jobqueue then I need to get the staff details from the staff table. Basically, I need to find staff for a group who are not assigned any job and if all staffs are assigned job then find staff with the least amount of jobs assigned. Could use some help with this. Also, tagging as Yii as I would like to know if this is doable with Yii active-records. But I am okay with a plain sql query that will work with Yii sql commands.
not sure that it is optimal query, but it works:
select d.groupId, d.name, (select count(*) from jobqueue as e where e.staff_id=d.id) as jobassigned
from staff as d
where d.id in (
select
(
select a.id
from staff as a
left outer join
jobqueue as b
on (a.id = b.staff_id)
where a.groupId = c.groupId
group by a.id
order by count(distinct job_id) asc
limit 1
) as notassigneduserid
from (
select distinct groupId from staff
) as c)
maybe need some comments:
c query is needed to get all distinct groupId - if you have separate table for this, you can replace it
notassigneduserid statement for each groupId select user with minimal job count
d query is needed to fetch actual user names, groupId for all found "unassigned users" and present it
here is the results for data from question:
Group Staff Jobs assigned
1 Jerry Smith 0
2 Jane Smith 1
with
counts as (
select s.groupId
, s.id
, (select count(*) from jobqueue where staff_id = s.id) count
from staff s
group by s.id, s.groupId),
groups as (
select groupId, min(count) mincount
from counts
group by groupId)
select c.groupId, c.id, c.count
from counts c
join groups g on c.groupId = g.groupId
where c.count = g.mincount
This SQL will give you all the staff with the minimum number of jobs in each group. It might be that more than one staff has the same minimum number of jobs. The approach is to use common table expressions to build first a list of counts, and then to retrieve the minimum count for each group. Finally I join the counts and groups tables and retrieve the staff that have the minimum count for each group.
I tested this on SQL Server, but the syntax should work for MySQL as well. To your data I added:
id Name groupId Status
5 Bubba Jones 2 1
6 Bubba Smith 1 1
and
id job_id staff_id jobStatus
5 4 5 1
Results are
group name count
1 Bubba Smith 0
1 Jerry Smith 0
2 Bubba Jones 1
2 Jane Smith 1
BTW, I would not try to do this with active record, it is far too complex.
As Ilya Bursov said this answer wasn't respond exactly what was asked. So here is a more optimized solution:
SELECT *
FROM (
SELECT s.id as id_staff, s.Name, s.groupId, count(distinct t.id) as jobsXstaff
FROM staff s
LEFT JOIN jobqueue t ON s.id=t.staff_id
GROUP BY s.id, s.groupId
ORDER BY s.groupId, jobsXstaff
) tmp
GROUP BY groupId
Old answer below.
This works but without table group which I don't create. You can simply join table groups as you did:
SELECT min(cstaff),tmp.id
FROM (
SELECT s.id, count( staff_id ) cstaff
FROM jobqueue t
RIGHT JOIN staff s ON t.staff_id = s.id
GROUP BY t.id
) tmp
As you see you need to get all values from table staff (right join) and select the id staff from it's own table (s.id instead of t.staff_id). Also you have to get tmp.id instead of staff_id now.
I have 2 tables named user and userFriend. I want all user from user table, and specific member from userFriend table. Then I want to join both of them...
user
userID userName
1 aaa
2 bbb
3 ccc
4 ddd
5 eee
userFriend
userFriendID userID friendUserID
1 1 2
2 2 3
3 1 4
4 4 2
So if my userID = 1,
then I want result like
userID userName userFriendID friendUserID
2 bbb 1 2
3 ccc NULL NULL
4 ddd 3 4
5 eee NULL NULL
so in this way I want conditional for 2nd table, I only want to join 2nd table having userID = 1 with 1st table using left join.
It's an unconventional thing to ask for... but this gives you the results you want.
SELECT u.userID, u.userName, uf.userFriendID, uf.friendUserID
FROM user u
LEFT JOIN userFriend uf ON u.userID = uf.friendUserID AND uf.userID =1
WHERE u.userID !=1
For what you want you dont need a query from both tables. Just using userFriend will give you the data. You'll need the user table for names only.
SELECT DISTINCT userID FROM userFriend
WHERE friendUserID = ?
this will give you all users that have that specific friend that you need.
Optionally you can add a INNER JOIN to see what are the names:
SELECT DISTINCT f.userID, u.name
FROM userFriend f
INNER JOIN users u ON u.userID = f.userID
WHERE friendUserID = ?
UPDATE
A comment about your structure. You don't need the userFriendID. The combination of userID and friend ID can be your key.
UPDATE of the query
SELECT * FROM users u
LEFT JOIN userFriend f on u.userID = f.userID
WHERE f.friendUserID = ?
This will return you all users that have the friend X. The left join is not important in this query. This specific condition is making it irrelevant.
To get exactly what you want you need to execute this.
SELECT * FROM users u
LEFT JOIN (
SELECT DISTINCT * FROM userFriend f
WHERE friendID = 4
) x ON u.id = x.userId
It can be done using a subquery, but I dont think is the most logical way of organizing your tables.