SQL WHERE clause searching for multiple parameters from one field - sql

Essentially I would like the MS Access query to output all records that meet all three of the following criteria:
Expanded_Status = "Eligible but not enrolled"
Hospital = "UHN"
Comments = "transplant" or "tx" or "post-transplant"
The comments field has 3 different versions of the word "transplant" so some fields may have "tx" instead of "transplant" and some records may have "post-transplant" instead of "transplant".
The query below is outputting records that don't match all three of the criteria for some reason.
Please help me modify the query so that I can find only records with all three criteria fulfilled
SELECT [First Name], [Last Name], [Subject ID], Expanded_Status, Hospital, Comments
FROM [Barriers UHN Screen - 2017 Mailing]
WHERE Expanded_Status = "Eligible but not enrolled" AND Hospital = "UHN" AND Comments LIKE "transplant" OR Comments OR "post transplant" OR Comments = "tx";

Building on #Harun24HR 's answer:
SELECT
[First Name], [Last Name], [Subject ID], Expanded_Status, Hospital, Comments
FROM
[Barriers UHN Screen - 2017 Mailing]
WHERE
Expanded_Status = "Eligible but not enrolled" AND Hospital = "UHN" AND
(LOWER(Comments) LIKE "*transplant*" OR LOWER(Comments) LIKE "*post transplant*" OR LOWER(Comments) LIKE "*tx*");
I added two things:
Calling lower on Comments makes sure you are only comparing lower cased string - if someone has "Post Transplant" in the comments, it might not match on "post transplant"
Wildcards * on the beginning and end of the comparison string - this tells Access my comparison string can be anywhere in the comments.
Like Operator

Zuva,
Try this
SELECT
[First Name], [Last Name], [Subject ID], Expanded_Status, Hospital, Comments
FROM
[Barriers UHN Screen - 2017 Mailing]
WHERE
Expanded_Status = "Eligible but not enrolled" AND Hospital = "UHN" AND
(Comments = "transplant" OR Comments = "post transplant" OR Comments = "tx");
Enclosing the Comment section of the where clause in brackets, tells access to evaluate each separately as part of the AND clause.

Related

How to add 2nd price column in SQL when info is normally stored on 2 separate rows

I currently have a very simple SQL script, which provides the sell price for a special customer of ours, who gets Priceline "J". I'd like to add a column of Retail pricing as well for them to reference, which is normally Priceline "R". My query currently looks like this:
SELECT RS_PCL.SKU,
RS_PCL.SKU_DESC,
RS_PCL.PACKAGE,
RS_PCL.PRICE AS [Sell Price],
RS_PCL.STD AS [Start Date],
RS_PCL.END AS [End Date]
FROM RS_PCL
WHERE RS_PCL.PRICELINE = "J"
Would anyone be able to help me figure out how I could add the "R" price line as another column instead of separate rows?
I can add WHERE RS_PCL.PRICELINE IN ("J","R") but it would create a separate row for each Retail price, instead of in the column next to it. I've seen examples of a separate SELECT CASE WHEN, but not sure exactly how the syntax works.
The prices are always going to have the same start date, so it would just need to join where the SKU matches and the Start Date matches.
NOTE : IM sorry I accidentally edited your answer, not my question...
Revised: Running into errors with this code still, saying "Your query does not include the specified expression 'GL Type' as part of an aggregate function" or would say each field that isn't included in the group by clause
SELECT RS_PCL.[GL Type],
RS_PCL.SKU,
RS_PCL.[SKU Desc],
RS_PCL.Supplier,
RS_PCL.[Case UPC],
RS_PCL.[Pack UPC],
RS_PCL.[Unit UPC],
RS_PCL.PDCN,
RS_PCL.Package,
RS_PCL.[Price Start Date],
RS_PCL.[Price End Date],
Iif( RS_PCL.PRICELINE = "J" , RS_PCL.Price , Null) AS [Sell Price],
Iif( RS_PCL.PRICELINE = "R" , RS_PCL.Price , Null) AS [Retail Price],
RS_PCL.Cost,
RS_PCL.Tax,
RS_PCL.Freight
FROM RS_PCL
WHERE (RS_PCL.PRICELINE IN ("J","R"))
Tested the Self Join query listed below, I keep getting a "Syntax Error in FROM Clause" with this query.
SELECT J.*, R.R_PRICE FROM RS_PCL AS J
INNER JOIN SELECT (RS_PCL.SKU, RS_PCL.[Price Start Date], RS_PCL.Price AS R_PRICE FROM RS_PCL WHERE RS_PCL.PRICELINE = "R") AS R
ON J.SKU = R.SKU AND J.[Price Start Date] = R.[Price Start Date]
WHERE RS_PCL.Priceline = "J"
SELECT CASE does not work in Access query. Use IIf() or Switch() or Choose().
Perhaps a CROSSTAB query or emulate CROSSTAB with expressions to create fields. Presuming, as indicated in question title, there is one "J" and one "R" record for each data combination:
SELECT SKU, SKU_DESC, PACKAGE, STD, END,
Max(IIf(PRICELINE = "J", PRICE, Null)) AS J,
Max(IIf(PRICELINE = "R", PRICE, Null)) AS R
FROM RS_PCL
GROUP BY SKU, SKU_DESC, PACKAGE, STD, END
Or a self-join, assuming only fields needed as unique identifier are SKU and STD:
SELECT J.*, R.R_PRICE FROM RS_PCL AS J
INNER JOIN (SELECT SKU, STD, PRICE AS R_PRICE FROM RS_PCL WHERE PRICELINE = "R") AS R
ON J.SKU = R.SKU AND J.STD = R.STD
WHERE PRICELINE = "J";
This would be useful on a report but useless for a data entry form.
Yet another approach would use DLookup() domain aggregate function. Domain aggregate function can cause slow performance in query or form but the dataset would be editable.

I'm having issues getting the NZ function working in a query

I'm trying to tie a description to group codes to use in another table. Unfortunately the data is from a very old database and didn't have very good requirements for fields when it was initially created. So now I have a number of fields for part number group codes that were blank. I'm trying to convert these null values to say "blank". I've tried this many different ways and cannot get the nz function to modify the data in any way.
In the provided code snippet I have tried using the nz function only after select, only after from, and in both places as shown.
SELECT [Part Numbers].Part, nz([Part Numbers].Group,"Blank"), [Group Codes].Description
FROM [Part Numbers]
INNER JOIN [Group Codes] ON nz([Part Numbers].[Group],"Blank") = [Group Codes].[Group Code];
That query doesn't return records where Group field is Null. It can't even be displayed in query design with that join.
Consider RIGHT JOIN:
SELECT [Part Numbers].Part, Nz([Part Numbers].Group,"Blank") AS Expr1, [Group Codes].Description
FROM [Group Codes] RIGHT JOIN [Part Numbers] ON [Group Codes].[Group Code] = [Part Numbers].Group;

How am I using the LIKE SQL operator incorrectly?

I am using VBA and trying to perform a SQL query to measure how many records exist where a certain column has a prefix of a given string. As I am debugging this process I am left very confused because it seems like there is very little room for error, but I'm still seeing strange behavior. SELECT * FROM [Part Sales Header] WHERE [Quote No] LIKE 'sm82520A' ; finds one record as I'd expect. However, if I change it to SELECT * FROM [Part Sales Header] WHERE [Quote No] LIKE 'sm82520%' ; 0 records are found. By my understanding % should find anything that comes after.
If it is useful, the rest of the VBA code that I am using looks like this:
sSQL = "SELECT * FROM [Part Sales Header] WHERE [Quote No] LIKE 'sm82520A' ;"
Set rst = CurrentDb.OpenRecordset(sSQL)
QuoteNoAmount = rst.RecordCount
If you're using MS Access, the wildcard is *, so it would be
sSQL = "SELECT * FROM [Part Sales Header] WHERE [Quote No] LIKE 'sm82520A*' ;"

VB.NET/Access - Calculate how many data entries/total attendance for each student

I have a table with the follow fields. StudentID, LastName, FirstName, TrainDate. I have windows form that I use to input the date of attendance in the TrainDate field. The TrainDate field is short text. My goal is to count how many TrainDate entries each person has and display it in a datagridview.
This is the query I am working on but I get the following error: "Date type mismatch in criteria expression"
Access.ExecQuery("SELECT FirstName AS [FIRST NAME], LastName AS [LAST NAME],
SUM(TrainDate) AS [TOTAL CLASSES]
FROM ACJATTENDANCE
GROUP BY FirstName, LastName, TrainDate;")
' REPORT & ABORT ON ERRORS
If NoErrors(True) = False Then End
' FILL DATAGRID
dgvAttend.DataSource = Access.DBDT
Can someone help me figure out the reason for the error? I have used this query to calculate wins and losses in another part of the program successfully. I'm not sure but I think this has something to do with TrainDate.
You are counting attendances so you probably want to use the COUNT function instead of SUM
Don't forget to remove TrainDate from GROUP BY selection, otherwise you will not get the result you want.
SELECT FirstName AS [FIRST NAME], LastName AS [LAST NAME],
COUNT(TrainDate) AS [TOTAL CLASSES]
FROM ACJATTENDANCE
GROUP BY FirstName, LastName ;
The issue was my date field in my database table was set to short text instead of Date\Time.

MS Access: Ignoring query criteria if blank

I have a form in Access where I run a query based on several text boxes. I apply criteria on several of the query fields that is pulled from the text boxes but would like the query to ignore the criteria when the text box is blank.
For example, if the Machine_TextBox is blank, do not apply criteria to the Events.Machine field.
SQL code is:
SELECT Events.Machine, Events.[Event Date], Events.[Event Description],
Events.[Action Taken], Events.[Machine Clinical], Events.[Modalities Not Clinical],
Events.[Manufacturer Ticket #], Events.[TLC Ticket #], Events.FSR, Events.ID,
Events.[Event Recorded By], Events.[Action Recorded By], Events.[Downtime Validation],
Events.[Event Time]
FROM Events
WHERE (((Events.Machine)=IIf([Forms]![SearchEvent]![Machine_TextBox] Is Null,"",
[Forms]![SearchEvent]![Machine_TextBox])) AND ((Events.[Event Date]) Between
Nz([Forms]![SearchEvent]![StartDate_TextBox],#1/1/1900#) And Nz([Forms]![SearchEvent]![EndDate_TextBox],#1/1/2100#))
AND ((Events.[Event Description]) Like "*" & [Forms]![SearchEvent]![EventDetails_TextBox])
AND ((Events.[Manufacturer Ticket #])=[Forms]![SearchEvent]![Manufacturer_TextBox])
AND ((Events.[TLC Ticket #])=[Forms]![SearchEvent]![TLC_TextBox])
AND ((Events.FSR)=[Forms]![SearchEvent]![FSR_TextBox]))
OR (((Events.Machine)=IIf([Forms]![SearchEvent]![Machine_TextBox] Is Null,"",[Forms]![SearchEvent]![Machine_TextBox]))
AND ((Events.[Event Date]) Between Nz([Forms]![SearchEvent]![StartDate_TextBox],#1/1/1900#)
AND Nz([Forms]![SearchEvent]![EndDate_TextBox],#1/1/2100#))
AND ((Events.[Action Taken]) Like "*" & [Forms]![SearchEvent]![EventDetails_TextBox])
AND ((Events.[Manufacturer Ticket #])=[Forms]![SearchEvent]![Manufacturer_TextBox])
AND ((Events.[TLC Ticket #])=[Forms]![SearchEvent]![TLC_TextBox])
AND ((Events.FSR)=[Forms]![SearchEvent]![FSR_TextBox]))
ORDER BY Events.[Date and Time Stamp] DESC;
Yours sincerely,
Mark
You can try the technique described here.
For each search box, use boolean logic to either filter for its value, or ignore this AND clause if it's empty, by making the AND clause TRUE.
I'll just use two search boxes as example:
SELECT stuff
FROM Events
WHERE ((Events.Machine = [Forms]![SearchEvent]![Machine_TextBox])
OR ([Forms]![SearchEvent]![Machine_TextBox] Is Null))
AND ((Events.[Event Description] Like "*" & [Forms]![SearchEvent]![EventDetails_TextBox] & "*")
OR ([Forms]![SearchEvent]![EventDetails_TextBox] Is Null))
AND ...