Combining multiple condition in single case statement - exact-online

Does Invantive SQL support multiple condition in a single case statement? I the statement below, I did not get any results. Tried the same statement with only 1 condition (no cascade), this retrieved the expected result.
select prj.code
, prj.startdate
, prj.enddate
from exactonlinerest..projects prj
where prj.code between $P{P_PROJECT_FROM} and $P{P_PROJECT_TO}
and case
/* when (prj.enddate is null or prj.enddate >= sysdate)
then 'Y'
when (prj.enddate is not null and prj.enddate <= sysdate)
then 'N' */
when prj.startdate <= sysdate
then 'B'
end
= $P{P_PROJECT_ACTIVE_FROM}

I think you where clause is not correctly formulated. With Exact Online, a project either has:
option 1: no end date,
option 2: an end date in the past
option 3: or an end date in the future
The first part of the case handles option 1 and option 3. The second part handles option 2. So there is never an outcome of 'B' in the case.
To analyze such problems, I recommend include the case in the select clause and removing the filter. That gives you a view of the possible outcomes.
Example:
use 868056,102673
select prj.division
, prj.code
, prj.startdate
, prj.enddate
, case
when prj.enddate is null or prj.enddate >= sysdate
then 'Y'
when prj.enddate is not null and prj.enddate <= sysdate
then 'N'
when prj.startdate <= sysdate
then 'B'
end
indicator
from exactonlinerest..projects prj
where prj.code between $P{P_PROJECT_FROM} and $P{P_PROJECT_TO}

Related

How to use placeholder in sql to another column in select

i want to make a report and i already calculate it in one column then i want to use the result to another column, something like this
select addtime (timediff(a,b), c) as 'total_lead', case when
total_lead <= then 'yes' else 'no' end as 'check data' from d
so i want to use the result and use it in another column
anyone can help?
You have to either repeat the expression:
select
addtime(timediff(a,b), c) as 'total_lead',
case when addtime(timediff(a,b), c) <= ? then 'yes' else 'no' end as 'check data'
or use a subquery, which may change the performance:
select
total_lead,
case when total_lead <= ? then 'yes' else 'no' end as 'check data'
from (
select
addtime(timediff(a,b), c) as 'total_lead'
from d
) d_with_total_lead

mysql date range query, when the field is not sent anything for the 'between' statement

I have this query, which returns me 7 records
Now if I add this line 'AND date_format (ds.date_emission,'% Y-% m-% d ') BETWEEN' 'AND' '', nothing returns me, I want that when the date range is empty I need that I also returned the 7 records
Please refer answer below.
I have added two variable replace inStartDate variable with a range start date and inEndDate with range end date.
SELECT ds.id
FROM documentos_salida ds
WHERE ds.terceros_id = 329
AND ds.estado =0
AND ds.tipo_documento ='Factura'
AND ds.saldo_pendiente_factura >0
AND (ds.fecha_vencimiento > now() or
ds.fetcha_vencimiento = NOW())
AND ds.numero_documento LIKE '%%'
AND ( CASE WHEN (inStartDate = '' AND inEndDate = '')
THEN TRUE
ELSE (date_format(ds.fetcha_emision, '%Y-%m-%d') BETWEEN inStartDate AND inStartDate)
END)
ORDER BY ds.fecha_emision DESC;

How to get the 1st transaction date then separate it to another column

I want to separate the 1st transaction date of every client to another column. Im not good at explaining. Hope the image below is enough to clarify my question..
I want to generate a result like in the picture. **I just edited this picture for the reference of the result that I want.
this my sqlfiddle ---> http://sqlfiddle.com/#!9/bebf5/1
I believe PN_NO represents client. We would need to perform ordering of rows based on PN_NO and Pay Date columns. It is needed to identify first transaction date. Please try below query.
SELECT
SCHED_NO,
PN_NO,
PAY_PRIN,
PAY_DATE,
CASE WHEN PN_NO_Rank=1 THEN PAY_DATE ELSE '' END AS BEGIN,
CASE WHEN PN_NO_Rank <>1 THEN PAY_DATE ELSE '' END AS CR
FROM
(
SELECT
SCHED_NO,
PN_NO,
PAY_PRIN,
PAY_DATE,
#PN_NO_Rank := IF(#current_PN_NO = PN_NO, #PN_NO_Rank + 1, 1) AS PN_NO_Rank,
#current_PN_NO := PN_NO
FROM PAY_SCHED
ORDER BY PN_NO, PAY_DATE ASC
) AS Temp
That should help:
SELECT SCHED_NO, PN_NO, PAY_PRIN, PAY_DATE,
CASE WHEN pay_date = (select min(pay_date) from pay_sched where pn_no=ps.pn_no) THEN pay_date ELSE NULL END as "BEGIN",
CASE WHEN pay_date = (select min(pay_date) from pay_sched where pn_no=ps.pn_no) THEN NULL ELSE pay_date END as "CR"
FROM PAY_SCHED ps order by pn_no, pay_date
http://sqlfiddle.com/#!9/bebf5/26/0

How can I use "OR" condition in MySQL CASE expression?

I have a procedure that contains CASE expression statement like so:
BEGIN
....
WHILE counter < total DO
....
CASE ranking
WHEN 1 OR 51 OR 100 OR 167 THEN SET
project_name = 'alpha';
WHEN 2 THEN SET
project_name = 'beta';
WHEN 10 OR 31 OR 40 OR 61 THEN SET
project_name = 'charlie';
....
ELSE SET
project_name = 'zelta';
END CASE;
INSERT INTO project (id, name) VALUES (LAST_INSERT_ID(), project_name);
SET counter = counter + 1;
END WHILE;
END
$$
DELIMITER ;
When I call the above procedure, cases with OR statements are either skipped completely or only the first item in the list is matched. What am I doing wrong?
CASE ranking
WHEN 1 THEN 'alpha'
WHEN 2 THEN 'beta'
WHEN 10 THEN 'charlie'
ELSE 'zelta'
END CASE;
You can use one of expresions that WHEN has, but you cannot mix both of them.
1) WHEN when_expression
Is a simple expression to which input_expression is compared when the simple CASE format is used. when_expression is any valid expression. The data types of input_expression and each when_expression must be the same or must be an implicit conversion.
2) WHEN Boolean_expression
Is the Boolean expression evaluated when using the searched CASE format. Boolean_expression is any valid Boolean expression.
You could program:
1)
CASE ProductLine
WHEN 'R' THEN 'Road'
WHEN 'M' THEN 'Mountain'
WHEN 'T' THEN 'Touring'
WHEN 'S' THEN 'Other sale items'
ELSE 'Not for sale'
2)
CASE
WHEN ListPrice = 0 THEN 'Mfg item - not for resale'
WHEN ListPrice < 50 THEN 'Under $50'
WHEN ListPrice >= 50 and ListPrice < 250 THEN 'Under $250'
WHEN ListPrice >= 250 and ListPrice < 1000 THEN 'Under $1000'
ELSE 'Over $1000'
END
But in any case you can expect that the variable ranking is going to be compared in a boolean expresion.
http://msdn.microsoft.com/en-us/library/ms181765.aspx
you can use in to compare the values both numeric or character
CASE
WHEN ranking in(1,2,3) THEN '1Q'
WHEN ranking in(4,5,6) THEN '2Q'
ELSE '3Q'
END CASE;
CASE
WHEN ranking in('1','2','3') THEN '1Q'
WHEN ranking in('4','5','6') THEN '2Q'
ELSE '3Q'
END CASE;
this will also work in select statement and stored procedure also.
select case when month(curdate()) in (4,5,6) then 1 when month(curdate()) in (7,8,9) then 2 else 3 end as fiscal_quarter ;
This is also possible:
select (case when (var1 = 0 or var2 = 1) then 'x' else 'y' end)
from...

MySQL IF ELSEIF in select query

I'm trying to select different prices of a product based on the quantity that user chooses.
This is the query I'm working on (it has a syntax error):
select id,
(SELECT
IF(qty_1<='23',price,1)
ELSEIF(('23'>qty_1 && qty_2<='23'),price_2,1)
ELSEIF(('23'>qty_2 && qty_3<='23'),price_3,1)
ELSEIF('23'>qty_3,price_4,1)
END IF) as total
from product;
You have what you have used in stored procedures like this for reference, but they are not intended to be used as you have now. You can use IF as shown by duskwuff. But a Case statement is better for eyes. Like this:
select id,
(
CASE
WHEN qty_1 <= '23' THEN price
WHEN '23' > qty_1 && qty_2 <= '23' THEN price_2
WHEN '23' > qty_2 && qty_3 <= '23' THEN price_3
WHEN '23' > qty_3 THEN price_4
ELSE 1
END) AS total
from product;
This looks cleaner. I suppose you do not require the inner SELECT anyway..
IF() in MySQL is a ternary function, not a control structure -- if the condition in the first argument is true, it returns the second argument; otherwise, it returns the third argument. There is no corresponding ELSEIF() function or END IF keyword.
The closest equivalent to what you've got would be something like:
IF(qty_1<='23', price,
IF('23'>qty_1 && qty_2<='23', price_2,
IF('23'>qty_2 && qty_3<='23', price_3,
IF('23'>qty_3, price_4, 1)
)
)
)
The conditions don't all make sense to me (it looks as though some of them may be inadvertently reversed?), but without knowing what exactly you're trying to accomplish, it's hard for me to fix that.
I found a bug in MySQL 5.1.72 when using the nested if() functions .... the value of column variables (e.g. qty_1) is blank inside the second if(), rendering it useless. Use the following construct instead:
case
when qty_1<='23' then price
when '23'>qty_1 && qty_2<='23' then price_2
when '23'>qty_2 && qty_3<='23' then price_3
when '23'>qty_3 then price_4
else 1
end
For your question :
SELECT id,
IF(qty_1 <= '23', price,
IF(('23' > qty_1 && qty_2 <= '23'), price_2,
IF(('23' > qty_2 && qty_3 <= '23'), price_3,
IF(('23' > qty_2 && qty_3<='23'), price_3,
IF('23' > qty_3, price_4, 1))))) as total
FROM product;
You can use the if - else control structure or the IF function in MySQL.
Reference:
http://easysolutionweb.com/sql-pl-sql/how-to-use-if-and-else-in-mysql/
As per Nawfal's answer, IF statements need to be in a procedure. I found this post that shows a brilliant example of using your script in a procedure while still developing and testing. Basically, you create, call then drop the procedure:
https://gist.github.com/jeremyjarrell/6083251