Convert Crystal Report Expression to SQL Query - sql

How can I change the below Crystal Report Expression to SQL Server Query Case Statement ? Any help appreciated.
AvgLbsPerWeek
If {#EnoughWeightsSum} < {#FreqForCalcs} then
(AvgLbsPerWeek := AvgLbsPerWeek +0;
0;)
else If {#EnoughWeightsSum} >= {#FreqForCalcs} then
(AvgLbsPerWeek := AvgLbsPerWeek +{#AvgByDaySum}/{#EnoughWeightsSum}*{#FreqForCalcs};
{#AvgByDaySum}/{#EnoughWeightsSum}*{#FreqForCalcs};)

This is a simple CASE statement
SELECT
AvgLbsPerWeek =
CASE WHEN #EnoughWeightSum < #FreqForCalcs
THEN AvgPoundsPerWeek
ELSE
AvgPoundsPerWeek + #AvgByDaySum/(#EnoughWeightSum*FreqForCalcs)
-- I didn't quite understand how the last code fit in
END
FROM Table

Related

Performing additional calculations in PROC SQL

I have this specification to derive AVAL for parameter Code DACAPNT
For the text highlighted in red this is the code I'm trying to use
*Performing calculations for DACAPNT AVAL *;
proc sql;
create table sum_by_totda as
select *
,sum(dastresn) as totn
from sdtm.da
where datestcd in ("RETAMT","DISPAMT")
group by usubjid, datestcd
;
quit;
My question is is there a way to do the calculation within this PROC SQL statement or would this require an additional data step/PROC SQL?
You can include CASE/WHEN/THEN/ELSE/END logic within aggregate functions in SQL, so this could be achieved using something like this:
sum(case when paramcd = 'CAPSEXP' then aval else 0 end +
case when datestcd = 'RETAMT' then dastresn else 0 end +
case when datestcd = 'DISPAMT' then -1*dastresn else 0 end)

GETTING ERROR-- ORA-00936:MISSING EXPRESSION for below query please help on this

SELECT CASE (SELECT Count(1)
FROM wf_item_activity_statuses_v t
WHERE t.activity_label IN ('WAITING_DISB_REQ',
'LOG_DDE',
'LOG_SENDBACK_DDE')
AND t.item_key IN(
SELECT r.i_item_key
FROM wf_t_item_xref r
WHERE r.sz_appl_uniqueid = '20400000988')
)
WHEN 0 THEN
(
delete
from t_col_val_document_uploaded p
WHERE p.sz_application_no = '20400000988'
AND p.sz_collateral_id = 'PROP000000000PRO1701'
AND p.i_item_key = '648197'
AND p.i_document_srno = '27' )
WHEN 1 THEN
(
DELETE
FROM t_col_val_document_uploaded p
WHERE p.sz_application_no = '20400000988'
AND p.sz_collateral_id = 'PROP000000000PRO1701'
AND p.i_document_srno = '28' )
ELSE NULL
END
FROM dual;
You need to recreate your query and make sure to follow the flow of the clauses properly, please check the next two links to get a better understanding :
[ORA-00936: missing expression tips]
How do I address this ORA-00936 error?
Answer: The Oracle oerr utility notes this about the ORA-00936 error:
ORA-00936 missing expression
Cause: A required part of a clause or expression has been omitted. For example, a SELECT statement may have been entered without a list of columns or expressions or with an incomplete expression. This message is also issued in cases where a reserved word is misused, as in SELECT TABLE.
Action: Check the statement syntax and specify the missing component.
The ORA-00936 happens most frequently:
1 - When you forget list of the column names in your SELECT statement.
2. When you omit the FROM clause of the SQL statement.
ora-00936-missing-expression
I hope this can help you.
You cannot use a simple select query like this. You have to use a PL/SQL block like below -
DECLARE NUM_CNT NUMBER := 0;
BEGIN
SELECT Count(1)
INTO NUM_CNT
FROM wf_item_activity_statuses_v t
WHERE t.activity_label IN ('WAITING_DISB_REQ',
'LOG_DDE',
'LOG_SENDBACK_DDE')
AND t.item_key IN(SELECT r.i_item_key
FROM wf_t_item_xref r
WHERE r.sz_appl_uniqueid = '20400000988');
IF NUM_CNT = 0 THEN
delete
from t_col_val_document_uploaded p
WHERE p.sz_application_no = '20400000988'
AND p.sz_collateral_id = 'PROP000000000PRO1701'
AND p.i_item_key = '648197'
AND p.i_document_srno = '27';
ELSIF NUM_CNT = 1 THEN
DELETE
FROM t_col_val_document_uploaded p
WHERE p.sz_application_no = '20400000988'
AND p.sz_collateral_id = 'PROP000000000PRO1701'
AND p.i_document_srno = '28' )
END IF;
END;

SAP HANA SQL Query with Dynamic Placeholder

I have a query that is passing the current year as a placeholder parameter that right now is hard coded. How can I have this just pass the current year? I've seen a few different potential solutions but most of them are in HANA Studio or involve dynamic SQL generation.
I'm putting the SQL into Tableau so those are both off the table.
...sum("StockInQualityInspection") as in_quality,
sum("StockInTransit") as its
from "_SYS_BIC"."stream.models.marketing.poly/InventoryQuery" ('PLACEHOLDER' = ('$$IPCurrentYear$$', '2018'))
where "StockValuatedUnrestrictedUse" <> 0 or "StockInQualityInspection" <> 0 or "StockInTransit" <> 0
group by case when "ReceivingPlant" is null then "Plant" else "ReceivingPlant" end,
case....
Remove the parameters input of your CV
Add this expression: year(now())
If you don't have access to manipulate the CV, into your query use:
('PLACEHOLDER' = ('$$IPCurrentYear$$', select year(now()) from DUMMY))
Regards
while placing a query is not permitted, you can pass a parameter as following
do begin
declare lv_param nvarchar(100);
select max('some_date')
into lv_param
from dummy /*your_table*/;
select *
from "_SYS_BIC"."path.to.your.view/CV_TEST" (
PLACEHOLDER."$$P_DUMMY$$" => :lv_param
);
end;
more can be found here
credit to #astentx

Case select another column AS400 query

I have simple query which is not working:
SELECT BATCH_0002.CREATOR, BATCH_0002.GLEXR,
case when BATCH_0002.GLIVD >'0' THEN BATCH_0002.GLIVD ELSE BATCH_0002.date end
as tarih FROM BATCH_0002
Error is:
ERROR: [IBM][System i Access ODBC Driver][DB2 for i5/OS]SQL0581 - The
results in a CASE expression are not compatible. Error Code:
-581
but when I change column name to something different than GLIVD, it is working.
SELECT BATCH_0002.CREATOR, BATCH_0002.GLEXR,
case when BATCH_0002.GLIVD >'0' THEN BATCH_0002.GLEXR ELSE BATCH_0002.date end
as tarih FROM BATCH_0002
Is there something wrong with my case statement?
In Case statement result datatype should be same. Here Using int and date as result in case that's why error showing for incompatible data
you should cast one datatype to another
SELECT BATCH_0002.CREATOR, BATCH_0002.GLEXR, case when BATCH_0002.GLIVD >'0' THEN cast(BATCH_0002.GLIVD as char) ELSE cast(BATCH_0002.date as char) end as tarih FROM BATCH_0002
thank you for all answers regarding data types. worked on this information and now solved, this works very well:
SELECT BATCH_0002.CREATOR, BATCH_0002.GLEXR,
case when BATCH_0002.GLIVD >'0' THEN
DATE(CHAR(1900000 + BATCH_0002.GLIVD))
ELSE BATCH_0002.date end as tarih FROM BATCH_0002

How to use case when in SQL Server 2008

I want to use case after where clause in SQL Server 2008; how can I use it like
select *
from MPR
where
case when #min >= 0
then MainComponent_Id = #mainc and SubComponent_Id = #subcomp
else MainComponent_Id = #mainc and SubComponent_Id = #subcomp and MinorComponent_Id = #minorc
end
end
I am using a stored procedure, and in my stored procedure if minorcomponent_id value is 0 then it will add 1 one else 2 one will work.
I had tried hard but its giving error in near case.
How to resolve it?
Thanks
You don't need to use a CASE statement. You can simplify logic as follows:
select
*
from
MPR
where
MainComponent_Id = #mainc AND SubComponent_Id = #subcomp
AND (#min >= 0 OR MinorComponent_Id = #minorc)