I have this table called employeetimesheets:
empsheet_id|employee_id|timesheet_status|last_update
The table allows the manager to have access to all the employee time sheets. One employee can have several time sheets. I would like to display only the most recent entry per employee. I read in the manual I have to write a groupwise-maximum subquery and left join with inner join but I'm unsure how to go about it here.
So far this is the query I have:
$sqlempsheets="SELECT * FROM employeetimesheets JOIN employees ON employeetimesheets.employee_id=employees.employee_id WHERE employeetimesheets.timesheet_status='Pending Approval'";
$resultempsheets=mysqli_query($db,$sqlempsheets);
Try this:
select *
from employeetimesheets t
join (
select employee_id,
max(empsheet_id) as empsheet_id
from employeetimesheets
group by employee_id
) t2 on t.employee_id = t2.employee_id
and t.empsheet_id = t2.empsheet_id
join employees e on t.employee_id = e.employee_id
where t.timesheet_status = 'Pending Approval';
Or using left join:
select t.*, e.*
from employeetimesheets t
left join employeetimesheets t2 on t.employee_id = t2.employee_id
and t.empsheet_id < t2.empsheet_id
join employees e on t.employee_id = e.employee_id
where t.timesheet_status = 'Pending Approval'
and t2.employee_id is null;
Related
So I have a large subquery and I would like to join on that subquery while using the result of the subquery in the join.
For example, I have a table called patient and one called appointment, and I would like to get the number of appointments per patient with given criteria.
Right now I am doing something like this:
SELECT
t1.*
FROM
(
SELECT
patient.name,
patient.id,
appointment.date
FROM
patient
LEFT JOIN appointment ON appointment.patient_id = patient.id
WHERE
/* a **lot** of filters, additional joins, etc*/
) t1
LEFT JOIN (
SELECT
COUNT(*) number_of_appointments,
patient.id
FROM
patient
LEFT JOIN appointment ON appointment.patient_id = patient.id
GROUP BY
patient.id
) t2 ON t1.id = t2.id
The problem is that this returns the number of appointments for each patient independent from the subquery above it. I tried writing the join as this:
LEFT JOIN (
SELECT
COUNT(*) number_of_appointments,
patient.id
FROM
t1
GROUP BY
patient.id
)
But obviously I'm getting an error saying that table t1 doesn't exist. Is there any way for me to do this cleanly without having to repeat all of the filters from t1 in t2?
Thanks!
Why not use window functions?
SELECT p.name, p.id, a.date,
COUNT(a.patient_id) OVER (PARTITION BY p.id) as num_appointments
FROM patient p LEFT JOIN
appointment a
ON a.patient_id = p.id
WHERE . . .
This provides the count based on the WHERE filtering. If you wanted a count of all appointments, then do the calculation before applying the WHERE:
SELECT p.name, p.id, a.date,
COALESCE(a.cnt, 0) as num_total_appointments,
COUNT(a.patient_id) OVER (PARTITION BY p.id) as num_matching appointments
FROM patient p LEFT JOIN
(SELECT a.*,
COUNT(*) OVER (PARTITION BY a.patient_id) as cnt
FROM appointment a
) a
ON a.patient_id = p.id
WHERE . . .
I´m trying to make a query listing all clients, and also get the last comment and
the date of that comment inside the table of history_client inside a single query for it to be listed.
select a.id_client,a.name,a.lastname,(select b.date_created,b.comentary
from history_of_client b where a.id_client = b.id_client_asociate) from clients_main_table
You could use an inner join on max(date_created) for id_client on history table and join
SELECT a.id_client,a.name,a.lastname, h.commentary
FROM clients_main_table a
INNER join (
select b.id_client_asociate, max(b.date_created) max_date
from history_of_client
group by b.id_client_asociate ) t on t.id_client_asociate = a.id_client
INNER JOIN history_of_client h on h.id_client_asociate = t.id_client_asociate
and h.date_created = t.max_date
Use the LEFT JOIN and INNER join to get your desired result set.
select a.id_client,
a.name,
a.lastname,
hc.date_created,
hc.comentary
from clients_main_table c
left join (select id_client_asociate,max(date_created) dt from history_of_client group by id_client_asociate) h
on (c.id_client = b.id_client_asociate)
inner join history_of_client hc
on (hc.id_client_asociate = b.id_client_asociate and hc.date_created = h.date_created)
This query:
select a.* , u.firstn, u.lastn, u.Extension
from jfi.ambitiontempdata a
inner join jackson_id.users u
on a.Extension = u.Extension;
Pulls up a lot of records I've created for a CSR call system showing call records, the number called and the number that initiated the call, etc.
This works great, but I need to filter it one more time by another piece of data.
I have a table called jfi.ambition_customer_data which doesn't have an ID or anything to join it in my existing query. However, I need to run the above query and check the jfi.ambition_customer_data table to see if certain phone numbers exist in each table. The number that I check from my jfi.ambitiontempdata table depends on two factors though.
From the query above, if a.outbound = 1 then check if called_party exists in jfi.ambition_customer_data, and if a.outbound = 0 then check to see if calling_party exists.
Here's a screenshot of my results for reference:
Here is data from the customer table, so the above call fields need to be compared to 'Phone' in this table:
So how can I filter my select data to make sure that I only get records where the 2 different numbers may exist in the customer table depending on the call type?
A simple union will suffice, I think, and is probably easier to understand:
select a.* FROM ambitiontempdata a
inner join customer c on a.called_party = c.phone
where a.outbound = 1
UNION
select a.* FROM ambitiontempdata a
inner join customer c on a.calling_party = c.phone
where a.outbound = 0;
EDIT
To combine this simply with your own query you can do:
select a.* , u.firstn, u.lastn, u.Extension
from jfi.ambitiontempdata a
inner join jackson_id.users u
on a.Extension = u.Extension
inner join jfi.ambition_customer_data c
on a.called_party = c.phone
where a.outbound = 1
UNION
select a.* , u.firstn, u.lastn, u.Extension
from jfi.ambitiontempdata a
inner join jackson_id.users u
on a.Extension = u.Extension
inner join jfi.ambition_customer_data c
on a.calling_party = c.phone
where a.outbound = 0;
SECOND EDIT
Is this any better?
SELECT ua.*, u.firstn, u.lastn, u.Extension
FROM
(select a.* FROM ambitiontempdata a
inner join customer c on a.called_party = c.phone
where a.outbound = 1
UNION
select a.* FROM ambitiontempdata a
inner join customer c on a.calling_party = c.phone
where a.outbound = 0) ua
INNER JOIN jackson_id.users u
on ua.Extension = u.Extension
Select
a.* , u.firstn, u.lastn, u.Extension
IF(a.outbound = 1,
(select phone from jfi.ambition_customer_data where customerNumber = a.calling_party),
(select phone from jfi.ambition_customer_data where customerNumber = a.called_party)
)
From jfi.ambitiontempdata a
INNER JOIN jackson_id.users u
ON a.Extension = u.Extension;
I have three tables, libraryitems, copies and loans.
A libraryitem hasMany copies, and a copy hasMany loans.
I'm trying to get the latest loan entry for a copy only; The query below returns all loans for a given copy.
SELECT
libraryitems.title,
copies.id,
copies.qruuid,
loans.id AS loanid,
loans.status,
loans.byname,
loans.byemail,
loans.createdAt
FROM copies
INNER JOIN libraryitems ON copies.libraryitemid = libraryitems.id AND libraryitems.deletedAt IS NULL
LEFT OUTER JOIN loans ON copies.id = loans.copyid
WHERE copies.libraryitemid = 1
ORDER BY copies.id ASC, loans.createdAt DESC
I know there needs to be a sub select of some description in here, but struggling to get the correct syntax. How do I only return the latest, i.e MAX(loans.createdAt) row for each distinct copy? Just using group by copies.id returns the earliest, rather than latest entry.
Image example below:
in the subquery , getting maximum created time for a loan i.e. latest entry and joining back with loans to get other details.
SELECT
T.title,
T.id,
T.qruuid,
loans.id AS loanid,
loans.status,
loans.byname,
loans.byemail,
loans.createdAt
FROM
(
SELECT C.id, C.qruuid, L.title, MAX(LN.createdAt) as maxCreatedTime
FROM Copies C
INNER JOIN libraryitems L ON C.libraryitemid = L.id
AND L.deletedAt IS NULL
LEFT OUTER JOIN loans LN ON C.id = LN.copyid
GROUP BY C.id, C.qruuid, L.title) T
JOIN loans ON T.id = loans.copyid
AND T.maxCreatedTime = loans.createdAt
A self left join on loans table will give you latest loan of a copy, you may join the query to the other tables to fetch the desired output.
select * from loans A
left outer join loans B
on A.copyid = B.copyid and A.createdAt < B.createdAt
where B.createdAt is null;
This is your query with one simple modification -- table aliases to make it clearer.
SELECT li.title, c.id, c.qruuid,
l.id AS loanid, l.status, l.byname, l.byemail, l.createdAt
FROM copies c INNER JOIN
libraryitems li
ON c.libraryitemid = li.id AND
li.deletedAt IS NULL LEFT JOIN
loans l
ON c.id = l.copyid
WHERE c.libraryitemid = 1
ORDER BY c.id ASC, l.createdAt DESC ;
With this as a beginning let's think about what you need. You want the load with the latest createdAt date for each c.id. You can get this information with a subquery:
select l.copyid, max(createdAt)
from loans
group by l.copyId
Now, you just need to join this information back in:
SELECT li.title, c.id, c.qruuid,
l.id AS loanid, l.status, l.byname, l.byemail, l.createdAt
FROM copies c INNER JOIN
libraryitems li
ON c.libraryitemid = li.id AND
li.deletedAt IS NULL LEFT JOIN
loans l
ON c.id = l.copyid LEFT JOIN
(SELECT l.copyid, max(l.createdAt) as maxca
FROM loans
GROUP BY l.copyid
) lmax
ON l.copyId = lmax.copyId and l.createdAt = lmax.maxca
WHERE c.libraryitemid = 1
ORDER BY c.id ASC, l.createdAt DESC ;
This should give you the most recent record. And, the use of left join should keep all copies, even those that have never been leant.
I'm breaking my head trying to modify this query(thx sgeddes) in order to
get at result not only from db_events.events fields, instead join it with some db_system.devices fields
SELECT e.*
FROM db_events.events e
JOIN (
SELECT Max(id) MaxId, device_id
FROM db_events.events
GROUP BY device_id ) e2 on e.Id = e2.MaxId AND e.device_id = e2.device_id
WHERE e.device_id IN (
SELECT device_id
FROM db_system.devices
WHERE vendor = 1)
ORDER BY e.id DESC
How can I get it without repeat the subquery:
SELECT *
FROM db_system.devices
WHERE vendor = 1
I need get db_system.devices.brand and db_system.devices.model joined with final results, and
I tried to modify it step to step, I tried with temporary tables, I suspect it should be something simple, but I have not been able to do it, of course thank you very much...
is this what you want?
SELECT e.*, a.*
FROM db_events.events e
INNER JOIN
(
SELECT Max(id) MaxId, device_id
FROM db_events.events
GROUP BY device_id
) e2 on e.Id = e2.MaxId AND
e.device_id = e2.device_id
INNER JOIN db_system.devices a
ON e.device_id = a.device_id AND
a.vendor = 1
ORDER BY e.id DESC
the condition a.vendor = 1 can also be moved on WHERE clause and the result is still the same since you are using INNER JOIN
SELECT ....
FROM .... JOIN ....
WHERE a.vendor = 1
ORDER BY ...