Returning unique values in a MS Access query based upon a set a rules - sql

I have a query that returns a dataset with UserID, CourseID, Course Completion date, Course Registration Date and Status (successful, not attempted, not finished).
However people are allowed to do a course multiple times.
For 1 report I need to get a unique recordset (based upon the Combination UserID and CourseID) back according to the following rules:
If a course is completed successfully by a learner take only that value
If a course is completed successfully by a learner multiple times, take the first completion date.
if a course not completed successfully by a learner take the last registration date.
I know how to create a query that only returns unique (Distinct) values, but not how to do it with a set of rules.

I see that this is a bit older. I don't know if this directly answers your question because I couldn't think of a way to write a single query to do this. I did however come up with a solution that I tested myself.
Using 3 tables and 4 queries, I think that I have produced the results you're looking for.
The first query I wrote was to get the "First Completed" dates. Since if a person only completes a course once, that is their first completion, this actually means your criteria 1 and 2 are identical.
I did this by grouping on the UserID and the CourseID and then taking the aggregate MIN of the CourseCompleteionDate where the Status is equal to 1.
SELECT tblRegistrations.UserID, tblRegistrations.CourseID, Min(tblRegistrations.CourseCompletionDate) AS FirstCompleted
FROM tblRegistrations
WHERE ((tblRegistrations.StatusID)=1)
GROUP BY tblRegistrations.UserID, tblRegistrations.CourseID;
The second query I wrote was to get the "Last Not Completed" registration dates. Again I grouped on the UserID and the CourseID, but this time I took the aggregate MAX of the CourseRegistrationDate to get the last time this course was attempted and not completed (with a status equal to 3)
SELECT tblRegistrations.UserID, tblRegistrations.CourseID, Max(tblRegistrations.CourseRegistrationDate) AS LastAttempt
FROM tblRegistrations
WHERE (((tblRegistrations.StatusID)=3))
GROUP BY tblRegistrations.UserID, tblRegistrations.CourseID;
The third query I wrote was to get the unique UserID/CourseID relationships, and that simply grouped them.
SELECT tblRegistrations.CourseID, tblRegistrations.UserID
FROM tblRegistrations
GROUP BY tblRegistrations.CourseID, tblRegistrations.UserID;
Finally, I wrote my result query which makes sure to pull every UserID/CourseID combination, and then show whether it was completed or not, and the requested date.
SELECT qryUserCourses.UserID, qryUserCourses.CourseID, Nz([FirstCompleted],[LastAttempt]) AS ResultDate, IIf([FirstCompleted],"Completed","Not Completed") AS Result
FROM (qryUserCourses
LEFT JOIN qryFirstCompleted ON (qryUserCourses.CourseID = qryFirstCompleted.CourseID) AND (qryUserCourses.UserID = qryFirstCompleted.UserID))
LEFT JOIN qryLastNotCompleted ON (qryUserCourses.CourseID = qryLastNotCompleted.CourseID) AND (qryUserCourses.UserID = qryLastNotCompleted.UserID);
This produced, for me, a list of UserIDs, CourseIDs, a date, and completion status.

Related

How to count unique occurences of string in table for separate records in apex 5

I am trying to automatically count the unique occurrences of a string saved in the table. Currently I have a count of a string but only when a user selects the string and it gives every record the same count value.
For example
Below is a image of my current table:
From the image you can see that there is a Requirement column and a count column. I have got it to the point were when the user would select a requirement record (each requirement record has a link) it would insert the requirement text into a requirement item called 'P33_REQUIREMENT' so the count can have a value to compare to.
This is the SQL that I have at current:
SELECT (SELECT COUNT(*)
FROM DIA_ASSOCIATED_QMS_DOCUMENTS
WHERE REQUIREMENT = :P33_REQUIREMENT
group by REQUIREMENT
) AS COUNT,
DPD.DIA_SELECTED,
DPD.Q_NUMBER_SELECTED,
DPD.SECTION_SELECTED,
DPD.ASSIGNED_TO_PERSON,
DAQD.REFERENCE,
DAQD.REQUIREMENT,
DAQD.PROGRESS,
DAQD.ACTION_DUE_DATE,
DAQD.COMPLETION_DATE,
DAQD.DIA_REF,
DA.DIA,
DA.ORG_RISK_SCORE
FROM DIA_PROPOSED_DETAIL DPD,
DIA_ASSOCIATED_QMS_DOCUMENTS DAQD,
DIA_ASSESSMENTS DA
WHERE DPD.DIA_SELECTED = DAQD.DIA_REF
AND DPD.DIA_SELECTED = DA.DIA
This is the sql used to make the table in the image.
This issue with this is, it is giving every record the same count when the user selects a requirement value. I can kind of fix this by also adding in AND DIA_SELECTED = :P33_DIA into the where clause of the count. DIA_SELECTED being the first column in the table and :P33_DIA being the item that stores the DIA ref number relating to the record chosen.
The output of this looks like:
As you can see there is only one count. Still doesn't fix the entire issue but a bit better.
So to sum up is there a way to have the count, count the occurrences individually and insert them in the requirements that are the same. So if there are three tests like in the images there would be a '3' in the count column where requirement = 'test', and if there is one record with 'test the system' there would be a '1' in the count column.
Also for more context I wont know what the user will input into the requirement so I can't compare to pre-determined strings.
I'm new to stack overflow I am hoping I have explained enough and its not too confusing.
The following extract:
SELECT (SELECT COUNT(*)
FROM DIA_ASSOCIATED_QMS_DOCUMENTS
WHERE REQUIREMENT = :P33_REQUIREMENT group by REQUIREMENT ) AS COUNT
Could be replaced by
SELECT (SELECT COUNT(*)
FROM DIA_ASSOCIATED_QMS_DOCUMENTS
WHERE REQUIREMENT = DAQD.REQUIREMENT ) AS COUNT
Which would give you - for each line, the number of requirements that are identical.
I'm not completely certain it is what you are after, but if it isn't, it should give you some ideas on how to progress (or allow you to indicate where I failed to understand your request)

Microsoft Access query to retrieve random transactions with seemingly impossible criteria

I was asked to assist with developing a report to retrieve a 25% sample of random transactions within a specific date range. I am not a programmer but I was able to devise the following fairly quickly:
SELECT TOP 25 PERCENT account.CID, account.ACCT, account.NAME, log.DATE, log.action_txt, log.field_nm, log.from_data, log.to_data, log.tran_id, log.init
FROM account INNER JOIN log ON account.ACCT = log.ACCT
GROUP BY account.CID, account.ACCT, account.NAME, log.DATE, log.action_txt, log.field_nm, log.from_data, log.to_data, log.tran_id, log.init
HAVING (((log.DATE) Between #2/7/2018# And #6/15/2018#) AND ((log.action_txt)="mod" Or (log.action_txt)="del") AND ((log.init)="J1X"
ORDER BY log.tran_dt
This returns 25% of the records within the date range. Each record row is unique but each account number potentially has multiple records on each day. In some cases the records have the same date and tran_id as well.
Upon further discussion with the requester, he actually wants to see all of the transactions for 25% of the accounts that have activity on each day within the date range. Thus if there were 100 accounts on 3/1/2018 with records in this table, he wants to see all of the transactions for 25 of those accounts; if there were 60 accounts on 3/2/2018 with records in this table, he wants to see all of the transactions for 15 of those accounts; and so on.
I was thinking that an Access module would work best in this scenario as I believe there are multiple parts to this. I figured that I need a function to loop through the date range and for each day:
1. Count the account numbers only one time
2. Return all of the transactions for 25% of the total accounts
But as I mentioned, I am not a programmer and I am exhausted from searching possible solutions for the many parts.
I think the key to your question is that you only really need a pseudo random selection of results for your report. So you can force the Random number generator to reorder your results based on a value in the record and the current time.
Something like this should work - I assume your actiontxt field is a text field and pull out the length of each field and apply that to current date/time to create a pseudo random number that can be sorted.
All I really do is change your ORDER BY line
See if this works for you
SELECT TOP 25 PERCENT
account.CID, account.ACCT, account.NAME, log.DATE, log.action_txt, log.field_nm, log.from_data,
log.to_data, log.tran_id, log.init
FROM account
INNER JOIN log ON account.ACCT = log.ACCT
GROUP BY account.CID, account.ACCT, account.NAME, log.DATE, log.action_txt, log.field_nm, log.from_data, log.to_data, log.tran_id, log.init
HAVING (((log.DATE) Between #2/7/2018# And #6/15/2018#) AND ((log.action_txt)="mod" Or (log.action_txt)="del") AND ((log.init)="J1X"
ORDER BY Rnd(CLng(Now()*Len(log.action_txt))-(Now()*Len(log.action_txt)));
Modified similar idea from other StackOverflow question and response

Ignore suppressed records in crosstab

My report shows only the latest diagnosis per patient based on their date_of_diagnosis - all other records are suppressed:
I summarize by diagnosis and age group in a crosstab. Crosstabs evaluate before printing, so any attempts to suppress, share variables, or summarize happen after the crosstab populates. This means Total in Each Age Group is correct, because each patient only has one age - but if a patient has more than one diagnosis, even if they're suppressed, they get counted multiple times:
I absolutely must use a crosstab for this due to the large number of diagnoses and age groups involved. How can I get the crosstab to ignore suppressed records? Or if I need to use a custom SQL Command table, how can I rewrite the existing SQL to ignore obsolete records?
Crystal's auto-generated SQL (through ODBC):
SELECT "Codes"."diagnosis_code",
"Codes"."diagnosis_value",
"Codes"."PATID",
"Codes"."FACILITY",
"Codes"."EPISODE_NUMBER",
"Record"."date_of_diagnosis"
FROM "SYSTEM"."Codes" "Codes",
"SYSTEM"."Entry" "Entry",
"SYSTEM"."Record" "Record"
WHERE "Codes"."DiagnosisEntry"="Entry"."ID" AND
"Codes"."EPISODE_NUMBER"="Entry"."EPISODE_NUMBER" AND
"Codes"."FACILITY"="Entry"."FACILITY" AND
"Codes"."PATID"="Entry"."PATID" AND
"Entry"."DiagnosisRecord"="Record"."ID" AND
"Entry"."EPISODE_NUMBER"="Record"."EPISODE_NUMBER" AND
"Entry"."FACILITY"="Record"."FACILITY" AND
"Entry"."PATID"="Record"."PATID"
You need only the latest diagnosis among a set of diagnoses. So I would suggest:
SELECT "Codes"."PATID",
"Codes"."diagnosis_code",
"Codes"."diagnosis_value",
"Codes"."FACILITY",
"Codes"."EPISODE_NUMBER",
"Record"."date_of_diagnosis"
FROM "SYSTEM"."Codes" "Codes",
"SYSTEM"."Entry" "Entry",
"SYSTEM"."Record" "Record"
WHERE "Codes"."DiagnosisEntry"="Entry"."ID" AND
"Codes"."EPISODE_NUMBER"="Entry"."EPISODE_NUMBER" AND
"Codes"."FACILITY"="Entry"."FACILITY" AND
"Codes"."PATID"="Entry"."PATID" AND
"Entry"."DiagnosisRecord"="Record"."ID" AND
"Entry"."EPISODE_NUMBER"="Record"."EPISODE_NUMBER" AND
"Entry"."FACILITY"="Record"."FACILITY" AND
"Entry"."PATID"="Record"."PATID"
AND "Entry"."date_of_diagnosis" = (SELECT MAX("date_of_diagnosis") FROM
"DiagonsisRecord" "A" WHERE "A"."DiagnosisRecord"="Entry"."DiagnosisRecord" )
This should get the maximum Date_of_Diagnosis for each patient and pass the filter parameter to get the last diagnosis of that patient.
Building off of Muffaddal Shakir's answer, I was able to write this query to perform the correct filter:
SELECT "Codes"."PATID",
"Codes"."diagnosis_code",
"Codes"."diagnosis_value",
"Codes"."FACILITY",
"Codes"."EPISODE_NUMBER",
"Record"."date_of_diagnosis"
FROM "SYSTEM"."codes" "Codes",
"SYSTEM"."entry" "Entry",
"SYSTEM"."record" "Record"
WHERE "Codes"."DiagnosisEntry"="Entry"."ID" AND
"Codes"."EPISODE_NUMBER"="Entry"."EPISODE_NUMBER" AND
"Codes"."FACILITY"="Entry"."FACILITY" AND
"Codes"."PATID"="Entry"."PATID" AND
"Entry"."DiagnosisRecord"="Record"."ID" AND
"Entry"."EPISODE_NUMBER"="Record"."EPISODE_NUMBER" AND
"Entry"."FACILITY"="Record"."FACILITY" AND
"Entry"."PATID"="Record"."PATID"
AND "Record"."date_of_diagnosis" = (
SELECT MAX("Record2"."date_of_diagnosis")
FROM "SYSTEM"."entry" "Entry2",
"SYSTEM"."record" "Record2"
WHERE "Entry2"."DiagnosisRecord"="Record2"."ID" AND
"Entry2"."EPISODE_NUMBER"="Record2"."EPISODE_NUMBER" AND
"Entry2"."FACILITY"="Record2"."FACILITY" AND
"Entry2"."PATID"="Record2"."PATID" AND
"Record"."PATID"="Record2"."PATID"
)
The key differences being:
The subquery uses unique aliases from the main query.
The last line "Record"."PATID"="Record2"."PATID" - Without this, the query only pulls back one diagnosis (the latest one in the whole system.) But now it checks for the latest diagnosis per person.

Access query, grouped sum of 2 columns where either column contains values

Another team has an Access database that they use to track call logs. It's very basic, really just a table with a few lookups, and they enter data directly in the datasheet view. They've asked me to assist with writing a report to sum up their calls by week and reason and I'm a bit stumped on this problem because I'm not an Access guy by any stretch.
The database consists of two core tables, one holding the call log entries (Calls) and one holding the lookup list of call reasons (ReasonsLookup). Relevant table structures are:
Calls
-----
ID (autonumber, PK)
DateLogged (datetime)
Reason (int, FK to ReasonLookup.ID)
Reason 2 (int, FK to ReasonLookup.ID)
ReasonLookup
------------
ID (autonumber PK)
Reason (text)
What they want is a report that looks like this:
WeekNum Reason Total
------- ---------- -----
10 Eligibility Request 24
10 Extension Request 43
10 Information Question 97
11 Eligibility Request 35
11 Information Question 154
... ... etc ...
My problem is that there are TWO columns in the Calls table, because they wanted to log a primary and secondary reason for receiving the call, i.e. someone calls for reason A and while on the phone also requests something under reason B. Every call will have a primary reason column value (Calls.Reason not null) but not necessarily a secondary reason column value (Calls.[Reason 2] is often null).
What they want is, for each WeekNum, a single (distinct) entry for each possible Reason, and a Total of how many times that Reason was used in either the Calls.Reason or Calls.[Reason 2] column for that week. So in the example above for Eligibility Request, they want to see one entry for Eligibility Request for the week and count every record in Calls that for that week that has Calls.Reason = Eligibility Request OR Calls.[Reason 2] = Eligibility Request.
What is the best way to approach a query that will display as shown above? Ideally this is a straight query, no VBA required. They are non-technical so the simpler and easier to maintain the better if possible.
Thanks in advance, any help much appreciated.
The "normal" approach would be to use a union all query as a subquery to create a set of weeks and reasons, however Access doesn't support this, but what you can do that should work is to first define a query to make the union and then use that query as a source for the "main" query.
So the first query would be
SELECT datepart("ww",datelogged) as week, Reason from calls
UNION ALL
SELECT datepart("ww",datelogged), [Reason 2] from calls;
Save this as UnionQuery and make another query mainQuery:
SELECT uq.week, rl.reason, Count(*) AS Total
FROM UnionQuery AS uq
INNER JOIN reasonlookup AS rl ON uq.reason = rl.id
GROUP BY uq.week, rl.reason;
You can use a Union query to append individual Group By Aggregate queries for both Reason and Reason 2:
SELECT DatePart("ww", Calls.DateLogged) As WeekNum, ReasonLookup.Reason,
Sum(Calls.ID) As [Total]
FROM Calls
INNER JOIN Calls.Reason = ReasonLookup.ID
GROUP BY DatePart("ww", Calls.DateLogged) As WeekNum, ReasonLookup.Reason;
UNION
SELECT DatePart("ww", Calls.DateLogged) As WeekNum, ReasonLookup.Reason,
Sum(Calls.ID) As [Total]
FROM Calls
INNER JOIN Calls.[Reason 2] = ReasonLookup.ID
GROUP BY DatePart("ww", Calls.DateLogged) As WeekNum, ReasonLookup.Reason;
DatePart() outputs the specific date's week number in the calendar year. Also, UNION as opposed to UNION ALL prevents duplicate rows from appearing.

SQL Selecting records where one date range doesn't intersect another

I'm trying to write a simple reservation program for a campground.
I have a table for campsites (one record for every site available at the campground).
I have a table for visitors which uses the campsite table's id as a foreign key, along with a check in date and check out date.
What I need to do is gather a potential check in and check out date from the user and then gather all the campsites that are NOT being used at any point in that range of dates.
I think I'm close to the solution but there's one piece I seem to be missing.
I'm using 2 queries.
1) Gather all the campsites that are occupied during that date range.
2) Gather all campsites that are not in query 1.
This is my first query:
SELECT Visitors.CampsiteID, Visitors.CheckInDate, Visitors.CheckOutDate
FROM Visitors
WHERE (((Visitors.CheckInDate)>=#CHECKINDATE#
And (Visitors.CheckInDate)<=#CHECKOUTDATE#)
Or ((Visitors.CheckOutDate)>=#CHECKINDATE#
And (Visitors.CheckOutDate)<=CHECKOUTDATE));
I think I'm missing something. If the #CHECKINDATE# and #CHECKOUTDATE# both occur between someone else's Check-in and Check-out dates, then this doesn't catch it.
I know I could split this between two queries, where one is dealing with just the #CHECKINDATE# and the second is dealing with the #CHECKOUTDATE#, but I figure there's a cleaner way to do this and I'm just not coming up with it.
This is my second one, which I think is fine the way it is:
SELECT DISTINCT Campsites.ID, qryCampS_NotAvailable.CampsiteID
FROM Campsites LEFT JOIN qryCampS_NotAvailable
ON Campsites.ID = qryCampS_NotAvailable.CampsiteID
WHERE (((qryCampS_NotAvailable.CampsiteID) Is Null));
Thanks,
Charles
To get records that overlap with the requested time period, use this simple logic. Two time periods overlap when one starts before the other ends and the other ends after the first starts:
SELECT v.CampsiteID, v.CheckInDate, v.CheckOutDate
FROM Visitors v
WHERE v.CheckInDate <= #CHECKOUTDATE# and
v.CheckOutDate >= #CHECKINDATE# ;