Why does this access query work and the other does not? - sql

I have a query that filters based on a boolean value on a form (Task Archived). The boolean value is stored as a long in the underlying table (only 0 and -1 are allowed).
I wanted to write a query that would either show only those records that are false (records not archived) or all records.
I would have expected the follwing query to work, but it does not... it returns only those records for which the archive field is set to True (-1):
SELECT tbl_Tasks.TaskID, tbl_Tasks.TArchive
FROM tbl_Tasks
WHERE (((tbl_Tasks.TArchive)=
IIf([Forms]![frm_Activity]![txtArchivedTaskDisplay]=0,False,
(tbl_Tasks.TArchive)=0 Or (tbl_Tasks.TArchive)=-1)));
The query below, which I would expect to show only those records that are True, in fact returns all records:
SELECT tbl_Tasks.TaskID, tbl_Tasks.TArchive
FROM tbl_Tasks
WHERE (((tbl_Tasks.TArchive)=
IIf([Forms]![frm_Activity]![txtArchivedTaskDisplay]=0,False,
(tbl_Tasks.TArchive)<>0)));
Why is this? What logic is access follwing here?

You cannot put SQL where condition within IIF. Anything within IIF will be evaluated if possible. Since you only have 0 or -1 below evaluation will always be true.
((tbl_Tasks.TArchive)=0 Or (tbl_Tasks.TArchive)=-1) => true
that's why you are only seeing TArchive = true
You could try something like this for your first query:
WHERE
tbl_Tasks.TArchive **<** IIf(displayArchivedOnly,0,1);
for the second one:
WHERE
tbl_Tasks.TArchive = [Forms]![frm_Activity]![txtArchivedTaskDisplay]
assuming your txtArchivedTaskDisplay holds either true or false value like a toggle button. but again, read more about how iif works.

Related

IIF statement to filter Access Query with checkbox

I'm working on a search form based off a single table. I'm working primarily in the query design view. I'd like to have a checkbox on the form so if it is checked (true) it will only return records where the setID is Null. If unchecked (false) it will return all records regardless of if there is a value in setID or not. Looking for a bit of help writing the iif statement (I'm very, very new to this).
source table: Inventory
field: setID
form: frmSearchInventory
form control: ckExcludeSet
iif(Forms!frmSearchInventory!ckExcludeSets = true, Inventory.SetID is Null, Inventory.SetID is not Null)
Close? Also, in the query design view, do I need anything additional in the criteria row? Many thanks!
For a dynamic query, calculate a field that returns SetID or a value in lieu of null: Nz(SetID, "N")
Then criteria under that calculated field:
LIKE IIf(Forms!frmSearchInventory!ckExcludeSets, "N", "*")
An unbound checkbox can be set for triple state. Make sure yours allows only True or False, never Null - set TripleState property to No. Set DefaultValue property to either True or False.

Filter on SQL As varialble

I have following query. How can I use the SQL variable is_followed (which is true or false) in where clause ?
Or How can I only get those records where subquery is_followed is true.
scoped = User.select(<<-SQL
DISTINCT(users.*),
EXISTS(
SELECT 1 FROM followers
WHERE
followers.follower_id=#{current_user.id} AND
followers.active=true
) AS is_following,
SQL
.squish)
I wanted to use the is_followed variable further in the next queries. Something like as follow
scoped.where(is_following: true)
I know I can do scoped.select(&:is_following) but I wanted to use it in SQL
Regards

Oracle: updating column IF ONLY it is not in condition of Merge statement

I'm having trouble with this Oracle Merge query.
merge into NEW_DOCTORS NP
USING OLD_DOCTORS MD
on (MD.SSN=NP.SSN OR MD.NPI=NP.NPI OR MD.PIN=NP.PIN)
WHEN MATCHED THEN
UPDATE SET
NP.othervalues=MD.othervalues
NP.PIN/SSN/NPI=MD.PIN/SSN/NPI (**update two of these three that did not return TRUE in the above ON condition)
WHEN NOT MATCHED THEN
insert(NP.SSN,NP.NPI,NP.PIN,NP.other_values)
values(MD.SSN,MD.NPI,MD.PIN,MD.other_values)
Is this possible? Thanks!
EDIT: I'm asking because I read somewhere that fields that are in the ON condition can't be updated. However I'm not sure if the author was talking only in context of the field that evaluates true or for all fields. I'm hoping there's someone who might have an idea about this, as well as any workarounds.
You can use CASE WHEN ... :
UPDATE SET
NP.othervalues=MD.othervalues
,NP.PIN = CASE WHEN (MD.SSN=NP.SSN OR MD.NPI=NP.NPI) THEN MD.PIN ELSE NP.PIN END
,--add here the 2 others
This means, when you have MD.SSN=NP.SSN OR MD.NPI=NP.NPI true, then update NP.PIN with MD.PIN else let the same value NP.PIN
Or you can do 3 differents MERGE, so it will be more readable.
EDIT
Thinking about it, if you update only when this is not the same value (cause it will join only on same value) you can directly update them with the MD table value.
Do that :
NP.othervalues=MD.othervalues
,NP.PIN=MD.PIN
,NP.SSN=MD.SSN
,NP.NPI=MD.NPI
If they match, the update will keep same value, if not it will update the value.

Update Table in Access

In MS-Access 2007, I have a table, [Test_Master] where I have a field [DT_REPORT]. I want to update [Test_Norm_Due] by 2 months if field [Size] = "small". If the field "Size" = "Med." then by 3 months. I create below query but it is throwing Syntax error. Can someone help.
UPDATE Test_Master
SET Test_Master.Test_Norm_Due =
IIF((([Test_Master]![Size]="small")), DateAdd(("m",2,[Test_Master]![DT_REPORT]))),
IIF((([Test_Master]![Size]="med.")), DateAdd(("m",3,[Test_Master]![DT_REPORT])));
I believe you have a problem with your parentheses - try nesting them using an external Text editor (like notepad++) for greater visibility - also, you are using extra parentheses that are getting in your way, try simplifying; and you're missing one final condition - what should happen with Test_Norm_Due when Size is neither "small" nor "med."
Note that syntax for IIF is:
IIF (condition, value if true, value if false).
You are nesting IIFs, so you should have something like:
IIF (condition, value if true, IIF(other condition, value if true, value if false))
Try something like this (I broke it in multiple lines just to try to make it more visible for you).
UPDATE Test_Master SET Test_Master.Test_Norm_Due =
IIF (([Test_Master]![Size]="small"),
DateAdd("m",2,[Test_Master]![DT_REPORT]),
IIF (([Test_Master]![Size]="med."),
DateAdd("m",3,[Test_Master]![DT_REPORT]),
{missing value - What happens if it's neither "small" nor "med."} ));

IIf function inside Access query

I have a query and I would like to use an IIf function as part of the criteria. Here is the full SQL:
SELECT Hits.HitID, Hits.ListingID, Hits.HitCount, Hits.HitDate, Hits.HitTypeID, Hits.IsDeleted
FROM Hits
WHERE (((Hits.HitDate)>=[Forms]![frmReports]![txtStartDate]) AND ((Hits.IsDeleted)="N"));
Here is the piece of code that causing me anguish:
>=[Forms]![frmReports]![txtStartDate]
If I have a date on frmReports, this will work fine; however, if no date is entered, this returns 0 records (I want it to return ALL records, if this is the case).
How can I make this work?
Try this:
SELECT Hits.HitID, Hits.ListingID, Hits.HitCount, Hits.HitDate, Hits.HitTypeID, Hits.IsDeleted
FROM Hits
WHERE (((Hits.HitDate)>=nz([Forms]![frmReports]![txtStartDate],"1/1/1")) AND ((Hits.IsDeleted)="N"));
or this
SELECT Hits.HitID, Hits.ListingID, Hits.HitCount, Hits.HitDate, Hits.HitTypeID, Hits.IsDeleted
FROM Hits
WHERE (((Hits.HitDate)>=[Forms]![frmReports]![txtStartDate]) AND ((Hits.IsDeleted)="N"))
OR (([Forms]![frmReports]![txtStartDate] = "") AND (Hits.IsDeleted="N"));