So currently I'm fiddling with something that looks a little like below.
It's design is to display things from table attendance, if the student(student) has taken 3 or more instances of the same class (subject).
Sometimes teachers(teacher) have substitutes, so we need to know who was teaching when the student acquired the three classes. That is why we need it to display the teacher, as well as the subject & class of 3 or more instances of the same student taking the same class.
SELECT i.teacher, i.subject, i.student
FROM attendance i
INNER JOIN (
SELECT subject, student, COUNT(*)
FROM attendance
GROUP BY subject, student
HAVING COUNT(*) > 2) temp
ON temp.subject = i.subject
AND temp.student = i.student
ORDER BY subject, student
My dilemma is that I already got it to display 3 or more instances, and to display those instances... But I still need to compress it down so multiple instances of the same teacher, student, and subject aren't listed more than once and to be honest I've gotten as far as I can.
And finally a SQL Fiddle of what I'm left with, and trying to remove the duplicates in.
Help greatly appreciated!
Looks like you're close, you can either:
use SELECT DISTINCT .... or
add SELECT ..... FROM ... JOIN .... GROUP BY i.teacher, i.subject, i.student GROUP BY ...
Related
I want to view all students that take more than two classes by using the student.id, (student.id is unique but can have many class.id associated with it).
If student.id has more than two classes associated with it, I need to display this data, using MySQL.
Without any data provided, and limited to just student.id and class.id columns, you can try this query:
SELECT student.id,COUNT(class.id) as "Total Class" FROM your_table GROUP BY student.id;
I am studying for SQL exam, and I came across this fact, regarding subqueries:
2. Main query and subquery can get data from different tables
When is a case when this feature would be useful? I find it difficult to imagine such a case.
Millions of situations call for finding information in different tables, it's the basis of relational data. Here's an example:
Find the emergency contact information for all students who are in a chemistry class:
SELECT Emergency_Name, Emergency_Phone
FROM tbl_StudentInfo
WHERE StudentID IN (SELECT b.StudentID
FROM tbl_ClassEnroll b
WHERE Subject = 'Chemistry')
SELECT * FROM tableA
WHERE id IN (SELECT id FROM tableB)
There is plenty of reasons why you have to get data from different tables, such as select sth from main query, which is based on subquery/subqueries from another tables. The usage is really huge.
choose customers from main query which is based on regions and their values
SELECT * FROM customers
WHERE country IN(SELECT name FROM country WHERE name LIKE '%land%')
choose products from main query which is greater or lower than average incoming salary of customers and so on...
You could do something like,
SELECT SUM(trans) as 'Transactions', branch.city as 'city'
FROM account
INNER JOIN branch
ON branch.bID = account.bID
GROUP BY branch.city
HAVING SUM(account.trans) < 0;
This would for a company to identify which branch makes the most profit and which branch is making a loss, it would help identify if the company had to make changes to their marketing approach in certain regions, in theory allowing for the company to become more dynamic and reactive to changes in the economy at any give time.
I am fairly new to Databases and I am just beginning to understand the DML/queries, I have two tables, one named customer this contain customer data and one named requested_games, this contains games requested by the customers, I would like to write a query that will return the customers that have requested more than two games, so far when I run the query, I don't get the desired result, not sure if I'm doing it right.
Can anyone assist with this thanks,
Below is a snippet of the query
select customers.customer_name, wants_list.requested_game, wants_list.wantslists_id,count(wants_list.customers_ID)
from customers, wants_list
where customers.customers_ID = wants_list.customers_id
and wants_list.wantslists_id = wants_list.wantslists_id
and wants_list.requested_game > '2';
just include a HAVING clause
GROUP BY customers_ID
HAVING COUNT(*) > 2
depending on how you have your data setup you may need to do
HAVING COUNT(wants_list.requested_game) > 2
This is how I like to describe how a query works maybe itll help you visualize how the query executes :)
SELECT is making an order at a restaurant....
FROM is the menu you want to order from....
JOIN is what sections of the menu you want to include
WHERE is any customization you want to make to your order (aka no mushrooms)....
GROUP BY (and anything after) is after the order has been completed and is at your table...
GROUP BY tells your server to bring your types of food together in groups
ORDER BY is saying what dishes you want first (aka i want my entree then dessert then appetizer ).
HAVING can be used to pick out any mushrooms that were accidentally left on the plate....
etc..
I would like to write a query that will return the customers that
have requested more than two games
For this to happen you need to do the following
First you need to use GROUP BY to group the games based on customers (customers_id)
Then you need to use HAVING clause to get customers who requested more than two games
Then make this a SUBQUERY if you need more information on the customer like name
Finally you use a JOIN between customers and the sub query (temp) to display more information on the customer
Like the following query
SELECT customers.customer_id, customers.customer_name, game_count
FROM (SELECT customer_id, count(wantslists_id) AS game_count
FROM wants_list
GROUP BY customer_id
HAVING count(requested_game) > '2') temp
JOIN customers ON customers.customer_id = temp.customer_id
Hey guys sorry to ask but i need help with this query please I've been messing around with different solutions but so far have not been able to solve it myself.
I have 4 tables called customer, figures, notes and lender. They all have a field called reference, which is what I'm using to link them together. Customer is the primary table and there is only one record in the figures table for each customer so i can do:
select * From customer, figures
where customer.reference = figures.reference
However, there may be multiple notes and lender records for each customer. How can I link them to show only one record?
Ideally, there would be a way to display it as:
reference, name, figures, lender 1, lender 2, note 1, note 2, note 3
You can use group_concat():
SELECT customer.reference, customer.name, figures.name,
GROUP_CONCAT(DISTINCT lender.name),
GROUP_CONCAT(DISTINCT notes.name)
FROM customer
JOIN figures ON figures.reference = customer.reference
LEFT JOIN lender ON lender.reference = customer.reference
LEFT JOIN notes ON notes.reference = customer.reference
GROUP BY customer.reference;
Assuming that each of the tables has a field name, you should change it to whatever your columns are.
I have to create a table where there are lots of columns pulling date from one table and two views. To display the columns is no problem. The problem comes into play when I have to alias two columns to show a patient who received services from the first two distinct doctors (doctor_1 and doctor_2). Each doctor has their own ID. I’m not sure if I should use the “distinct top (2)”, “rank”, etc… Here is what it sort of looks like.
Date, LastName, FirstName, Address, City, State, Zip, Hostital, Room, Doctor_1,Doctor_2
Any help would be greatly appreciated.
Let's assume your table looks something as follows:
doctor_patients:
doctor_id int,
patient_id int,
visit_date datetime
In that case you can write a query similar to the following:
select a.patient_id, b.doctor_id as doctor_1, b.visit_time_max as doctor_1_visit_time, a.doctor_id as doctor_2, max(a.visit_time) as doctor_2_visit_time
from doctor_patients a
inner join
(select patient_id, doctor_id, max(visit_time) as visit_time_max
from doctor_patients
where visit_time < a.visit_time
and patient_id = a.patient_id
and doctor_id = a.doctor_id) b
You can always put a where clause at the tail end and apply filters to the "a" table. I am not sure if this query will run as-is because I did not try it out on SQL. But I hope this gives you a general idea about what you need to do. There might be better ways to accomplish this using latest SQL features but this should get you rolling. I hope this is the answer you were looking for!