I have an optional parameter in my presto SQL query I tried many articles but none has worked for me.
I need something like this:
on c.portfolio_code = x.portfolio_code
/* this is a conditional parameter, this can be a null */
and c.portfolio_code = coalesce( '{p_portfolio_code}', x.portfolio_code)
when I try ISNULL( '{p_portfolio_code}', x.portfolio_code) its saying unrecognized function error
and (c.portfolio_code = ‘{p_portfolio_code}’ or ‘{p_portfolio_code}’ is null)
and (c.portfolio_code = ‘{p_portfolio_code}’ or ‘{p_portfolio_code}’ = ‘’)
Please help I tried many approaches but of no use.
and case '{p_portfolio_code}' when 'null' then 1=1 else Portfolio_Code = '{p_portfolio_code}' end
I tried the above and it resolved my issue, when no Parameter is passed, I checked it with 'null' and it worked.
Thanks Guru Stron for your help
Related
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
I used the following statement below:
Trim(IF FromDataSource.PID_VALID = 'Y' THEN FromDataSource.Person_ID ELSE #NULL)
Assuming you are using this code within a Transformer stage in DataStage
this will help
IF Trim(FromDataSource.PID_VALID) = 'Y' THEN Trim(FromDataSource.Person_ID) ELSE #NULL
Hint:
For you next question you might ask in this forum you should provide more details - do not let us guess. Also describe what you have tried and what error you got etc.
You can use case expression :
(CASE WHEN FromDataSource.PID_VALID = 'Y' THEN TRIM(FromDataSource.Person_ID) END)
else will return null if condition evaluate as false, you don't need to specify null.
If you want to set null if PID is not valid then->
IF FromDataSource.PID_VALID = 'Y' THEN trim(FromDataSource.Person_ID) ELSE setnull()
If you want to set empty when PID is not valid then->
IF FromDataSource.PID_VALID = 'Y' THEN trim(FromDataSource.Person_ID) ELSE ''
The following code I have written works find. Outputs what I expect.
select distinct
mi_tm.st_event_time,
mi_tm.st_location,
mi_tm.st_log_class,
mi_tm.st_qty,
mi_tm.st_reason,
mi_tm.st_reason_data,
mi_tm.st_sku_code,
mi_tm.st_ulid,
mi_tm.st_user_id,
x_prod.description,
x_supplier.supplier_name
from
mi_tm, x_prod,
x_igd, x_supplier
where
mi_tm.st_date >= {?Start} and
mi_tm.st_date <= {?End} and
mi_tm.st_sku_code = x_prod.prod_code (+) and
mi_tm.st_sku_code = x_igd.prod_code (+) and
x_igd.supplier_no = x_supplier.supplier_no (+) and
mi_tm.st_sku_code like '{?SKU}' and
mi_tm.st_log_class in ('TM_ADJUST_QTY', 'TM_CREATE', 'TM_DELETE')
However for this line
mi_tm.st_sku_code like '{?SKU}' and
I would like it to be able to expect a wildcard parameter, that will return all values run between the dates. Just querying a % seems to crash my report. and I have tried adding '%' around the parameter. But I either get an invalid number error, or SQL not ended properly. I would be grateful for any help on this issue.
Edit: Place this at the bottom
If {?SKU} = '*' then
mi_tm.st_sku_code like '*'
else mi_tm.st_sku_code like '{?SKU}'
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.
Alright, so I have a query that looks like this:
SELECT
`orders`.*,
GROUP_CONCAT(
CONCAT(
`menu_items`.`name`,
' ($',
FORMAT(`menu_items`.`price`,2),
')'
) SEPARATOR '<br>'
) as `items`,
SUM(`menu_items`.`price`) as `additional`,
`children`.`first_name`,
`children`.`last_name`,
`organizations`.`base_price`
FROM
`orders`, `order_items`, `menu_items`, `children`, `organizations`
WHERE
`order_items`.`menu_item_id` = `menu_items`.`id` AND
`order_items`.`order_id` = `orders`.`id` AND
`orders`.`added_by` = {$user_id} AND
`orders`.`date` > '{$cutoff}' AND
`children`.`id` = `orders`.`child_id` AND
`organizations`.`id` = `children`.`organization_id`
GROUP BY
`orders`.`id`
I know it's a monstrosity and that some people will die before not using explicit joins. Ignoring that, however, what I wish to do is to only use the CONCAT inside the GROUP_CONCAT if the menu_items.price is greater than 0, otherwise only return menu_items.name. I have had, however, no success trying to throw an IF in there. I've read the manual but all the ways that I've tried aren't working and I'm pretty sure I'm missing something on the whole conditional statements thing.
Have you tried using something like this?
CASE WHEN 'menu_items'.'price' = 0 THEN 'menu.items'.'name' ELSE CONCAT (etc) END
Replacing the CONCAT statement of course.
Something like this should work (but I didn't test it, sorry):
GROUP_CONCAT(
CONCAT(
`menu_items`.`name`,
IF(`menu_items`.`price` > 0, -- <condition>
CONCAT(' ($', FORMAT(`menu_items`.`price`,2), ')'), -- <true-expr>
'' -- <false-expr>
)
)
SEPARATOR '<br>'
) as `items`,
The IF() function is really simple:
IF( <condition>, <true-expr>, <false-expr> )
The function has three arguments: the first is <condition>. If the condition evaluates to true, the function returns the result of <true-expr>. Else the function returns the result of <false-expr>.
Things get harder to get right when you use really long, multi-line expressions that contain parentheses and commas and so on. You just have to do it carefully. I suggest starting with more simple expressions and then build up.