I'm working on a query, and I'm a bit stuck.
Here's my query:
SELECT *
FROM
routine_actions AS ra
JOIN
routines AS r ON r.id = ra.routine_id
JOIN
account_routines AS ar ON ar.routine_id = r.id
JOIN
accounts AS a ON a.id = ar.account_id
WHERE
(ra.last_run + INTERVAL ra.interval_minutes MINUTE <= NOW() OR ra.last_run IS NULL)
AND
r.created_at + INTERVAL r.runtime_days DAY > NOW()
What I'm trying to do:
An account has many routines. Only one routine can be used at a time, and that's the routine with the highest priority. The table that contains the priority column is account_routines because accounts can reuse routines and specify a different priority.
A higher number equals a higher priority. Currently the query is pulling all routines from all accounts. But I only need one routine with the highest priority from each account.
How is this possible? I don't need the solution, just where to look so I can figure out how to solve this problem.
I think you need to locate the max(priority) and include that in the joins to limit the rows returned. e.g.
SELECT
*
FROM routine_actions AS ra
JOIN routines AS r ON r.id = ra.routine_id
JOIN account_routines AS ar ON ar.routine_id = r.id
JOIN (
SELECT
account_id
, MAX(priority) AS max_priority
FROM account_routines
GROUP BY
account_id
) AS mxr ON ar.priority = mxr.max_priority
AND ar.account_id = mxr.account_id
JOIN accounts AS a ON a.id = ar.account_id
WHERE ...
Related
I want to get all the data from the users table & the last record associated with him from my connection_history table , it's working only when i don't add at the end of my query
ORDER BY contributions DESC
( When i add it , i have only the record wich come from users and not the last connection_history record)
My question is : how i can get the entires data ordered by contributions DESC
SELECT * FROM users LEFT JOIN connections_history ch ON users.id = ch.guid
AND EXISTS (SELECT 1
FROM connections_history ch1
WHERE ch.guid = ch1.guid
HAVING Max(ch1.date) = ch.date)
The order by should not affect the results that are returned. It only changes the ordering. You are probably getting what you want, just in an unexpected order. For instance, your query interface might be returning a fixed number of rows. Changing the order of the rows could make it look like the result set is different.
I will say that I find = to be more intuitive than EXISTS for this purpose:
SELECT *
FROM users u LEFT JOIN
connections_history ch
ON u.id = ch.guid AND
ch.date = (SELECT Max(ch1.date)
FROM connections_history ch1
WHERE ch.guid = ch1.guid
)
ORDER BY contributions DESC;
The reason is that the = is directly in the ON clause, so it is clear what the relationship between the tables is.
For your casual consideration, a different formatting of the original code. Note in particular the indented AND suggests the clause is part of the LEFT JOIN, which it is.
SELECT * FROM users
LEFT JOIN connections_history ch ON
users.id = ch.guid
AND EXISTS (SELECT 1
FROM connections_history ch1
WHERE ch.guid = ch1.guid
HAVING Max(ch1.date) = ch.date
)
We can use nested queries to first check for max_date for a given user and pass the list of guid to the nested query assuming all the users has at least one record in the connection history table otherwise you could use Left Join instead.
select B.*,X.* from users B JOIN (
select A.* from connection_history A
where A.guid = B.guid and A.date = (
select max(date) from connection_history where guid = B.guid) )X on
X.guid = B.guid
order by B.contributions DESC;
I've found a few posts in here that are similar, but doesn't work with what i'd like to do...
similar post: Trying to write a query that counts multiple things with different where cases
similar post: Query that Counts records with a WHERE clause
what I want to do is I have some... 200 groups, and within those groups are people with specific application dates. I want a count of how many people are in those groups that have a application date that falls within a specific range.
So this is the first method i've been using, but it only works for 1 group at a time
SELECT count(*) as count
FROM membersapplication ma
INNER JOIN members mb on mb.mbr_id = ma.mbr_id
WHERE (GPL_ID = 20179) and (ma.mpl_effectivedate >= '2/01/2015' and ma.mpl_effectivedate <= '4/30/2015') and (ma.mpl_cancellationdate is null)
This code takes the count of anyone that falls under GPL_ID 20179 (group placement id), i have 200 GPL_ID's that I would like this to run for, there is never a duplicate GPL_ID.
SELECT Gr.GPL_ID, Gr.GPL_Effectivedate, G.GRP_Enrolltype, G.GRP_Name, G.GRP_ID, G.GRP_Executive
FROM groupsreview gr
INNER JOIN groups g on gr.grp_ID = g.grp_ID
WHERE (GRP_ENROLLTYPE = 1) and (gp.gpl_effectivedate >= '4/30/2014' and gp.gpl_effectivedate <= '4/30/2015')
order by grp_name asc
This code gives me a list of every GPL_ID that I want (based off GRP_Enrolltype = 1) that falls within my desired date range
I basically would like to combine the two codes so that the 2nd set of code adds another column that has a count based off the fist code
Seems you really just need add GROUP BY to your query:
SELECT ma.GPL_ID, count(*) as count
FROM membersapplication ma
INNER JOIN members mb
ON mb.mbr_id = ma.mbr_id
where (ma.mpl_effectivedate >= '2/01/2015' and ma.mpl_effectivedate <= '4/30/2015')
AND (ma.mpl_cancellationdate is null)
GROUP BY ma.GPL_ID
This should do it. But I would double check the dates, I just used the ones you supplied; they don't match, and I am not sure if they should:
SELECT ma.GPL_ID, count(*) as count
FROM groups g
INNER JOIN groupsreview AS gr ON g.grp_ID = gr.grp_ID
INNER JOIN membersapplication AS ma ON gr.GPL_ID = ma.GPL_ID
INNER JOIN members AS mb ON mb.mbr_id = ma.mbr_id
WHERE g.GRP_ENROLLTYPE = 1
AND gr.gpl_effectivedate BETWEEN 20140430 AND 20150430
AND ma.mpl_effectivedate BETWEEN 20150201 and 20150430
AND ma.mpl_cancellationdate IS NULL
GROUP BY ma.GPL_ID
;
Judging from your question's wording, it feels a little odd to group by GPL_ID instead of grp_ID.
Not sure if this will work, but I can give it a try:
SELECT
*
FROM
(SELECT
count(*) as count, GPL_ID
FROM
membersapplication ma
inner join members mb ON mb.mbr_id = ma.mbr_id
where
(ma.mpl_effectivedate >= '2/01/2015'
and ma.mpl_effectivedate <= '4/30/2015')
and (ma.mpl_cancellationdate is null)
GROUP BY GPL_ID) T1
INNER JOIN
(SELECT
Gr.GPL_ID,
Gr.GPL_Effectivedate,
G.GRP_Enrolltype,
G.GRP_Name,
G.GRP_ID,
G.GRP_Executive
FROM
groupsreview gr
inner join groups g ON gr.grp_ID = g.grp_ID
WHERE
(GRP_ENROLLTYPE = 1)
and (gp.gpl_effectivedate >= '4/30/2014'
and gp.gpl_effectivedate <= '4/30/2015')) T2
ON T1.GPL_ID = T2.GPL_ID
Basically you should approach this by combining joins and then grouping on GPL_ID along with a having clause. Here's what came up with.
SELECT Gr.GPL_ID, Gr.GPL_Effectivedate, G.GRP_Enrolltype, G.GRP_Name, G.GRP_ID, G.GRP_Executive
count(*) as grp_count
FROM membersapplication ma
INNER JOIN members mb on mb.mbr_id = ma.mbr_id
INNER JOIN groupsreview gr on mb.GPL_ID = gr.GPL_ID
INNER JOIN groups g on gr.grp_ID = g.grp_ID
WHERE (GRP_ENROLLTYPE = 1) and (gp.gpl_effectivedate >= '4/30/2014' and gp.gpl_effectivedate <= '4/30/2015')
GROUP BY Gr.GPL_ID, Gr.GPL_Effectivedate, G.GRP_Enrolltype, G.GRP_Name, G.GRP_ID, G.GRP_Executive
HAVING (ma.mpl_effectivedate >= '2/01/2015' and ma.mpl_effectivedate <= '4/30/2015') and (ma.mpl_cancellationdate is null)
order by grp_name asc
Hopefully this helps
There are 3 tables, persontbl1, persontbl2 (each 7500 rows) and schedule (~3000 active schedules i.e. schedule.status = 0). Person tables contain data for the same persons as one to one relationship and INNER join between two takes less than a second. And schedule table contains data about persons to be interviewed and not all persons have schedules in schedule table. With Left join query instantly takes around 45 seconds, which is causing all sorts of issues.
SELECT persontbl1._CREATION_DATE, persontbl2._TOP_LEVEL_AURI,
persontbl2.RESP_CNIC, persontbl2.RESP_CNIC_NAME,
persontbl1.MOB_NUMBER1, persontbl1.MOB_NUMBER2,
schedule.id, schedule.call_datetime, schedule.enum_id,
schedule.enum_change, schedule.status
FROM persontbl1
INNER JOIN persontbl2 ON (persontbl2._TOP_LEVEL_AURI = persontbl1._URI)
AND (AGR_CONTACT=1)
LEFT JOIN SCHEDULE ON (schedule.survey_id = persontbl1._URI)
AND (SCHEDULE.status=0)
AND (DATE(SCHEDULE.call_datetime) <= CURDATE())
ORDER BY schedule.call_datetime IS NULL DESC, persontbl1._CREATION_DATE ASC
Here is the explain for query:
Schedule Table structure:
Schedule Table indexes:
Please let me know if any further information is required.
Thanks.
Edit: Added fully qualified table names and their columns.
You should just replace this line:
AND (DATE(SCHEDULE.call_datetime) <= CURDATE())
to this one:
AND SCHEDULE.call_datetime <= '2015-04-18 00:00:00'
so mysql will not call 2 functions per every record but will use static constant '2015-04-18 00:00:00'.
So you can just try for performance improvements if your query is:
SELECT persontbl1._CREATION_DATE, persontbl2._TOP_LEVEL_AURI,
persontbl2.RESP_CNIC, persontbl2.RESP_CNIC_NAME,
persontbl1.MOB_NUMBER1, persontbl1.MOB_NUMBER2,
schedule.id, schedule.call_datetime, schedule.enum_id,
schedule.enum_change, schedule.status
FROM persontbl1
INNER JOIN persontbl2 ON (persontbl2._TOP_LEVEL_AURI = persontbl1._URI)
AND (AGR_CONTACT=1)
LEFT JOIN SCHEDULE ON (schedule.survey_id = persontbl1._URI)
AND (SCHEDULE.status=0)
AND (SCHEDULE.call_datetime <= '2015-02-01 00:00:00')
ORDER BY schedule.call_datetime IS NULL DESC, persontbl1._CREATION_DATE ASC
EDIT 1 So you said without LEFT JOIN part it was fast enough, so you can try then:
SELECT persontbl1._CREATION_DATE, persontbl2._TOP_LEVEL_AURI,
persontbl2.RESP_CNIC, persontbl2.RESP_CNIC_NAME,
persontbl1.MOB_NUMBER1, persontbl1.MOB_NUMBER2,
s.id, s.call_datetime, s.enum_id,
s.enum_change, s.status
FROM persontbl1
INNER JOIN persontbl2 ON (persontbl2._TOP_LEVEL_AURI = persontbl1._URI)
AND (AGR_CONTACT=1)
LEFT JOIN
(SELECT *
FROM SCHEDULE
WHERE status=0
AND call_datetime <= '2015-02-01 00:00:00'
) s
ON s.survey_id = persontbl1._URI
ORDER BY s.call_datetime IS NULL DESC, persontbl1._CREATION_DATE ASC
I'm guessing that AGR_CONTACT comes from p1. This is the query you want to optimize:
SELECT p1._CREATION_DATE, _TOP_LEVEL_AURI, RESP_CNIC, RESP_CNIC_NAME,
MOB_NUMBER1, MOB_NUMBER2,
s.id, s.call_datetime, s.enum_id, s.enum_change, s.status
FROM persontbl1 p1 INNER JOIN
persontbl2 p2
ON (p2._TOP_LEVEL_AURI = p1._URI) AND (p1.AGR_CONTACT = 1) LEFT JOIN
SCHEDULE s
ON (s.survey_id = p1._URI) AND
(s.status = 0) AND
(DATE(s.call_datetime) <= CURDATE())
ORDER BY s.call_datetime IS NULL DESC, p1._CREATION_DATE ASC;
The best indexes for this query are: persontbl2(agr_contact), persontbl1(_TOP_LEVEL_AURI, _uri), and schedule(survey_id, status, call_datime).
The use of date() around the date time is not recommended. In general, that precludes the use of indexes. However, in this case, you have a left join, so it doesn't make a difference. That column is not being used for filtering anyway. The index on schedule is only for covering the on clause.
I have a MySQL query that maps Users to Zones according to their location, and the zone boundaries:
UPDATE User u SET u.zoneId = (
SELECT z.id FROM Zone z
WHERE ST_Contains(z.boundary, u.location)
ORDER BY z.level DESC
LIMIT 1
);
This works fine, but it is quite slow as it's performing a subquery for every single record.
Is it possible to rewrite it using a JOIN, even though it's using ORDER BY ... LIMIT 1 in the subquery?
This ORDER BY ... LIMIT 1 is necessary as several encapsulated zones can match a location, and only the smallest one (highest level) must be assigned.
In the absence of any test data or full DDLs I can't really test this, but this might work. Using joins and a sub query, but burying the sub query an extra level down which might allow MySQL to ignore he use of the table to be updated in the sub query.
Does rely on the user table having a unique key ( I have just taken it as being called id ).
UPDATE User u
INNER JOIN Zone z
ON ST_Contains(z.boundary, u.location)
INNER JOIN
(
SELECT id, MaxLevel
FROM
(
SELECT u.id, MAX(z.level) AS MaxLevel
FROM User u
INNER JOIN Zone z
ON ST_Contains(z.boundary, u.location)
GROUP BY u.id
) Sub1
) Sub2
ON u.id = Sub2.id AND z.level = Sub2.MaxLevel
SET u.zoneId = z.id
If you could set up some test data in SQL fiddle I can test this.
I am having trouble getting a MySQL query to work for me. Here is the setup.
A customer has asked me to compile a report from some accounting data. He wants to select a date (and possibly other criteria) and have it return all of the following (an OR statement):
1.) All invoices that were inserted on or after that date
2.) All invoices regardless of their insert date that have corresponding payments in a separate table whose insert dates are on or after the selected date.
The first clause is basic, but I am having trouble pairing it with the second.
I have assembled a comparable set of test data in an SQL Fiddle. The query that I currently have is provided.
http://www.sqlfiddle.com/#!2/d8d9c/3/2
As noted in the comments of the fiddle, I am working with July 1, 2013 as my selected date. For the test to work, I need invoices 1 through 5 to appear, but not invoice #6.
Try this: http://www.sqlfiddle.com/#!2/d8d9c/9
Here are the summarized changes
I got rid of your GROUP BY. You did not have any aggregate functions. I used DISTINCT instead to eliminate duplicate records
I removed your implicit joins and put explicit joins in their place for readability. Then I changed them to LEFT JOINs. I am not sure what your data looks like but at a minimum, I would assume you need the payments LEFT JOINed if you want to select an invoice that has no payments.
This will probably get you the records you want, but those subselects in the SELECT clause may perform better as LEFT JOINs then using the SUM function
Here is the query
SELECT DISTINCT
a.abbr landowner,
CONCAT(f.ForestLabel, '-', l.serial, '-', l.revision) leasenumber,
i.iid,
FROM_UNIXTIME(i.dateadded,'%M %d, %Y') InvoiceDate,
(SELECT IFNULL(SUM(ch.amount), 0.00) n FROM test_charges ch WHERE ch.invoiceid = i.iid) totalBilled,
(SELECT SUM(p1.amount) n FROM test_payments p1 WHERE p1.invoiceid = i.iid AND p1.transtype = 'check' AND p1.status = 2) checks,
(SELECT SUM(p1.amount) n FROM test_payments p1 WHERE p1.invoiceid = i.iid AND p1.transtype = 'ach' AND p1.status = 2) ach,
CASE WHEN i.totalbilled < 0 THEN i.totalbilled * -1 ELSE 0.00 END credits,
CASE WHEN i.balance >= 0 THEN i.balance ELSE 0.00 END balance,
t.typelabel, g.groupname
FROM test_invoices i
LEFT JOIN test_contracts c
ON i.contractid = c.cid
LEFT JOIN test_leases l
ON c.leaseid = l.bid
LEFT JOIN test_forest f
ON l.forest = f.ForestID
LEFT JOIN test_leasetypes t
ON l.leasetype = t.tid
LEFT JOIN test_accounts a
ON l.account = a.aid
LEFT JOIN test_groups g
ON c.groupid = g.gid
LEFT JOIN test_payments p
ON p.invoiceid = i.iid
WHERE (i.dateadded >= #startdate) OR (p.dateadded >= #startdate)
Try this.
http://www.sqlfiddle.com/#!2/d8d9c/11/2
TL;DR:
… AND (i.dateadded > #startdate
OR EXISTS (
SELECT * FROM test_payments
WHERE test_payments.invoiceid = i.iid
AND test_payments.dateadded >= #startdate))