Conditional Query Expression - sql

I have the below SQL in Access Query. But I need to add a condition to the Expression "Lasted".
SELECT ErrorLog.Reported_Date, ErrorLog.Details, ErrorLog.Started_At, ErrorLog.Ended_At, ErrorLog.Project_Name, ([ended_at]-[started_at]) AS Lasted, ErrorLog.Not_Count_For_Pro_Time
FROM ErrorLog
GROUP BY ErrorLog.Reported_Date, ErrorLog.Details, ErrorLog.Started_At, ErrorLog.Ended_At, ErrorLog.Project_Name, ([ended_at]-[started_at]), ErrorLog.Not_Count_For_Pro_Time;
This query is used as a Record Source for a report. As it stands now, the expression contains the value of " ([ended_at]-[started_at])". But I need to find a way to show 0 for "lasted", when "Not_Count_For_Pro_Time" is checked/True.
I have tried this code below, but it only shows the records where "Not_Count_for_Pro_Time" is false. I want the report to show all the records regardless if the "Not_Count_For_Pro_Time" is true or false, but the Expression "Lasted" should be adjusted as indicated earlier.
SELECT ErrorLog.Reported_Date, ErrorLog.Details, ErrorLog.Started_At, ErrorLog.Ended_At, ErrorLog.Project_Name, ([ended_at]-[started_at]) AS Lasted, ErrorLog.Not_Count_For_Pro_Time
FROM ErrorLog
GROUP BY ErrorLog.Reported_Date, ErrorLog.Details, ErrorLog.Started_At, ErrorLog.Ended_At, ErrorLog.Project_Name, ([ended_at]-[started_at]), ErrorLog.Not_Count_For_Pro_Time
HAVING (((ErrorLog.Not_Count_For_Pro_Time)=False));

Use conditional logic:
IIF(ErrorLog.Not_Count_For_Pro_Time = 'true', 0, [ended_at]-[started_at]) AS Lasted,

Related

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

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.

Add conditions where with 2 field conditions do not run accurately

The data that appears does not match the conditions that have been applied
I implemented SQL code in the Navicat application, and have changed the structure of the code several times but it still doesn't work,
data that is not of ilart condition type
still appears
SELECT SERMAT,ILART,sum(GKSTP) as jumlah
FROM swift_zab_iw39
WHERE ILART='OVH' OR ILART='TST' and SERMAT='024147-000:09052'
GROUP BY ILART,SERMAT
Use IN operator:
WHERE ILART IN('OVH','TST') AND SERMAT = '024147-000:09052'
Add parenthesis to the OR condition in the WHERE clause as:
WHERE (ILART = 'OVH' OR ILART = 'TST') AND SERMAT = '024147-000:09052'

IIF statement with multiple conditions

Consent:
IIf([consents].[decision]=1,"grant",
IIf([consents].[allowbreaktheglass]=0,"deny""Default"),
IIf([consents].[AllowBreakTheGlass]=1 AND [consents].[Decision]=0,"emergency only"))
IIF statement is returning an error:
The expression you entered has a function containing the wrong number of arguments
Criteria for consents are Grant, Deny and Emergency only.
Syntax for IIF statement is: IIf ( expr , truepart , falsepart )
You are missing falsepart in following 2:
Try Changing: IIf([consents].[allowbreaktheglass]=0,"deny""Default")
to: IIf([consents].[allowbreaktheglass]=0,"deny","Default")
and: IIf([consents].[AllowBreakTheGlass]=1 AND [consents].[Decision]=0,"emergency only")
to: IIf([consents].[AllowBreakTheGlass]=1 AND [consents].[Decision]=0,"emergency only","")
for more info visit: https://support.office.com/en-us/article/iif-function-32436ecf-c629-48a3-9900-647539c764e3
It's not 100% clear what you are trying to accomplish with your Nested IIF statement, however you are simply missing one failure argument, the following may be what you want:
Added 'Error?' for the last failure argument, as well as moved the 'Default' into the failure of the first clause.
Consent:
IIF([consents].[decision] = 1, 'grant',
IIF([consents].[allowbreaktheglass] = 0, 'deny',
IIF([consents].[AllowBreakTheGlass] = 1 AND [consents].[Decision] = 0,
'emergency only', 'Error?')) 'Default')
See whether you find Switch easier than nested IIf statements.
Open a new query in the Access query designer, switch to SQL View, and paste in this statement ...
SELECT
Switch(
c.[decision]=1, 'grant',
c.[allowbreaktheglass]=0, 'deny',
c.[AllowBreakTheGlass]=1 AND c.[Decision]=0, 'emergency only',
True, 'Default'
) AS Consent
FROM consents AS c;
Switch operates on expression/value pairs. It returns the value from the first pair whose expression evaluates as True, and ignores the remaining pairs regardless of whether or not they would be True.
Not sure I understood the logic for what you wanted, but that first pair would return 'grant' if [decision]=1. If not, it would examine the next pair. And so on. The last pair has True as its expression, so when none of the first 3 pairs are matched, the function will return 'Default'.

How do I use a Parameter form to select whether or not I want null values?

I am working on a database that my school uses for student recruitment. I want to include an option on the parameter form used to generate a students contact information that allows the user to select whether or not to include "applied" students in that contact list. I am currently trying to do that by using a select query that pulls data based on whether or not the "IdNumber" field is null. When I run the attached SQL, it doesn't pull any data at all. Any help would be appreciated!
SELECT InterestCards.NotStudentID, InterestCards.Email, InterestCards.YearOfGraduation,
InterestCards.SourceTwoLocation, InterestCards.FirstName, InterestCards.LastName,
InterestCards.Program, InterestCards.Inactive, InterestCards.IDNumber
FROM InterestCards
WHERE (((InterestCards.Email)<>"no email")
AND ((InterestCards.YearOfGraduation) Like [Forms]![MassCommunicationTool]![GradYear] & "*")
AND ((InterestCards.SourceTwoLocation) Like [Forms]![MassCommunicationTool]![FairName] & "*")
AND ((InterestCards.Inactive)=No)
AND ((InterestCards.IDNumber)=Switch([Forms]![MassCommunicationTool]![IncludeApplied]=0,Null,[forms]![MassCommunicationTool]![IncludeApplied]=(-1),([InterestCards].[IDNumber]) Is Not Null Or ([InterestCards].[IDNumber]) Is Null))
AND ((InterestCards.HighSchool) Like [Forms]![MassCommunicationTool]![HighSchool] & "*")
AND ((InterestCards.SourceThreeType) Like [Forms]![MassCommunicationTool]![SelectTerm] & "*")
AND ((InterestCards.DateEntered) Between Nz([Forms]![MassCommunicationTool]![StartDate],#1/1/1900#) And Nz([Forms]![MassCommunicationTool]![EndDate],#1/1/2199#)));
Operators cannot be dynamic in a query object. Cannot be selected via a conditional expression. However, parameters can be dynamic. Suggest you construct a field with an expression that handles the nulls to return either of 2 values:
IIf([IDNumber] Is Null, "XXXX", "AAAA") AS Grp
Then to select either the Null or not Null records, apply conditional parameter to that field:
Grp = IIf([Forms]![MassCommunicationTool]![IncludeApplied]=0, "XXXX", "AAAA")
I've never used dynamic parameterized queries. I prefer VBA to construct filter criteria and apply to form or report.

Crystal reports - How to translate a group by with condition into a report

I have the following query
SELECT dbo_DIRIGEANTS.IU_DIR,
[1-CR-liste_Pixi].Siren,
[1-CR-liste_Pixi].Nic,
FROM [1-CR-liste_Pixi] LEFT JOIN dbo_DIRIGEANTS ON [1-CR-liste_Pixi].Siren = dbo_DIRIGEANTS.SIREN
GROUP BY
dbo_DIRIGEANTS.IU_DIR,
[1-CR-liste_Pixi].Siren,
[1-CR-liste_Pixi].Nic,
IIf([QUAL_JUR]>"0003","",Right([QUAL_JUR],1))
HAVING
((([1-CR-liste_Pixi].Siren) Not Like "p*")
AND (([1-CR-liste_Pixi].Nic) Not Like "p*")
AND ((dbo_DIRIGEANTS.REP_LEGAL)="1") AND (([1-CR-liste_Pixi].Nicsiège) Not Like "p*"))
ORDER BY IIf([QUAL_JUR]>"0003","",Right([QUAL_JUR],1));
What I'm interested is setting that part
GROUP BY dbo_DIRIGEANTS.IU_DIR,
[1-CR-liste_Pixi].Siren,
[1-CR-liste_Pixi].Nic,
IIf([QUAL_JUR]>"0003","",Right([QUAL_JUR],1))
And especially that part
IIf([QUAL_JUR]>"0003","",Right([QUAL_JUR],1))
Because of performance, I kept my tables and use CR's functions to deal with the condition part instead of creating a query.
I did the following:
Went to Formula selection and choose group
Chose the field I wanted
Put the formula
The error below popped out
It said my formula needs to be using boolean values.
To see if I was right, I've amended it that way IF {DIRIGEANTS.QUAL_JUR} > "0003" THEN TRUE ELSE FALSE. It worked.
TL;DR
Why I cannot do that in my expert group selection?
IF {DIRIGEANTS.QUAL_JUR} > "0003" THEN "" ELSE RIGHT({QUAL_JUR},1)
Thanks
Group selection should return Boolean but that doesn't mean you write true or false....it means you pass some thing to fields to perform job
IF {DIRIGEANTS.QUAL_JUR} > "0003"
THEN datasefield=""
ELSE database fled=RIGHT({QUAL_JUR},1)
This will do the job
Edit: Apologize as conditions are a bit confused for your requirement
Create formula and write below code
IF {DIRIGEANTS.QUAL_JUR} > "0003" THEN ""
ELSE RIGHT({DIRIGEANTS.QUAL_JUR},1)
Now go to insert group and select this formula and click ok. This will do the trick