Translating a nested if statement into a set analysis expression - qlikview

I'm trying to translate this nested if statement into set analysis expression.
=Sum(if(Project=96,WorkTime*39.273,if(Project=92 and WorkType<>65,WorkTime*32.426,WorkTime*42.251)))
This is what I've done
=Sum({$<Project={96}>}WorkTime*39.273) or Sum({$<Project = {96}, WorkType -={65}>} WorkTime*32.426) or Sum({$<Project = {96}>} WorkTime*42.251)
I get an error near WorkType -= {65}, the problem is the operator. I've tried it even with <>, still an error.

If you just want to ignore some filters you can use your if expression combined with set analysis to ignore those fields like:
=Sum({<Field1_to_ignore = , Field2_to_ignore = >}if(Project=96,WorkTime*39.273,if(Project=92 and WorkType<>65,WorkTime*32.426,WorkTime*42.251)))

Related

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'

Syntax error in CASE expression, unable to rectify

I want below code in my where condition:
Condition statement is like :
ONE DAY PRIOR TO RENEWAL,
Policy has the Newly Independent Discount (RAP16.DSCCD = '366' exists)
AND
AT RENEWAL,
The Newly Independent discount drops (RAP16.DSCCD = '366' dropped)
I tried below code
AND case when tt.ja2_effdt_t = rap01.hmedt_t - 1
then trim(RAP16.DSCCD) = '366'
when trim(rap01.hmedt_t) <> tt.ja2_effdt_t)
then trim(RAP16.DSCCD) <> '366'
end
There is some syntax error in below code, But I am unable to rectify it.
It's generally better to use AND/OR constructions instead of case expressions in the WHERE clause. You probalby want something like:
AND ((tt.ja2_effdt_t = rap01.hmedt_t - 1 AND trim(RAP16.DSCCD) = '366')
AND
(trim(rap01.hmedt_t) <> tt.ja2_effdt_t AND trim(RAP16.DSCCD) <> '366'))

SQL Hive subquery error

I have the query below
set hive.cli.print.header=true;
set hive.query.max.partition=1000;
set hive.mapred.mode=unstrict;
SELECT
dim_lookup("accounts",name,"account_id") = '28016' as company,
dim_lookup("campaigns",name,"campaign_id") in (117649,112311,112319,112313,107799,110743,112559,112557,105191,105231,107377,108675,106587,107325,110671,107329,107181,106565,105123,106569,106579,110835,105127,105243,107185,105211,105215) as campaign_name,
case when is_click_through=0 then "PV" else "PC" end as conv_type,
(SELECT COUNT(1) FROM impressions WHERE ad_info[2] in (117649,112311,112319,112313,107799,110743,112559,112557,105191,105231,107377,108675,106587,107325,110671,107329,107181,106565,105123,106569,106579,110835,105127,105243,107185,105211,105215)) AS impressions
FROM actions
WHERE
data_date>='20170101'
AND data_date<='20171231'
AND conversion_action_id in (20769223,20769214,20769219,20764929,20764932,20764935,20769215,20769216,20764919,20769218,20769217,20769220,20769222)
GROUP BY conv_type
When I execute it I get an error
ERROR ql.Driver: FAILED: ParseException line 8:1 cannot recognize input near 'SELECT' 'COUNT' '(' in expression specification
I am trying to fetch each count of impression for a specified conversion_action_id. What could be the error in my query? Thanks for the help.
FYI: ad_info[2] and campaign_id are the same.
The problem is quite clear, you have a subquery inside your SELECT.
That is not how this works.
Unfortunately the exact solution is not that clear, as it I am not completely sure what you want, but here is some general advice:
Write your subquery, test it and make sure it is ok
Rather than putting it in your SELECT part, put it in your FROM part, and (as always) SELECt from the FROM
Just think of your subquery output as an other table that can be used in the from statement, and which needs to be combined (JOIN, UNION?) with other tables in the from statement.

Converting sql case expression

I'm trying to take the following sql case statement and convert it to Access 2010 as a calculation for a column. I've looked at the IIF statements but have received errors trying to sort it out. Thank you for any help.
case
when left(Tiers,4) = 'Tier' and isnumeric(right((left(Tiers,7)),2)) = 1 then right((left(Tiers,7)),2)
when left(Tiers,4) = 'Tier' and isnumeric(right((left(Tiers,7)),2)) = 0 then right((left(Tiers,6)),1)
else Tiers
end
;
you can only use Case statements in access in VBA Code.
and iif should work as well in access
like this
iif(left(Tiers,4) ='Tire',iif(isnumeric(right((left(Tiers,7)),2)) = 1,right((left(Tiers,7)),2),right((left(Tiers,6)),1)), Tires)
You can use IIF() as you mentioned and it will likely be a pain. There is also SWITCH function available which will likely make it easier for you to convert your case statement instead of nested IIF()s
Switch( expr-1, value-1 [, expr-2, value-2 ] … [, expr-n, value-n ] )
https://support.office.com/en-us/article/Switch-Function-d750c10d-0c8e-444c-9e63-f47504f9e379
This is a duplicate question of:
Case expressions in Access.

ORA-00933 using case/when inside a select

I'm trying to write this code into the toad, but it throws an ORA-00933 (SQL command not properly ended), just at the first WHEN.
I don't know exactly what happens. I've tried to follow the ORACLE docs, but with no success. Any help?
I let you the code written with toad.
select template.seq_temp, rules.fec_desde_serv, rules.seq_rec, rules.seq_agencia, rules.SEQ_TTOO, rules.COD_INTERFACE, pais.COD_PAIS, rules.cod_idioma
from re_t_vp_voucher_template template, re_t_vp_voucher_rules rules, re_t_vp_voucher_rules_pais pais,
case &tipser
when 'TRF' then re_t_vp_voucher_rules_trf trf
when 'ACC' then re_t_vp_voucher_rules_acc acct
else re_t_vp_voucher_rules_otro otro
end
where template.seq_temp = rules.seq_temp
and RULES.SEQ_RULE = PAIS.SEQ_RULE
group by template.seq_temp, rules.fec_desde_serv, rules.seq_rec, rules.seq_agencia, rules.SEQ_TTOO, rules.COD_INTERFACE, pais.COD_PAIS, rules.cod_idioma
order by template.seq_temp, rules.fec_desde_serv;
You cannot use CASE construct in FROM clause.
So your SQL as highlighted below is incorrect.
from re_t_vp_voucher_template template, re_t_vp_voucher_rules rules, re_t_vp_voucher_rules_pais pais,
case &tipser
when 'TRF' then re_t_vp_voucher_rules_trf trf
when 'ACC' then re_t_vp_voucher_rules_acc acct
else re_t_vp_voucher_rules_otro otro
end
where template.seq_temp = rules.seq_temp
The reason is that, you are trying to use dynamic table names in SQL, which is NOT allowed. You can't use dynamic table names in SQL unless you write dynamic SQL statements (i.e. build up the statement as a string in PL/SQL and then execute it using EXECUTE IMMEDIATE or the DBMS_SQL package).
So rewrite the query to meet the SQL standards, or (ab)use dynamic sql.
CASE is an expression, which works like IF-THEN-ELSE logic, and Oracle
uses short-circuit evaluation. so, it always needs a comparison
expression. Which you cannot have in FROM clause.
Your where condition is wrong.:-
where template.seq_temp = rules.seq_temp
and RULES.SEQ_RULE = PAIS.SEQ_RULE
case &tipser
when 'TRF' then and rules.seq_rule = trf.seq_rule
when 'ACC' then and rules.seq_rule = acct.seq_rule
else and rules.seq_rule = otro.seq_rule
end
Try to remove the 'and' keywords from there.