ParseException at FROM statement when updating SQL view using AND operator with CASE WHEN - sql

I am getting an error at the FROM sb.v_transactions t line after my CASE WHEN statements. I tried adding commas, parentheses, and am considering perhaps using CTE to create my output. Was wondering if anyone could help me figure out why I'm getting the error? I have a feeling it has something to do with how I'm using the AND operators.
%sql
UPDATE sb.transactions u
SET t.FranchiseStore = u.FranchiseStore
AND t.LocationNumber = u.LocationNumber
AND openflg = CASE openflg
WHEN o.FiscYKey IS NULL THEN 1
ELSE 0
END
AND closeflg_y = CASE closeflg_y
WHEN c.FiscYKey IS NULL THEN 1
ELSE 0
END
AND closeflg_p = CASE closeflg_p
WHEN c.FiscYKey IS NULL THEN 1
ELSE 0
END
AND closeflg_w = CASE closeflg_w
WHEN c.FiscYKey IS NULL THEN 1
t.FiscWkKey THEN 1
ELSE 0
END
-- PARSE EXCEPTION ON THE FROM BELOW. WHY?
FROM sb.v_transactions t
LEFT JOIN sb.v_loc_opendates o
ON t.LocationNumber = o.LocationNumber
LEFT JOIN sb.v_loc_closedates c
ON t.LocationNumber = c.LocationNumber
WHERE t.LocationNumber = u.LocationNumber
AND t.DayDate = u.DayDate

Related

Nested case conditionals in PostgreSQL, syntax error?

I use PostgreSQL and I would like to combine these two case conditions, but when I uncomment the code, I get a syntax error. This is not a difficult instruction. I want to divide or multiply the obtained number depending on the condition. How can I enter it so that the code is compiled?
SELECT
SUM(
CASE
WHEN transactions.recipient_account_bill_id = recipientBill.id AND recipientBill.user_id = 2
THEN 1
ELSE -1
END * transactions.amount_money
/* CASE
WHEN senderBillCurrency.id = recipientBillCurrency.id
THEN NULL
ELSE
CASE
WHEN recipientBillCurrency.base = true
THEN /
ELSE *
END senderBillCurrency.current_exchange_rate
END */
) as TOTAL
FROM transactions
LEFT JOIN bills AS senderBill ON senderBill.id = transactions.sender_account_bill_id
LEFT JOIN bills AS recipientBill ON recipientBill.id = transactions.recipient_account_bill_id
LEFT JOIN currency as senderBillCurrency ON senderBillCurrency.id = senderBill.currency_id
LEFT JOIN currency as recipientBillCurrency ON recipientBillCurrency.id = recipientBill.currency_id
WHERE 2 IN (senderBill.id, recipientBill.id)
You cannot create dynamic SQL expressions like that. A CASE expression cannot return an operator as if SQL was some sort of macro language. You can only return an expression.
You already used the following approach using 1 and -1 with your multiplication. Why not also use it with N and 1/N:
<some expression> * CASE WHEN condition THEN 1 / N ELSE N END
Or in your case:
<some expression> * CASE
WHEN senderBillCurrency.id = recipientBillCurrency.id THEN 1
WHEN recipientBillCurrency.base THEN 1 / senderBillCurrency.current_exchange_rate
ELSE senderBillCurrency.current_exchange_rate
END
Notice, you can put several WHEN clauses in a CASE expression. No need to nest them

CASE with IN clause

I have a condition (in Business objects ) as below
=If([Actual ]=0;Sum([Applied] In ([Project];[ Name];[Number];[Sub ])))
which I need to convert it to CASE statement for my OBIEE
Below is the query I had tried but it doesn't work:
SELECT
CASE
WHEN F.ACTUAL_EQP_COST = 0
THEN SUM((F.HRS_APPLIED) IN(F.PROJECT,F.NAME,F.NUMBER,F.SUB))
ELSE 0
END
FROM F
Probably
select SUM(F.HRS_APPLIED)
from DTS_OSC_WIP_REP_CST_ANALYSIS_A F
where F.ACTUAL_EQP_COST = 0
group by F.PROJECT,F.WIP_ENTITY_NAME,F.OPERATION_NUMBER,F.OPERATION_SUB_SEQ
You can't use "in" inside a "then" statement.
I couldn't understand exactly but something in this direction might help:
select
CASE WHEN F.ACTUAL_EQP_COST = 0
THEN (SELECT SUM(F1.HRS_APPLIED)
FROM DTS_OSC_WIP_REP_CST_ANALYSIS_A F1
WHERE F1.column_name IN (F.PROJECT,F.WIP_ENTITY_NAME,F.OPERATION_NUMBER,F.OPERATION_SUB_SEQ))
ELSE 0 END
from DTS_OSC_WIP_REP_CST_ANALYSIS_A F

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)

Using CASE WHEN in a WHERE Statement to return ALL records if #parameter = 0

I am using Visual Studio 2008 and am in a report trying to create a query that has a WHERE statement that will return all records, if the #parameter = 0, otherwise I want the query to return only records that have the #paramete.
Here is the statement I envision, but can't seem to get correct:
WHERE (ADVISOR.staffPersonID = CASE WHEN #advisor = 0 THEN **'Do NOT
filter by staffpersonID'** ELSE #advisor END)
I have tried the following code which doesn't work:
CASE WHEN #advisor = 0 THEN ADVISOR.staffPersonID ELSE #advisor END
ADVISOR.staffPersonID is the field I would filter on, if the #parameter had a value other than 0. From what I've researched online, using the field itself as the THEN in the statement should work. It does, unless the table (advisor) has no records that match in it at all.
WHERE
(#advisor <> 0 AND ADVISOR.staffPersonID = #advisor)
OR
(#advisor = 0 )
try this:
WHERE ADVISOR.staffPersonID = #advisor OR #advisor = 0
WHERE ((ADVISOR.staffPersonID IS NULL AND #advisor = 0) 
OR (ADVISOR.staffPersonID  = CASE WHEN #advisor = 0 THEN 
ADVISOR.staffPersonID 
ELSE 
#advisor END
))

T-SQL and decision making constructs

Please help me with the syntax of this query. It doesn't compile. It says there's a syntax error near the keyword END.
Obviously, I've got these BEGINs and ENDS mixed up.
I am using Microsoft SQL Server 2008 R2. I am not sure of the syntax of these BEGINS and ENDs.
Please don't mind the condition 1 = 0. That's something that will be replaced with a proper predicate later.
IF EXISTS (SELECT * FROM StringCategory WHERE ResourceKeyId = 18134 AND CategoryId = 0)
BEGIN
UPDATE StringCategory
SET CategoryId = 0
WHERE ResourceKeyId = 18134
END
ELSE
BEGIN
IF 1 = 0
BEGIN
DELETE FROM StringCategory WHERE ResourceKeyId = 18134
END
ELSE
BEGIN
INSERT INTO StringCategory
VALUES(18134, 0)
END
END
END
Your last END is an extra. You can think of the BEGINs and ENDs like { and } in C# for the IF constructs (They serve to mark the beginning and end of the block to be executed in the IF/ELSE statement).
Here is a much simpler way to do what you appear to be attempting.
insert into StringCategory
(ResourceKey, CategoryId)
select 18134, 0
where not exists (
SELECT *
FROM StringCategory
WHERE ResourceKeyId = 18134
AND CategoryId = 0)