MySQL Select Statement Create Column Then Compare - mysql

Very New to SQL. Trying to get the results to show only when TotalSales is Less Than the goal.
SELECT SUM(Table1.Column1) AS TotalSales
FROM Table 1
WHERE Table1.Goal > TotalSales
GROUP BY EmployeeID;
It is giving me error 1054: Unknown column.
Again, very basic but I'm stuck. Thanks again for the help.

You need subquery with JOIN :
SELECT t1.*, t2.TotalSales
FROM TABLE1 t1 INNER JOIN (
SELECT EmployeeID, SUM(Table1.Column1) AS TotalSales
FROM Table1
GROUP BY EmployeeID ) t2
ON t1.EmployeeID = t2.EmployeeID
WHERE t1.Goal > t2.TotalSales;

Related

Merge rows in mysql based on condition

I am trying to merge the rows based on condition in mysql.
I have table as shown below :
Looking merge the row 1 into row 2 (where the attendance count is larger)
and need to shown the result as :
I was trying to divide the dataset into 2 parts using the below query
select
a.student_id,a.school_id,a.name,a.grant,a.classification,a.original_classification,,a.consent_type
from (
select * from school_temp where original_classification='all' and availability='implicit')a
join(select * from school_temp where original_classification!='all' and availability!='implicit')b
on a.student_id = b.student_id and a.school_id=b.school_id and a.name=b.name
But unable to merge the rows and get total attendance count .
Please help me ,i am badly stuck in this
Split this into two queries that you combine with UNION.
The first joins the implicit row with the row with the highest attendance among the explicit rows for each student. See Retrieving the last record in each group - MySQL for how that works. Use SUM(attendance_count) to combine the attendances.
The second query in the UNION gets all the rows that don't have the highest attendance.
WITH explicit as (
SELECT *
FROM school_temp
WHERE original_classification!='all' and availability!='implicit'
)
SELECT a.student_id, a.school_id, a.name, SUM(attendance_count) AS attendance_count,
b.grant, b.classification, b.original_classification, b.consent_type
FROM school_temp AS a
JOIN (
SELECT t1.*
FROM explicit AS t1
JOIN (
SELECT student_id, school_id, name, MAX(attendance_count) AS max_attendance
FROM explicit AS t2
GROUP BY student_id, school_id, name
) AS t2 ON t1.student_id = t2.student_id AND t1.school_id = t2.school_id AND t1.name = t2.name AND t1.attendance_count = t2.max_attendance
) AS b ON a.student_id = b.student_id and a.school_id=b.school_id and a.name=b.name
WHERE a.original_classication = 'all' AND a.availability = 'implicit'
UNION ALL
SELECT t1.*
FROM explicit AS t1
JOIN (
SELECT student_id, school_id, name, MAX(attendance_count) AS max_attendance
FROM explicit AS t2
GROUP BY student_id, school_id, name
) AS t2 ON t1.student_id = t2.student_id AND t1.school_id = t2.school_id AND t1.name = t2.name AND t1.attendance_count < t2.max_attendance
I've used a CTE to give a name to the subquery that gets all the explicit rows. If you're using MySQL 5.x, you'll need to replace explicit with that subquery throughout the query. Or you could define it as a view.

How to perform calculations on queries in mysql that return multiple values

First query:
SELECT COUNT(sales.ucid) AS totalOutcomes
FROM sales
group by date(sales.saleDate)
Second query:
SELECT COUNT(*) AS joinedOutcomes
FROM sales
JOIN calls
ON sales.ucid = calls.call_id
group by date(sales.saleDate)
I now want to use the output from the second query and divide that by the output from the first query.
Can someone please help with this? Thanks!
Join the two queries.
SELECT t1.date, joinedOutcomes/totalOutcomes
FROM (
SELECT date(sales.saleDate) AS date, COUNT(sales.ucid) AS totalOutcomes
FROM sales
GROUP BY date
) AS t1
JOIN (
SELECT date(sales.saleDate) AS date, COUNT(*) AS joinedOutcomes
FROM sales
JOIN calls ON sales.ucid = calls.call_id
group by date
) AS t2 ON t1.date = t2.date

MySQL subquery as alias - unknown column error

Column not found: 1054 Unknown column 'expensetot' in 'field list'
Based on the query below, what is the best way to produce grandtotal without an error?
SELECT
table1.cost,
(SELECT SUM(expense) FROM table2 WHERE table2.key=table1.id) as expensetot
(table1.cost+expensetot) as grandtotal,
table3.label
FROM
table1
LEFT JOIN table3 ON table3.key=table1.id
WHERE
table1.saledate>SUBDATE(CURDATE(), INTERVAL 1 YEAR)
ORDER BY grandtotal
You can't use alias in select clause you must repeat the code
and you missed a comma before table1.cost + ...
and in subselect the outer table are not in scope so you should use a proper join with subquery for sum
SELECT
table1.cost,
t.expensetot,
table1.cost + t.expensetot as grandtotal,
table3.label
FROM table1
INNER JOIN (
select table2.key, sum(table2.expense) expensetot
from table2
group by table2.key
) t on t..key=table1.id
LEFT JOIN table3 ON table3.key=table1.id
WHERE
table1.saledate>SUBDATE(CURDATE(), INTERVAL 1 YEAR)
ORDER BY grandtotal
Maybe that would help
SELECT
table1.cost,
(SELECT SUM(expense) FROM table2 WHERE table2.key=table1.id) as expensetot,
(table1.cost+(SELECT SUM(expense) FROM table2 WHERE table2.key=table1.id)) as grandtotal,
table3.label
FROM
table1
LEFT JOIN table3 ON table3.key=table1.id
WHERE
table1.saledate>SUBDATE(CURDATE(), INTERVAL 1 YEAR)
ORDER BY grandtotal
or you can read about user variables in queries:
https://dev.mysql.com/doc/refman/8.0/en/user-variables.html

Every derived table must have its own alias? How is this wrong

I've given my derived table an alias but still when i run the query it gives me the error "Every derived table must have its own alias".
select a,b,c,sum(d) as 'sum'
from(select a,b,c,sum(d)as 'd' from e join f using(z)) as 'alias'
group by a;
EDIT: better sample
----This gives derived table error-----
select name,sum(pop) as 'total' from(select name as 'name',sum(population)
as pop from table1 join table2 using(countrycode));
----This gives me the SQL syntax error--------
select name,sum(pop) as 'total' from(select name as 'name',sum(population)
as pop from table1 join table2 using(countrycode)) as 'alias';
Thanks in advance.
Try this :
select name,
sum(pop) as `total`
from(
select name as `name`, sum(population) as pop
from table1
join table2 using(countrycode)
) `m` <---- alias needed here too

order without duplicates

I have a table as follows:
ident_nr|proj_nr|start_time|
----------------------------
05.26.73|0000001|1116936920|
09.56.df|0000002|1213431234|
11.ac.12|0000003|1236485758|
98.er.df|0000001|1287789755|
70.12.5n|0000001|1011245554|
33.dt.vp|0000003|1239125544|
And I want a result like this:
ident_nr|proj_nr|start_time|
----------------------------
98.er.df|0000001|1287789755|
09.56.df|0000002|1213431234|
33.dt.vp|0000003|1239125544|
where proj_nr is in asc order and start time with the max value.
Note: ident_nr is unique and proj_nr could have multiple ident_nr.
Database: MySQL.
Is there an SQL query that could achieve this result?
Thanks
SELECT t.ident_nr, t.proj_nr, t.start_time
FROM YourTable t
INNER JOIN (SELECT proj_nr, MAX(start_time) AS MaxTime
FROM YourTable
GROUP BY proj_nr) q
ON t.proj_nr = q.proj_nr
AND t.start_time = q.MaxTime
ORDER BY t.proj_nr;
SELECT t1.*
FROM table AS t1
INNER JOIN (
SELECT proj_nr, MAX(start_time) AS MaxTime
FROM table
GROUP BY proj_nr) AS t2
ON (t1.proj_nr = t2.proj_nr AND t1.start_time = t2.MaxTime)
Your criteria seems to be MAX(start_time) in your sample data. If not, please be more detailed in your question about what you want.