Group by with MAX() still selects other row - mysql

I'm writing a query where I need to select student name by who has a MAX gradelevel_id. How ever it still selects the other row with the same id of the student where I already define what gradelevel_id should I select.
schoolyear_id | student_id | gradelevel_id
407 18 307
409 18 309`
Query:
SELECT
student_mt.student_id,
registration_mt.firstname, registration_mt.middlename, registration_mt.lastname,
MAX(grade.gwa)
FROM schoolyear_student_lt
INNER JOIN gradelevel_mt ON gradelevel_mt.gradelevel_id = schoolyear_student_lt.gradelevel_id
INNER JOIN student_mt ON student_mt.student_id = schoolyear_student_lt.student_id
INNER JOIN registration_mt ON registration_mt.registration_id = student_mt.registration_id
INNER JOIN student_grade ON student_grade.student_id = schoolyear_student_lt.student_id
INNER JOIN grade ON grade.grade_id = student_grade.grade_id
WHERE gradelevel_mt.gradelevel_id = 309
GROUP BY student_mt.student_id;
If I define 307 in my WHERE CLAUSE still selects the student name which I should not already see in my row.
Output:
student_id | firstname | middlename | lastname | MAX(grade.gwa)
18 Billie Joe Armstrong 88

(This is more of a comment) I guess you accidentally stumbled on the quirky MySQL behavior of GROUP BY.
When using GROUP BY, in the SELECT clause we can only put the GROUP BY predicate (student_mt.student_id) and aggregate functions (MAX(grade.gwa)). Even though MySQL allows this, the DBEngine assumes you know what you are doing but might result in anomalies.
Why not get take the approach of getting the student_id(pk) for the MAX(grade.gwa) as a inner sub query and then do the INNER JOIN's with other tables to select what you wanted in the outer subquery.

Related

Show all of sum and count without using group by

I want to Retrieve customer names, total orders (how many time they order the products) and the total amount they're spent in the lifetime. Run a single query WITHOUT Join, group by, having operators. Show only customers who have at least one order.
Here is my database
Customer- CustomerID| CustomerName SalesOrder- SalesOrderID | CustomerID | SaleTotal
100000 | John 1001 | 100000 | 2000
200000 | Jane 1002 | 100000 | 3000
300000 | Sean 1003 | 200000 | 5000
When I query
SELECT CustomerName,count(*) AS Total_Orders,sum(SaleTotal) AS SaleTotal
FROM Customer C,SalesOrderHeader SH WHERE C.CustomerID=SH.CustomerID;
It show only one row.
The answer that I want is
CustomerName | Total_Orders | SaleTotal
John 2 5000
Jane 1 5000
I just new on mysql.
So does anyone here know how to do this?
If you are to do this without joins and group by, then the simplest approach is to use correlated subqueries:
select *
from (
select
c.customerName,
(
select count(*)
from salesOrder so
where so.customerID = c.customerID
) totalOrders,
(
select sum(salesTotal)
from salesOrder so
where so.customerID = c.customerID
) saleTotal
from customer c
) t
where totalOrders > 0
Note that this query is clearly suboptimal - because it scans the salesOrder table twice, while a single scan would suffice. A better way to write this would be:
select c.customerName, count(*) totalOrders, sum(salesTotal) saleTotal
from customer c
inner join saleOrder so on so.customerID = c.customerID
group by c.customerID, c.customerName
There is no need for a having clause here - the inner join filters out customers that have no order already.
Use aggregation . . . and proper join syntax:
SELECT CustomerName, COUNT(*) AS Total_Orders, SUM(SaleTotal) AS SaleTotal
FROM Customer C JOIN
SalesOrderHeader SH
ON C.CustomerID = SH.CustomerID
GROUP BY CustomerName;
Your query would fail in almost any database -- including newer versions of MySQL. You have mixed aggregated columns and unaggregated columns in the SELECT. The unaggregated ones should be in a GROUP BY.
Never use commas in the FROM clause. Always use proper, explicit, standard, readable JOIN syntax.
You have to use below query. You cannot achieve it without join and group by
SELECT CustomerName,count(*) AS Total_Orders,sum(SaleTotal) AS SaleTotal
FROM Customer C,SalesOrderHeader SH WHERE C.CustomerID=SH.CustomerID
group by;

SQL Query, Getting multiple dates for one entity

I am working on writing a SQL query to produce a table that will look something like this:
Name |Dates Absent|Total Absences
student |10/28/2018 | 2
|10/29/2018 |
I currently have a data base which has 2 tables that are part of a larger system which contain the needed data (absences, students).
I have tried the following query
SELECT s.student_id,s.last_name,s.first_name, COUNT(s.student_id) AS 'Total Absences'
FROM `students` s, `absences` a INNER JOIN students ON students.student_id=a.student_id
Which yielded the following results:
student_id | last_name | first_name | Total Absences
1 | student | name | 12464
I want this to only use each ID once and count the times it appears. Is the problem from a relationship in the database that has many dates the one person can be absent? The ID was left in the select for now for debugging purposes, it will be removed later.
EDIT
I now have the query
SELECT s.last_name, s.first_name,a.date_absence, COUNT(s.student_id) AS 'Total Absences'
FROM `students` s, `absences` a
INNER JOIN students ON students.student_id=a.student_id
GROUP BY s.student_ID
This only displays one of the dates, how I can add all of the dates without redisplaying the students information?
You can do this with group_concat. It's not quite what you descibe, but it's close.
SELECT s.student_id,s.last_name,s.first_name, group_concat(a.date_absent) AS 'Dates Absent', COUNT(a.id) AS 'Total Absences'
FROM `students` s JOIN `absences` a ON s.student_id = a.student_id
GROUP BY s.student_id
which should yield
student_id | last_name | first_name | Dates Absent | Total Absences
1 | student | name | 10/28/2018,10/29/2018 | 2
It looks like you are almost there with the counting, but missing your GROUP BY statement
If you include aggregate functions, such as COUNT(), but leave off the GROUP BY, the whole intermediate result is taken as one group
You also seem to have a strange CROSS JOIN going on with your duplicate mention of the students table
If you want the absence dates in each row you'll have to use another aggregate function, GROUP_CONCAT()
Something along the lines of
SELECT s.student_id, /** Include as names could feasibly be duplicated */
CONCAT(s.first_name, ' ', s.last_name) name,
GROUP_CONCAT([DISTINCT] a.date) dates_absent, /** May want distinct here if more than one absence is possible per diem */
COUNT(*) total_absences
FROM students s
JOIN absences a
ON a.student_id = s.student_id
GROUP BY s.student_id[, name] /** name required for SQL standard */
[ORDER BY name [ASC]] /** You'll probably want some kind of ordering */
[] indicate optional inclusions

Reuse body of a mysql query for both count and rows

Because I'm working with a framework (Magento) I don't have direct control of the SQL that is actually executed. I can build various parts of the query, but in different contexts its modified in different ways before it goes to the database.
Here is a simplified example of what I'm working with.
students enrolments
-------- ------------------
id| name student_id| class
--+----- ----------+-------
1| paul 1|biology
2|james 1|english
3| jo 2| maths
2|english
2| french
3|physics
3| maths
A query to show all students who are studying English together with all the courses those students are enrolled on, would be:
SELECT name, GROUP_CONCAT(enrolments.class) AS classes
FROM students LEFT JOIN enrolments ON students.id=enrolments.student_id
WHERE students.id IN ( SELECT e.student_id
FROM enrolments AS e
WHERE e.class LIKE "english" )
GROUP BY students.id
This will give the expected results
name| classes
----+----------------------
paul|biology, english
james|maths, english, french
Counting the number of students who study english would be trivial, if it weren't for the fact that Magento automatically uses portions of my first query. For the count, it modifies my original query as follows:
Removes the columns being selected. This would be the name and classes columns.
Adds a count(*) column to the select
Removes any group by clause
After this butchery, my query above becomes
SELECT COUNT(*)
FROM students LEFT JOIN enrolments ON students.id=enrolments.student_id
WHERE students.id IN ( SELECT e.student_id
FROM enrolments AS e
WHERE e.class LIKE "english" )
Which will not give me the number of students enrolled on the English course as I require. Instead it will give me the combined number of enrolments of all students who are enrolled on the English course.
I'm trying to come up with a query which can be used in both contexts, counting and getting the rows. I get to keep any join clauses and and where clauses and that's about it.
The problem with your original query is the GROUP BY clause. Selecting COUNT(*) by keeping the GROUP BY clause would result in two rows with a number of classes for each user:
| COUNT(*) |
|----------|
| 2 |
| 3 |
Removing the GROUP BY clause will just retun the number of all rows from the LEFT JOIN:
| COUNT(*) |
|----------|
| 5 |
The only way I see, magento could solve that problem, is to put the original query into a subquery (derived table) and count the rows of the result. But that might end up in terrible performance. I would also be fine with an exception, complaining that a query with a GROUP BY clause can not be used for pagination (or something like that). Just return an anexpected result is probably the worst what a library can do.
Well, it just so happens I have a solution. :-)
Use a corelated subquery for GROUP_CONCAT in the SELECT clause. This way you will not need a GROUP BY clause.
SELECT name, (SELECT GROUP_CONCAT(enrolments.class)
FROM enrolments
WHERE enrolments.student_id = students.id
) AS classes
FROM students
WHERE students.id IN ( SELECT e.student_id
FROM enrolments AS e
WHERE e.class LIKE "english" )
However, I would rewrite the query to use an INNER JOIN instead of an IN condition:
SELECT s.name, (
SELECT GROUP_CONCAT(e2.class)
FROM enrolments e2
WHERE e2.student_id = s.id
) AS classes
FROM students s
JOIN enrolments e1
ON e1.student_id = s.id
WHERE e1.class = "english";
Both queries will return the same result as your original one.
| name | classes |
|-------|----------------------|
| paul | biology,english |
| james | maths,english,french |
But also return the correct count when modified my magento.
| COUNT(*) |
|----------|
| 2 |
Demo: http://rextester.com/OJRU38109
Additionally - chances are good that it will even perform better, due to MySQLs optimizer, which often creates bad execution plans for queries with JOINs and GROUP BY.

MySQL - Selecting data from three different tables

I'm trying to select data from three different tables. How would I go about joining these tables to make the proper query?
****customers****
--------------------------
id full_name
54 Matt Damon
53 Jimmy Kimmel
****samples****
--------------------------
id rma_id
57 USARP011315-25
56 USARP011315-24
****early_ships****
--------------------------
customer_id sample_id shipping_carrier
54 57 UPS
53 56 FedEx
This is the query I've been running but I've yielded 0 results. This is the tutorial I've been following: http://javarevisited.blogspot.com/2012/11/how-to-join-three-tables-in-sql-query-mysql-sqlserver.html
SELECT samples.rma_id, customers.full_name, early_ships.shipping_carrier,
FROM customers c JOIN early_ships e ON c.id = e.customer_id
JOIN samples s ON e.sample_id = s.id
You have to use the table aliases in the SELECT clause. Please check this fiddle I created for you. It's working perfectly. http://sqlfiddle.com/#!2/49462/8
Two issues:
There's an extra comma at the end of the SELECT clause.
In the FROM..JOIN clause, you've given nicknames, but then you're trying to use the original names in the SELECT clause.
Try this:
SELECT s.rma_id, c.full_name, e.shipping_carrier
FROM customers c
JOIN early_ships e
ON c.id = e.customer_id
JOIN samples s
ON e.sample_id = s.id
(whitespace added for readability)

mysql - selecting groups and users all in same query

I have following two tables 'USERS' and 'GROUPS':
USERS
-id
-name
-groupid
GROUP
-id
-name
I'd like to return all users along with their group's name and group id. It should be an outer join on group id field correct?
A simple INNER JOIN should be enough:
SELECT `USERS`.*, `GROUP`.name AS group_name
FROM `USERS`, `GROUP`
WHERE `USERS`.groupid = `GROUP`.id
You're going to want to look at the JOIN statement
Doing this from my phone, so pardon any moderately incorrect syntax, but something a long the lines of
Edit: other guy's syntax is better. It's too early here
You can use a LEFT JOIN between users and groups so that users who are not in a group still show up in the result set, but with group name and id NULL:
SELECT
a.*,
b.name AS group_name
FROM
users a
LEFT JOIN
`group` b ON a.group_id = b.id
Side note: Ensure that you're encasing the table name group in backticks because it is a reserved keyword.
The result-set should look something like:
id | name | group_id | group_name
-----------------------------------------------------------------------------
1 | John | 5 | ThisIsGroup5
3 | Tim | 3 | ThisIsGroup3
6 | NotInGroup | NULL | NULL
Changing LEFT to INNER in the above query would INNER JOIN the two tables and exclude the user "NotInGroup" from the result-set.