Converting sql case expression - sql

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.

Related

SQL case query with DISTINCT in cakephp3 ORM

I am trying to build a case query with distinct count in cakephp 3.
This is the query in SQL:
select COUNT(distinct CASE WHEN type = 'abc' THEN app_num END) as "count_abc",COUNT(distinct CASE WHEN type = 'xyz' THEN app_num END) as "count_xyz" from table;
Currently, I got this far:
$query = $this->find();
$abc_case = $query->newExpr()->addCase($query->newExpr()->add(['type' => 'abc']),' app_num','string');
$xyz_case = $query->newExpr()->addCase($query->newExpr()->add(['type' => 'xyz']),'app_num','string');
$query->select([
"count_abc" => $query->func()->count($abc_case),
"count_xyz" => $query->func()->count($xyz_case),
]);
But I can't apply distinct in this code.
Using keywords in functions has been a problem for quite some time, see for example this issue ticket: https://github.com/cakephp/cakephp/issues/10454.
This has been somewhat improved in https://github.com/cakephp/cakephp/pull/11410, so that it's now possible to (mis)use a function expression for DISTINCT as kind of a workaround, ie generate code like DISTINCT(expression), which works because the parentheses are being ignored, so to speak, as DISTINCT is not a function!
I'm not sure if this works because the SQL specifications explicitly allow parentheses to be used like that (also acting as a whitespace substitute), or because it's a side-effect, so maybe check that out before relying on it!
That being said, you can use the workaround from the linked PR until real aggregate function keyword support is being added, ie do something like this:
"count_abc" => $query->func()->count(
$query->func()->DISTINCT([$abc_case])
)
This would generate SQL similar to:
(COUNT(DISTINCT(CASE WHEN ... END)))

How can I use CASE expression with SUM function in SQL?

I have an Oracle SQL query that I need to select certain deduction codes and provide a predetermined amount if based on the premium amount for deduction code 91B. These are hard coded values that I need to summarize.
SELECT SUM(DECODE(DEDCD,'91A',AL_AMOUNT,0)) MED91A,
SUM(CASE WHEN DEDCD = '91B' THEN
DECODE(AL_AMOUNT, 23.54,7.85,
40.62,8.31,
43.85,8.31,
56.77,8.31,
AL_AMOUNT)) MED91B
FROM PS_AL_CHK_DED
WHERE WEEK_NBR = 6
AND TO_CHAR(CHECK_DT, 'YYYY')=TO_CHAR(SYSDATE, 'YYYY')
The SUM(DECODE ..) statement used to summarize values where dedcd = '91A' works fine. When I add the part to summarize values where dedcd = '91B' it produces a 'Missing Keyword' error after the decode statement. I'm trying to streamline the query in order to produce the results I need for these two deduction codes because the full query takes entirely too long to run.
Oracle Sql Developer 4.0.2.15
You must complete the case expression syntax with an END keyword.
Do it like this:
CASE WHEN DEDCD = '91B' THEN
DECODE(AL_AMOUNT, 23.54,7.85,
40.62,8.31,
43.85,8.31,
56.77,8.31,
AL_AMOUNT)
END
A basic case expression looks like:
CASE [ expression ]
WHEN condition_1 THEN result_1
WHEN condition_2 THEN result_2
...
WHEN condition_n THEN result_n
ELSE result
END
See the documentation for more details.

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.

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.

How to select if a row exists in HQL

EDIT: Specifically talking about querying against no table. Yes I can use exists, but I'd have to do
select case when exists (blah) then 1 else 0 end as conditionTrue
from ARealTableReturningMultipleRows
In T-SQL I can do:
select case when exists(blah) then 1 else 0 end as conditionTrue
In Oracle I can do:
select case when exists(blah) then 1 else 0 end as conditionTrue from DUAL
How can I achieve the same thing in HQL?
select count() seems like the second-best alternative, but I don't want to have to process every row in the table if I don't need to.
Short answer: I believe it's NOT possible.
My reasoning:
According to Where can I find a list of all HQL keywords? Hibernate project doesn't publish HQL grammar on their website, it's available in the Hibernate full distribution as a .g ANTLR file though.
I don't have much experience with .g files from ANTLR, but you can find this in the file (hibernate-distribution-3.6.1.Final/project/core/src/main/antlr/hql.g):
selectFrom!
: (s:selectClause)? (f:fromClause)? {
// If there was no FROM clause and this is a filter query, create a from clause. Otherwise, throw
// an exception because non-filter queries must have a FROM clause.
if (#f == null) {
if (filter) {
#f = #([FROM,"{filter-implied FROM}"]);
}
else
throw new SemanticException("FROM expected (non-filter queries must contain a FROM clause)");
}
which clearly states there are some HQL queries having no FROM clause, but that's acceptable if that's a filter query. Now again, I am not an expert in HQL/Hibernate, but I believe a filter query is not a full query but something you define using session.createFilter (see How do I turn item ordering HQL into a filter query?), so that makes me think there's no way to omit the FROM clause.
I'm use fake table with one row for example MyDual.
select case when exists(blah) then 1 else 0 end as conditionTrue from MyDual
According to http://docs.jboss.org/hibernate/core/3.3/reference/en/html/queryhql.html#queryhql-expressions it looks like they support both case and exists statements.