Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
as part of an inner query I need to select the latest department number (dept_no) where each employee (emp_no) has worked. Note, some employees have worked in multiple departments. The name of this table is dept_emp.
It seems very simple (and probably is), but I haven't been able to figure it out. This is one of the queries I have tried
SELECT dept_no, emp_no, from_date
FROM dept_emp
GROUP BY emp_no
HAVING MAX(from_date);
This doesn't return the right results. for employee "10010" it returns dept_no "d004" and from_date "1996-11-24" while I expected "d006" and "2000-06-26"
Can anyone suggest a working query to me?
Your query is malformed. If MySQL accepts it you are probably running MySQL 5.x. MySQL 8.x does not accept those malformed queries by default anymore.
In order to get the rows you want you can use a subquery. For example:
select
d.dept_no,
d.emp_no,
d.from_date
from dept_emp d
join (
select emp_no, max(from_date) as max_from_date
from dept_emp
group by emp_no
) m on m.emp_no = d.emp_no
and m.max_from_date = d.from_date
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have this task:
Write a single query to display the fname and lname of employees whose salary is higher than $5000.
The problem is that there is 2 tables and fname and lname is from the table "employee" and salary is from the table "Job". How do I make this condition work?
I can only write this query, but I don't know how to write the fact that salary is in another table.
SELECT fname, lname FROM employee WHERE fixed_salary > 5000;
Please, help and thank you! :)
SELECT fname, lname FROM employee
INNER JOIN Job ON employee.job_id=job.job_id
WHERE fixed_salary > 5000;
basic SQL joining, read something about e.g. here:
https://dev.mysql.com/doc/refman/8.0/en/join.html
SELECT
e.fname,
e.lname,
j.fixed_salary
FROM employee e
INNER JOIN Job j ON j.id = e.job_id
WHERE j.fixed_salary > 5000;
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Trying to understand SQL more as part of a POC I'm working on and have hit a snag. I have two select queries (shown below) and I want to combine these into a single query that:
Shows all of the results of query one
Joins the result of query two if the category column matches.
Provides a default of 0 if there is no match
Query one:
SELECT activityId, location, category, activityScore FROM activities WHERE location = "manchester";
Query two:
SELECT userId, category, userScore FROM userscore s WHERE userId = "32976428";
Expected Output:
The resulting query should show all activities in "manchester" along with the associated userScore if the specified use has one that matches that category. If there is no userscore then 0 should be returned instead.
Thanks for any help.
Carl
I think you need a LEFT JOIN on your userscore table
SELECT a.activityId, a.location, a.category, a.activityScore,
s.userId, ISNULL(s.userScore,0) as userScore
FROM activities a
LEFT JOIN userscore s ON s.category = a.category AND s.userId = "32976428"
WHERE a.location = "manchester";
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have a table of multiple employment records of an employee/s where i want to query for a certain range of date.
Parameters are Company.id,date_hired,date_end
Results must be on the range of the specified date and employee.id must only be one if the employee has multiple employment records result must get the latest employment record..
THIS IS WHAT I CURRENTLY HAVE.
SELECT *
FROM employmentrecords
WHERE employmentrecords.id IN(
SELECT MAX(employmentrecords.id)
FROM employmentrecords
WHERE ((employmentrecords.date_end >='2017-08-22'
OR employmentrecords.date_end IS NULL
OR (employmentrecords.date_end <='2017-08-22'
AND employmentrecords.date_end >='2017-08-08'))
AND employmentrecords.date_hired <='2017-08-22')
GROUP
BY employmentrecords.employee_id)
AND employmentrecords.company_id<>0`
Hope any one would suggest a better approach.Thank you
I am not sure if I got your question all clear, this query below will give you the latest employee record if there are multiple user records -
SELECT * FROM employmentrecords WHERE id IN(SELECT MAX(id) FROM employmentrecords
WHERE ((date_end >='2017-08-22'
OR date_end IS NULL
OR (date_end <='2017-08-22' AND date_end >='2017-08-08'))
AND date_hired <='2017-08-22')
GROUP BY employee_id)
AND company_id<>0
and rownum = 1
order by date_hired desc
JFYI, no need to use the table name as alias if there is only one table you are fetching the data from, it just makes it hard to read the query on go.
SELECT id, max(date_hired)
FROM employmentrecords
WHERE ((employmentrecords.date_end >='2017-08-22'
OR employmentrecords.date_end IS NULL
OR (employmentrecords.date_end <='2017-08-22'
AND employmentrecords.date_end >='2017-08-08'))
AND employmentrecords.date_hired <='2017-08-22') group by id
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Update the emp table, move the employee with the highest commission to department number 40, make him/her a manager and give him/her a pay raise of £1000. Hint : Use a subquery and >= all as with the ALL and ANY slide on the SQL Commands Handout.
All I seem to get:
Any ideas?
Your query is:
update emp
set . . .
where comm = max(comm);
The error is correct for several reasons. The simplest is that aggregation functions do not belong in a where clause; nor do they belong in an update.
If you want to update one row, you would do:
update emp
set . . .
order by comm desc
limit 1;
If you want to update all rows where comm is maximum (and there could be multiple such rows), then use join:
update emp e join
(select max(comm) as maxcomm from emp) ee
on e.comm = ee.maxcomm
set . . .;
I should note that neither of these are standard SQL. The standard SQL method (which MySQL does not support) is:
update emp
set . . .
where comm = (select max(e2.comm) from emp e2);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Alright, after 3-4 days of trying to solve this I am giving up.
I started out with only one table but now I have changed it alot so I am able to have two tables, I thought it would make it alot easier for a rookie like me.
See my earlier post here
Instead of posting all the table details here I am providing a screenshot:
Tables and expected result
I have managed to come quite far with this but the problem is that I am not able to do the calculations excluding the batches that should not be included.
How could I solve this? I really appreciate the help I get from you guys.
Thanks in advance
First step: Create a filter for the details table
SELECT
MAX(id) AS id
FROM details
GROUP BY `concat`, `batch`
Next step: Use this to query the details table
SELECT * FROM details
WHERE id IN (
SELECT
MAX(id) AS id
FROM details
GROUP BY `concat`, `batch`
)
Next step: Use this derived table to join the master table for your final result
SELECT
`master`.id AS id,
`master`.plant AS plant,
`master`.`code` AS `code`,
COUNT(*) AS distinct_batches,
SUM(filtereddetails.volume) AS total_vol,
SUM(filtereddetails.`value`) AS total_val,
SUM(filtereddetails.volume*filtereddetails.risk) AS risk_vol,
SUM(filtereddetails.`value`*filtereddetails.risk) AS risk_val,
MAX(filtereddetails.end_date-filtereddetails.start_date) AS max_date_diff
FROM
`master`
INNER JOIN (
SELECT * FROM details
WHERE id IN (
SELECT
MAX(id) AS id
FROM details
GROUP BY `concat`, `batch`
)
) AS filtereddetails ON `master`.`concat`=filtereddetails.`concat`
GROUP BY
`master`.`concat`