Simplify SQL query for me please [closed] - mysql

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

Related

SELECT row COUNT and another row value MYSQL [closed]

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 1 year ago.
Improve this question
I need to get city and model + order by common model value.
Without city mysql query is preparing successfully but with city is not working.
How can i get two columns info in one query?
SELECT model
FROM cars
GROUP BY model
ORDER BY COUNT(model) DESC
LIMIT 6
not working: SELECT cars,model FROM cars GROUP BY model ORDER BY COUNT(model) DESC LIMIT 6
If you want the model/city combinations with the most rows, then include both in the select and group by:
SELECT model, city
FROM cars
GROUP BY model, city
ORDER BY COUNT(*) DESC
LIMIT 6

Why does my simple select SQL query fail? [closed]

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

How to find a list of entries where they have the same id and certain part numbers? [closed]

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 3 years ago.
Improve this question
I have a table which has three columns: id, layer and part_number.
The table looks like this:
I'd like to find the id that has part_numbers: 412789, 412801 & 412806. Can anyone help?
Thanks in advance!
You can group by record_id, and do an "IN" statement, to grab all ID's with those part numbers, and then fetch all the unique record_id's with any of those part numbers. In this case, it would be 1:
SELECT record_id
FROM parts_table
WHERE part_number IN (412789, 412789, 412806)
GROUP BY record_id
You can use distinct:
SELECT distinct record_id
FROM parts_table
WHERE part_number IN (412789, 412789, 412806)
I think what you're trying to return here is all the records from your table, where the record_id is shared across the 3 part_numbers.
It's a little messy, however, you should be able to do this with the following SQL:
with cte as
(select record_id,count(*) cnt
from parts_table
where part_number in (412789,412801,412806)
group by record_id having count(*) =3
)
select * from parts_table p
join cte c on c.record_id=p.record_id
where p.part_number in (412789,412801,412806)
I can't see the image of your table, but something like this should do the trick:
SELECT id
FROM (
SELECT id, part_number
FROM MyTable
WHERE part_number IN (142789, 412801, 412806) -- Restrict part numbers
GROUP BY id, part_number -- Get one row per id / part_number
) src
HAVING COUNT(*) = 3 -- Only return id's with three rows (one for each part_number)
GROUP BY id
Give it a try and let me know.
Update
To support variable part_numbers, you should parameterize the query using a stored procedure. Here are some links to get you started:
Stored Procedures - Intro
Stored Procedures - Parameters
Stored Procedures - More

Display group data along with their last latest message [closed]

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 3 years ago.
Improve this question
I want to display result having data categorized in the group with their latest message according to date.
I tried grouping and having along with nested queries but no luck.
SELECT groupName,date FROM chat where groupName like '%he%' group by groupName,date having min(date);
I want the two rows to be printed along having the latest message with them.Here it would be row 4 and row 5. this is just an arbitrary data though which I need to impplement on bulk.
You don't need group by, just filtering:
select c.*
from chat c
where c.date = (select max(c2.date) from chat c2 where c2.groupname = c.groupname);
You can do it with NOT EXISTS:
SELECT c.*
FROM chat c
WHERE
groupName LIKE '%he%'
AND
NOT EXISTS (
SELECT 1 FROM chat
WHERE groupname = c.groupname AND date > c.date
)

Mysql query relating to count and date time [closed]

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 7 years ago.
Improve this question
I have a table 'transaction' which contains columns transaction_id, sender_id,amount, date_time,payee_id .
I want to find transactions which are more than five made by sender_id in one day by single sender.
i tried
mysql> select * from transaction where sender_id count() =5 and datedif() =1;
I think this query will be helpful for you.
select sender_id, count(sender_id) as count
from `transaction`
group by sender_id, date(date_time)
having count>5
this will return the list of sender_ids who have more than 5 transactions
if you also need the list of that transactions than you need this
select t.*
from (
select sender_id, count(sender_id) as count
from `transaction`
group by sender_id, date(date_time)
having count>5
) as senders
inner join `transaction` as t on t.sender_id=senders.sender_id
PS. for good performance I recommend run second query on MySQL server with version 5.6.10 or higher