EXPLANATION
I am working on a project where my client sends documents (contracts) out to their customers. These documents go through several rounds of "events" as they are send back and forth (ex. Prep, Sent to Client, Received Revised From Client, Signed By Client, etc). For a specific company (companyID 123456), I am trying to pull the most recent event for each document. So the example query below might return say 5 different documents, and each might have 4 different events they went through. In the example results I have simplified it down to a single document (documentId 6789).
Given the example results below, I expect to get the third row. All rows are for a single document, and that one has the most recent date. However, I get four as you see below.
If I Group By the documentID, then I get the right date, but my event and event id values are incorrect (transposed). The only success I have had is to wrap each value in the Select statement in a Max() function. When I do that I get the one row I want with the right event. However, things like the document ID are wrong because of course it returns me the max id, not the one that matches.
Could someone please help me adjust this query so I get the result I need? Thanks in advance!
Note: I found this "solution" here, but I don't think it applies directly to what I am doing:
Fetch the row which has the Max value for a column
QUERY
SELECT e.eventID,
e.event,
de.documentEventID,
de.documentID,
Max(de.eventDate) AS eventDate,
sd.companyID,
FROM siteDocuments sd
LEFT JOIN documents d ON d.documentID = sd.documentID
LEFT JOIN documentTypes dt ON dt.documentTypeID = d.documentTypeID
LEFT JOIN documentEvents de ON de.documentID = sd.documentID
LEFT JOIN events e ON e.eventID = de.eventID
WHERE sd.companyID = 123456
GROUP BY e.eventID
EXAMPLE RESULTS
-----------------------
EventId --- Event Doc --- EventId --- documentId --- eventDate
16 -------- FakeEventA -- 135791 ------ 6789 ------- 2012-04-11 08:35:54
32 -------- FakeEventB -- 726351 ------ 6789 ------- 2012-04-11 08:56:02
24 -------- FakeEventC -- 987236 ------ 6789 ------- 2012-05-09 16:48:57 <======
81 -------- FakeEventD -- 982378 ------ 6789 ------- 2012-04-20 14:06:19
(I put the dashes in to enforce formatting)
Are you sure the answers on the linked question (specifically this answer) aren't doing exactly what you want?
SELECT sd.*
FROM siteDocuments AS sd
LEFT OUTER JOIN siteDocuments AS sd2
ON (sd.documentId = sd2.documentId AND sd.eventDate < sd2.eventDate)
WHERE
sd2.documentId IS NULL
and sd.documentId = 6789;
I've used that answer before to achieve what you're looking for. (To maintain performance, you have to be careful to set up indexes correctly on large datasets, however--varies based on the DBMS you're using).
Related
I have the following MySQL 8 tables:
[submissions]
===
id
submission_type
name
[reject_reasons]
===
id
name
[submission_reject_reasons] -- crosswalk joining the first 2 tables
===
id
submission_id
reject_reason_id
In my application, users can submit submissions, and other users can request changes to those submissions. When they request these rejections, 1+ entries get saved to the submission_reject_reasons table (which stores the ID of the submission for which rejections are requested, as well as the ID of the reason for why the rejection is being made). So a typical entry in the table might look like:
id submission_id reject_reason_id
==============================================
45 384 294
Where submission_id = 384 is the "Fizz Buzz" submission and reject_reason_id = 294 is the "Missing Required Field" reason.
I currently have a query that fetches all the reject_reasons out of the DB:
SELECT * FROM reject_reasons
I now want to modify this query to sort the results based on their usage frequency. Meaning the query might currently return:
294 | Missing Required Field
14 | Malformed Entry
1885 | Makes No Sense
etc. But lets say there are 5 entries in the submission_reject_reasons table where 294 (Missing Required Field) is the reject_reason_id, and say there are 15 enries where 1885 (Makes No Sense) is present, and 120 entries where 14 (Malformed Entry) are present. I need a query that returns all reject_reasons sorted by their count in the submission_reject_reasons (SRR) table, descending, so that the most frequently used appear earlier in the sort. Hence the result set would be:
14 | Malformed Entry --> because there are 120 instances of this in the SRR table
1885 | Makes No Sense --> because there are 15 instances in the SRR
294 | Missing Required Field --> because there are only 5 instances in the SRR
Furthermore, I need a ranking from most-used to least-used. If a reason doesn't exist in the SRR table it should have a default "count" of zero (0) but should still come back in the query. If 2+ reason counts are tied, then I don't care how they are sorted. Any ideas here? I need the final result set to only contain the rr.id and rr.name field/values.
My best attempt is not getting me anywhere:
SELECT rr.id, rr.name
FROM reject_reasons AS rr
LEFT JOIN submission_reject_reasons AS srr on rr.id = srr.reject_reason_id
GROUP BY rr.id
ORDER BY COUNT(*) DESC
Can anyone help me over the finish line here? Can anyone spot where I'm goin awry? Thanks in advance!
You should be grouping by the reject reason ID. COUNT(*) is what you want to count in each group.
SELECT rr.id, rr.name
FROM reject_reasons AS rr
JOIN submission_reject_reasons AS srr on rr.id = srr.reject_reason_id
GROUP BY rr.id
ORDER BY COUNT(*) DESC
There's no need for any EXISTS check, since the INNER JOIN won't return any reject reasons that don't exist in submission_reject_reasons.
I have very limited experience with MySQL past standard queries, but when it comes to joins and relations between multiple tables I have a bit of an issue.
I've been tasked with creating a job that will pull a few values from a mysql database every 15 minutes but the info it needs to display is pulled from multiple tables.
I have worked with it for a while to figure out the relationships between everything for the phone system and I have discovered how I need to pull everything out but I'm trying to find the right way to create the job to do the joins.
I'm thinking of creating a new table for the info I need, with columns named as:
Extension | Total Talk Time | Total Calls | Outbound Calls | Inbound Calls | Missed Calls
I know that I need to start with the extension ID from my 'user' table and match it with 'extensionID' in my 'callSession'. There may be multiple instances of each extensionID but each instance creates a new 'UniqueCallID'.
The 'UniqueCallID' field then matches to 'UniqueCallID' in my 'CallSum' table. At that point, I just need to be able to say "For each 'uniqueCallID' that is associated with the same 'extensionID', get the sum of all instances in each column or a count of those instances".
Here is an example of what I need it to do:
callSession Table
UniqueCallID | extensionID |
----------------------------
A 123
B 123
C 123
callSum table
UniqueCallID | Duration | Answered |
------------------------------------
A 10 1
B 5 1
C 15 0
newReport table
Extension | Total Talk Time | Total Calls | Missed Calls
--------------------------------------------------------
123 30 3 1
Hopefully that conveys my idea properly.
If I create a table to hold these values, I need to know how I would select, join and insert those things based on that diagram but I'm unable to construct the right query/statement.
You simply JOIN the two tables, and do a group by on the extensionID. Also, add formulas to summarize and gather the info.
SELECT
`extensionID` AS `Extension`,
SUM(`Duration`) AS `Total Talk Time`,
COUNT(DISTINCT `UniqueCallID`) as `Total Calls`,
SUM(IF(`Answered` = 1,0,1)) AS `Missed Calls`
FROM `callSession` a
JOIN `callSum` b
ON a.`UniqueCallID` = b.`UniqueCallID`
GROUP BY a.`extensionID`
ORDER BY a.`extensionID`
You can use a join and group by
select
a.extensionID
, sum(b.Duration) as Total_Talk_Time
, count(b.Answered) as Total_Calls
, count(b.Answered) -sum(b.Answered) as Missed_calls
from callSession as a
inner join callSum as b on a.UniqueCallID = b.UniqueCallID
group by a.extensionID
This should do the trick. What you are being asked to do is to aggregate the number of and duration of calls. Unless explicitly requested, you do not need to create a new table to do this. The right combination of JOINs and AGGREGATEs will get the information you need. This should be pretty straightforward... the only semi-interesting part is calculating the number of missed calls, which is accomplished here using a "CASE" statement as a conditional check on whether each call was answered or not.
Pardon my syntax... My experience is with SQL Server.
SELECT CS.Extension, SUM(CA.Duration) [Total Talk Time], COUNT(CS.UniqueCallID) [Total Calls], SUM(CASE CS.Answered WHEN '0' THEN SELECT 1 ELSE SELECT 0 END CASE) [Missed Calls]
FROM callSession CS
INNER JOIN callSum CA ON CA.UniqueCallID = CS.UniqueCallID
GROUP BY CS.Extension
i'm having some trouble with trying to extract some data from several MySQL tables in a join statement.
My tables and attributes are:
appointment_end_time (table)
appointment_end_time_id (int)(pk)(ai)
appointment_end_date (datetime)
appointment_start_time (table)
appointment_date_id (int)(pk)(ai)
appointment_start_date (datetime)
instructor(table)
instructor_id (int)(pk)(ai)
firstname varchar(45)
lastname varchar(45)
appointment_timetable
appointment_timetable_id int(11) AI PK
instructor_id int(11) FK
appointment_date_id int(11) FK
appointment_end_time_id int(11) FK
SELECT a.appointment_timetable_id, i.instructor_id, ad.appointment_start_date, aet.appointment_end_date
FROM db12405956.appointment_timetable a
JOIN instructor i on i.instructor_id = a.instructor_id
JOIN appointment_start_time ad on ad.appointment_date_id = a.appointment_date_id
JOIN appointment_end_time aet on aet.appointment_end_time_id = a.appointment_end_time_id
ORDER BY a.appointment_timetable_id;
However, this code brings back no rows selected when executed so i'm wondering what i'm doing wrong, any help will be much appreciated
Sample rows:
(appointment_end_time)
appointment_end_time_id appointment_end_date
1 2016-12-26 14:00:00
2 2016-12-24 13:00:00
3 2016-12-26 13:00:00
(appointment_start_time)
appointment_date_id appointment_start_date
1 2016-12-26 15:00:00
2 2016-12-24 16:00:00
3 2016-12-26 15:30:00
instructor_id firstname lastname
1 Sasha Thompson
2 Laura Robinson
3 John Walters
appointment_timetable
appointment_timetable_id instructor_id appointment_date_id appointment_end_time_
1 Blank Blank Blank
2 Blank Blank Blank
3 Blank Blank Blank
What you need is to learn how to diagnose the problem yourself. It is a common problem that a query doesn't return the expected results and you should understand how to break things down to find the issue.
Let's start with your query:
SELECT a.appointment_timetable_id, i.instructor_id, ad.appointment_start_date, aet.appointment_end_date
FROM db12405956.appointment_timetable a
JOIN instructor i on i.instructor_id = a.instructor_id
JOIN appointment_start_time ad on ad.appointment_date_id = a.appointment_date_id
JOIN appointment_end_time aet on aet.appointment_end_time_id = a.appointment_end_time_id
ORDER BY a.appointment_timetable_id;
What you do to break it down is start with the first table and then add the joins (and where conditions although you don't have any here), one at a time until the data problem appears. I find this easiest to do by using select * or select top 1 * (Or top 10 as I usually prefer to see more than one record) instead of the field list because then you don't have to look for the fields that are associated with joins you haven't added in yet.
So start with
SELECT top 10 *
FROM db12405956.appointment_timetable a
Then try
SELECT top 10 *
FROM db12405956.appointment_timetable a
JOIN instructor i on i.instructor_id = a.instructor_id
Then
SELECT top 10 *
FROM db12405956.appointment_timetable a
JOIN instructor i on i.instructor_id = a.instructor_id
JOIN appointment_start_time ad on ad.appointment_date_id = a.appointment_date_id
Finally
SELECT top 10 *
FROM db12405956.appointment_timetable a
JOIN instructor i on i.instructor_id = a.instructor_id
JOIN appointment_start_time ad on ad.appointment_date_id = a.appointment_date_id
JOIN appointment_end_time aet on aet.appointment_end_time_id = a.appointment_end_time_id
ORDER BY a.appointment_timetable_id;
At some point you will see where the records fell out and that is the location of the problem. Then you might need to look at the fields you are joining on and the data in them in your data sets to see why they are not returning any matches. For instance, if you are joining on dates, they may be stored as dates in one table and as varchar in another and date "01/01/2016' is not equal to 'Jan 1, 2016' or sometimes the column has some sort of prefix or suffix not in the other table. Something like PR2345 in one table and 2345 in the other. Sometimes the query is correct and no rows genuinely meet the conditions. This could be because the data is not fully populated yet (think writing a report for a system that is not live yet, no data on completed actions because none have completed yet.) or because the requirement was wrong in some of its assumptions or because there should be no matching records. It could even be a bug in the data entry.
Depending on the nature of the problem, you might need to return all the records or only use select top 1 (since all records are disappearing). Using SELECT * this way will help when you are returning too many or duplicate records as well as sometimes is is the fields not being returning that affect the results set. Note that I am not saying to use SELECT * in your final result set, it is only being used as a diagnostic tool here.
In your case, the problem looks as if it is in the first table. There are blanks for instructor ID and the other fields in your sample, so there is nothing to join on. (You only gave a sample so the rest of the table may not be like this.) If this is a case where the data is not there yet due to the feature that would add it not yet being live, then you can test your query only by adding test data to the table. Be sure to delete this data after you have finished unit testing. If the data should have been there, then you need to look at the insert from the application for a bug.
I have a MySQL database which records pager messages which has two tables, one is a table with messages and the other is a table with recipients, where the link between the tables is pagermessages.pcapcode = capcodes.bcapcode (a numerical address each pager message is sent to).
This works semi-well, and I typically use an inner or left join to show the various pager messages and who the messages are for.
The issue I have, is that the system is dynamic, and the pagers can be re-programmed so that address 1, which might have been for recipient EXAMPLE1 may eventually be changed to be for EXAMPLE 2.
This leads to the issue, that I want to store the messages in a historical form, but I need a way to be able to specify between a date range. My thought was to have in the Capcodes table a date & time (bStartDateTime) and a finish date & time (bFinishDateTime) and then in my query be able to use this to work out how to join the pager messages.
This is where I am stuck though, I would normally do something like SELECT * from PagerMessages INNER JOIN Capcodes ON PagerMessages.pCapcode = Capcodes.bCapcode however what I want to now be able to do is to do the same thing, but where the pager message date (pDateTime) is between bStartDateTime and bFinishDateTime (which could be blank) to link, but if it is outside of that then to not link.
So take this example
Pager Message Table
ID|pDateTime |pCapcode|pMessage
1 |2014-06-24 14:00|1 |This is a test message
2 |2014-06-24 15:00|1 |This is a test message
3 |2014-06-24 16:00|2 |This is a test message
Capcode Table
CapcodeID|pCapcode|Name |bStartDateTime |bFinishDateTime
1 |1 |Example 1|2014-06-24 14:00|2014-06-24 14:30
2 |1 |Example 2|2014-06-24 14:31|
3 |2 |Example 3|2014-06-24 14:31|
In the above examples, what I would like to be able to do is to join the tables so that I can get a table like the below:
ID|pDateTime |pCapcode|Name |pMessage
1 |2014-06-24 14:00|1 |Example 1|This is a test message
2 |2014-06-24 15:00|1 |Example 2|This is a test message
3 |2014-06-24 16:00|2 |Example 3|This is a test message
So you can see, basically because the capcode exists twice in the pCapcode table, it has taken the entry in which the date & time falls between, this way I can then have multiple entries of the same bCapcode in the capcode table, and then if they get changed just add the date it was changed in the Capcodes table, and add a new entry rather than having historical entries that are no longer accurate as I can't select them properly.
Sorry if it does not make too much sense, I can clarify if needed but I think i have covered most of what I am asking, which is really what kind of an SQL statement would allow me to do the above, as it is not a simple INNER / LEFT JOIN from what I can see.
Thanks!
You can still join them. Just make sure only use the finish time if it exists.
select a.id, a.pdatetime, b.name, a.pmessage
from PageMessage a
join Capcode b on a.pcapcode = b.pcapcode and b.bstartdatetime <= a.pdatetime
and (b.bfinishdatetime is null or a.pdatetime <= b.bfinishdatetime);
select ID,pdatetime,pcapcode,0 as 'Name',pmessage from Pager_Message
union
select ID,0,pcapcode,name,0 from Capcode
I have a custom shop, and I need to redo the shipping. However, that is sometimes later, and in the meantime, I need to add a shipping option for when a cart only contains a certain range of products.
SO there is a ship_method table
id menuname name zone maxweight
1 UK Standard ukfirst 1 2000
2 UK Economy uksecond 1 750
3 Worldwide Air world_air 4 2000
To this I have added another column prod_restrict which is 0 for the existing ones, and 1 for the restricted ones, and a new table called ship_prod_restrict which contains two columns, ship_method_id and item_id, listing what products are allowed in a shipping category.
So all I need to do is look in my transactions, and for each cart, just check which shipping methods are either prod_restrict of 0 or have 1 and have no products in the cart that aren't in the restriction table.
Unfortunately it seems that because you can't values from an outer query to an inner one, I can't find a neat way of doing it. (edited to show the full query due to comments below)
select ship_method.* from ship_method, ship_prod_restrict where
ship_method.`zone` = 1 and prod_restrict='0' or
(
prod_restrict='1'
and ship_method.id = ship_prod_restrict.ship_method_id
and (
select count(*) from (
select transactions.item from transactions
LEFT JOIN ship_prod_restrict
on ship_prod_restrict.item_id = transactions.item
and ship_prod_restrict.ship_method_id=XXXXX
where transactions.session='shoppingcartsessionid'
and item_id is null
) as non_permitted_items < 1 )
group by ship_method.id
gives you a list of whether the section matches or not, and works as an inner query but I can't get that ship_method_id in there (at XXXXX).
Is there a simple way of doing this, or am I going about it the wrong way? I can't currently change the primary shipping table, as this is already in place for now, but the other bits can change. I could also do it within PHP but you know, that seems like cheating!
Not sure how the count is important, but this might be a bit lighter - hard to tell without a full table schema dump:
SELECT COUNT(t.item) FROM transactions t
INNER JOIN ship_prod_restrict r
ON r.item_id = t.item
WHERE t.session = 'foo'
AND r.ship_method_id IN (**restricted, id's, here**)