Converting Access Syntax into SQL Syntax - sql

I am trying to convert the below into from an Access Database Syntax to sql query syntax using Case-When-Then etc. Can someone help me?
SpreadType: IIf([mv_Loan_Ext1]![LoanPurposeTypeCode] In ("008","080"),"QRLF-" & IIf([FAReceivesPatronage]=0,"NPAT","PAT"),IIf([SecDistrictIndustryCode]="999","Non-QRLF",IIf([PriDistrictIndustryCode]="999","RHL",IIf([mv_Loan_Ext1]![LoanPurposeTypeCode]+0 Between 1 And 7,"Other home",IIf([FANoteIsParticipationPurchased]=1,IIf([mv_FANote]![BranchNbr]="092","100% GP","PP"),IIf([PatronagePoolCode]="14","FCE",IIf([YBSAgStart]<>"No" Or IIf(Trim([AgGrow_NoOperator]) Is Null,0,IIf(Trim([AgGrow_NoOperator])="" Or Trim([AgGrow_NoOperator])="No",0,1))+IIf(Trim([AgNiche]) Is Null,0,IIf(Trim([AgNiche])="" Or Trim([AgNiche])="No",0,1))+IIf(Trim([AgGrowOperator]) Is Null,0,IIf(Trim([AgGrowOperator])="" Or Trim([AgGrowOperator])="No",0,1))>0,"YBS AgStart",IIf([GrainInventoryLoan]="Yes","Grain Inventory",IIf([COVID19] Like "SBA*","PPP",IIf(IIf([FarmTypeAbbr]="",IIf([FCALoanTypeAbbr]="PRIT","FL",IIf([FCALoanTypeAbbr]="REMG","FL","")),[FarmTypeAbbr])<>"FL","General non-farmer","Core farmer loans"))))))))))

You have to rewrite your query like this. You just have too many embed (). This is little mess.
select
case when [mv_Loan_Ext1].[LoanPurposeTypeCode] in ('008', '080') then 'QRLF-' + case FAReceivesPatronage when 0 then 'NPAT' else 'PAT' end
when [SecDistrictIndustryCode] = '999' THEN 'Non-QRLF'
when ...
end

Related

SQL query using if else

My SQL code looks like this:
SELECT
Scores.PupilId, Scores.BoysName, Scores.FormGroup,
IF (Scores.FormGroup = "10SB", "Great", "ok")
FROM
Scores
I get this message
no such function: if: SELECT Scores.PupilId, Scores.BoysName, Scores.FormGroup,
if(Scores.FormGroup="10SB","Great","ok")
FROM Scores
This is flat file database
Can anyone please help me understand why I am getting a message?
The correct ANSI-standard conditional expression in SQL is the case expression:
SELECT Scores.PupilId, Scores.BoysName, Scores.FormGroup,
(CASE WHEN Scores.FormGroup = '10SB' THEN 'Great' ELSE 'ok' END)
FROM Scores ;

Getting this error - ORA-00936: missing expression

I am new to SQL and trying to use an if statement equivalent in order to get the value in SQL.
After running this report I am getting the error "ORA-00936: missing expression"
CASE WHEN (select Sum(MONTANT) from fraisExterne where matching=fm.id and statut=3) IS NULL
THEN select sum(MONTANT) from fraisExterne where matching=fm.id
ELSE Select sum (MONTANTHTBROKER) from fraismatching where matching=fm.id
End End as "BROKER AMOUNT"
Please let us know if you can help me with it
You have a extra END key word at the end of your case statement. You also need to put your select query into ().
Try this:
SELECT
CASE
WHEN (select Sum(MONTANT) from fraisExterne where matching=fm.id and statut=3) IS NULL THEN (select sum(MONTANT) from fraisExterne where matching=fm.id)
ELSE (Select sum (MONTANTHTBROKER) from fraismatching where matching=fm.id)
End AS BROKER_AMOUNT
I hope this helps and welcome to StackOverflow. If you find this answer or any other answer solves your problem please mark it as the solution. This will help the community and fellow programmers who run into the same problem in the future. Thanks.

Excel MS Query - How to write parameter equals "" or show all in SQL query

Just finished writing an SQL script in the MS-Query and I'm having difficulty trying to get it to work.
What I'm after is the equivalent of this SQL where clause:
AND ((examplefield = #Para) or (#Para = ''))
So if parameter = something in the field, only show that or if the parameter = blank then show all results.
So far this is what I have which works fine if I want to select a particular item, now I just need to include the additional if blank show all.
AND (`'Project Master List$'`.`Type of Work`= ?)
This unfortunately doesn't work.
AND ((`'Project Master List$'`.`Type of Work`= ?) OR (? = ""))
Any suggestions?
Try a case?
Case When ?="" THEN
'docode'
WHEN ?="OtherValue" THEN
'DoCode'
Else
'DoCode '
End
The IIF Example:
iif(?="",
iif(?="OtherValue",ReturnSomethingTrue,ReturnSomethingFalse)
,ReturnSomethingTrue,ReturnsomethingFalse)
I looked back at my original question and realised I wrapped the where clause in the wrong quotes. See below
Wrong statement
AND ((`'Project Master List$'`.`Type of Work`= ?) OR (? = ""))
Correct statement
AND (`'Project Master List$'`.`Type of Work`= ? OR ? = '')
Once I'd made the change right at the end of the clause it worked.

HiveQL : use SELECT in clause WHERE

Is there a way to do this in HiveQL :
SELECT ......
from
default.thm_renta_produits_jour rpj
WHERE
rpj.co_societe = '${hiveconf:in_co_societe}'
AND rpj.dt_jour >= (SELECT MIN(dt_jour) FROM default.calendrier WHERE co_an_semaine = '${hiveconf:in_co_an_sem}')
Because when i do this, i get this error :
FAILED: ParseException line 51:26 cannot recognize input near 'SELECT' 'MIN' '(' in expression specification
Thanks,
Hive does not support sub queries in where clause it supports sub queries in from clause only.
Hive does not support sub queries in the WHERE clause. Perhaps you can work around this by moving your sub query to a JOIN clause like so:
SELECT
rpj.*
FROM
default.thm_renta_produits_jour rpj
JOIN
( SELECT MIN(dt_jour) AS min_dt_jour
FROM default.calendrier
WHERE co_an_semaine = '${hiveconf:in_co_an_sem}'
) m
WHERE
rpj.co_societe = '${hiveconf:in_co_societe}'
AND rpj.dt_jour >= m.min_dt_jour;
Hope that helps.
I know that this is an old post, but the previous answers are now outdated. Newer versions of Hive (0.13+) support subqueries of where clauses, so your query should run.
https://cwiki.apache.org/confluence/display/Hive/LanguageManual+SubQueries#LanguageManualSubQueries-SubqueriesintheWHEREClause

Access to T-SQL query conversion - What to use in place of the IIf() function?

My query is as follows :
I have written in access can anyone pls help how can I convert it into sql query I am getting an error near IIF condition.
SELECT #NT_VAR_STEP_1_1.SYS_ID, #NT_VAR_STEP_1_1.NODE, #NT_VAR_STEP_1_1.TEMP_ID,
#NT_VAR_STEP_1_1.EQUIP_TYPE, #NT_VAR_STEP_1_1.EQ_ID,
#NT_VAR_STEP_1_1.VAR_ID, #NT_VAR_STEP_1_1.NODE AS VAR_SET,
#NT_VAR_STEP_1_1.VAR_NAME, IIf([#NT_VAR_STEP_1_1]![VAR_SUBSET]=‘SELF’,
[#NT_VAR_STEP_1_1]![NODE],[#NT_VAR_STEP_1_1]![VAR_SUBSET]) AS VAR_SUBSET,
IIf([#NT_VAR_STEP_1_1]![EQUIP_TYPE]=‘SOURCE’,’PARAMVAR’,’VAR’) AS CALC_VAR_TYPE,
#NT_VAR_STEP_1_1.VAR_DATA_TYPE AS DATA_TYPE, #NT_VAR_STEP_1_1.DOF,
#NT_VAR_STEP_1_1.RETAIN, #NT_VAR_STEP_1_1.COEFF_OBJECT,
#NT_VAR_STEP_1_1.COEFF_VAR_SET, #NT_VAR_STEP_1_1.COEFF_VAR_TYPE,
#NT_VAR_STEP_1_1.COEFF_VAR_SUBSET, #NT_VAR_STEP_1_1.COEFF_VAR_NAME,
#NT_VAR_STEP_1_1.OPERAND, #NT_VAR_STEP_1_1.SIGN,
#NT_VAR_STEP_1_1.VAR_TP_OFFSET, #NT_VAR_STEP_1_1.COEFF_TP_OFFSET,
#NT_VAR_STEP_1_1.COEFF_VAR_SUBTYE INTO #OT_VAR_STEP_1_1_1
FROM #NT_VAR_STEP_1_1
GROUP BY #NT_VAR_STEP_1_1.SYS_ID, #NT_VAR_STEP_1_1.TEMP_ID,
#NT_VAR_STEP_1_1.EQUIP_TYPE, #NT_VAR_STEP_1_1.EQ_ID,
#NT_VAR_STEP_1_1.VAR_ID, #NT_VAR_STEP_1_1.NODE,
#NT_VAR_STEP_1_1.VAR_NAME, IIf([#NT_VAR_STEP_1_1]![VAR_SUBSET]=‘SELF’,
[#NT_VAR_STEP_1_1]![NODE],[#NT_VAR_STEP_1_1]![VAR_SUBSET]),
IIf([#NT_VAR_STEP_1_1]![EQUIP_TYPE]=‘SOURCE’,’PARAMVAR’,’VAR’),
#NT_VAR_STEP_1_1.VAR_DATA_TYPE, #NT_VAR_STEP_1_1.DOF,
#NT_VAR_STEP_1_1.RETAIN, #NT_VAR_STEP_1_1.COEFF_OBJECT,
#NT_VAR_STEP_1_1.COEFF_VAR_SET, #NT_VAR_STEP_1_1.COEFF_VAR_TYPE,
#NT_VAR_STEP_1_1.COEFF_VAR_SUBSET, #NT_VAR_STEP_1_1.COEFF_VAR_NAME,
#NT_VAR_STEP_1_1.OPERAND, #NT_VAR_STEP_1_1.SIGN,
#NT_VAR_STEP_1_1.VAR_TP_OFFSET, #NT_VAR_STEP_1_1.COEFF_TP_OFFSET,
#NT_VAR_STEP_1_1.COEFF_VAR_SUBTYE, #NT_VAR_STEP_1_1.NODE
HAVING (((#NT_VAR_STEP_1_1.EQUIP_TYPE)<>‘COST_NODE’));
Convert this line:
IIf([#NT_VAR_STEP_1_1]![EQUIP_TYPE]=‘SOURCE’,’PARAMVAR’,’VAR’) AS CALC_VAR_TYPE
to this:
CASE WHEN [#NT_VAR_STEP_1_1].[EQUIP_TYPE]='SOURCE'
THEN 'PARAMVAR'
ELSE 'VAR'
END CALC_VAR_TYPE
And then use the same in your GROUP BY clause, but without the column alias:
CASE WHEN [#NT_VAR_STEP_1_1].[EQUIP_TYPE]='SOURCE'
THEN 'PARAMVAR'
ELSE 'VAR'
END
I will assume you're going to SQL Server (T-SQL). T-SQL does not support IIF, you will need to use CASE instead.