how to use update query in to update query in postgresql - sql

I want to update ChannelInfo what ever Reporting ChannelId update row returns
but this query is not working, giving error:
ERROR: syntax error at or near ""AdsReporting""
LINE : where "CampaignInfo"."id" in (update "AdsReporting" set "adS...
^
SQL state: 42601
Character: 240
can anyone please help me?
Reporting ChannelId is a foreign key relationship with ChannelInfo primary key.
code:
update "ChannelInfo" set "Amount"=CASE WHEN "Duration"='60' THEN "Amount"-12.25 ELSE "Amount"-6.13 END
where "id"=(update "Reporting" set "Status"='run' where "Status"='a' RETURNING "ChannelId");

It sounds like you want to use CTEs:
with u as (
update "Reporting"
set "Status" = 'run'
where "Status" = 'a'
RETURNING "ChannelId"
)
update "ChannelInfo" ci
set "Amount"= (CASE WHEN ci."Duration" = '60' THEN ci."Amount" - uc.cnt * 12.25 ELSE ci."Amount" - uc.cnt * 6.13 END)
from (select "ChannelId", count(*) as cnt
from users u
group by "ChannelId"
) uc
where uc."ChannelId" = ci.id;

Related

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;

ERROR: CASE types integer and boolean cannot be matched

I'm getting the abovementioned error when trying to execute an update query. See below schema for reference.
The goal of the query is to update the capital row of the client table to be the same as the solde row of the compte table if the client has a compte. If they do not, the value of capital must be 0. I tried the below query and the error message mentioned in the title appeared.
UPDATE client
SET capital = (
SELECT
CASE
WHEN client_compte.compte_num IS NOT NULL THEN client.capital = compte.solde
ELSE 0
END
FROM compte
JOIN client_compte ON compte.compte_num = client_compte.compte_num
JOIN client ON client_compte.client_id = client.client_id);
Any help solving this error?
The message seems quite clear -- the types of the columns do not match. However, your update is malformed because of the two references to client. Presumably, you intend:
UPDATE client cl
SET capital = (CASE WHEN cc.compte_num IS NOT NULL
THEN cc.solde
ELSE 0
END)
FROM client_compte cc JOIN
compe c
ON c.compte_num = cc.compte_num;
WHERE cc.client_id = cl.client_id;
That fixes the error but is not exactly what you want. I think a correlated subquery is better:
UPDATE client cl
SET capital = COALESCE((SELECT cc.solde
FROM client_compte cc JOIN
compte c
ON c.compte_num = cc.compte_num
WHERE cc.client_id = cl.client_id
), 0
);

Update multiple columns from a sub query

UPDATE PINPOINT_SUPPLEMENT
SET (ATTACHMENT_VALUE,ATTACHMENT_TYPE) = (
SELECT key,'file'
FROM PINPOINT_DOCUMENT
WHERE PINPOINT_SUPPLEMENT.ATTACHMENT_VALUE::integer = PINPOINT_DOCUMENT.DOCUMENT_ID
)
WHERE ATTACHMENT_VALUE IS NULL
Getting Error when i execute this query
ERROR: syntax error at or near "SELECT"
LINE 3: SELECT key,'file
update PINPOINT_SUPPLEMENT
set
ATTACHMENT_VALUE = PINPOINT_DOCUMENT.key,
ATTACHMENT_TYPE = 'file'
from PINPOINT_DOCUMENT
where
PINPOINT_SUPPLEMENT.ATTACHMENT_VALUE::integer = PINPOINT_DOCUMENT.DOCUMENT_ID
and PINPOINT_SUPPLEMENT.ATTACHMENT_VALUE IS NULL
or
update PINPOINT_SUPPLEMENT
set
(ATTACHMENT_VALUE,ATTACHMENT_TYPE) = (PINPOINT_DOCUMENT.key, 'file')
from PINPOINT_DOCUMENT
where
PINPOINT_SUPPLEMENT.ATTACHMENT_VALUE::integer = PINPOINT_DOCUMENT.DOCUMENT_ID
and PINPOINT_SUPPLEMENT.ATTACHMENT_VALUE IS NULL
Support for updating tuples was introduced in Postgres 9.5, so you can't use that syntax with your version.
But as the second value is a constant I don't see a reason to use that syntax to begin with:
UPDATE PINPOINT_SUPPLEMENT
SET ATTACHMENT_VALUE = (SELECT "key"
FROM PINPOINT_DOCUMENT
WHERE PINPOINT_SUPPLEMENT.ATTACHMENT_VALUE::integer = PINPOINT_DOCUMENT.DOCUMENT_ID),
ATTACHMENT_TYPE = 'file'
WHERE ATTACHMENT_VALUE IS NULL
Note, that the sub-query might result in an error if there is more than one ATTACHMENT_VALUE for a document_id!

Postgres syntax error in if condition

I am trying to calculate commission and according to that commission value (commiss) i want to update a column value fixed_fee
select st1.* ,(st1.order_item_value * st1.commission_rate)/100 commiss ,
if commiss < 50 then
update payment_error set fixed_fee = -5 where commiss <= 50
else if commiss >= 50 then
update payment_error set fixed_fee = -10 where commiss >=50
else
end if
from payment_error as st1 join payment_error as st2 on st1.order_item_id=st2.order_item_id ;
while running is then error come is :
ERROR: syntax error at or near "<"
LINE 2: if commiss<50 then
^
********** Error **********
ERROR: syntax error at or near "<"
SQL state: 42601
Character: 87
You can't use if in SQL. This is only available in procedural languages like PL/pgSQL.
But more importantly you cannot embed an update statement inside a select statement like that.
What you are trying to do can be done with a single UPDATE statement using a CASE:
update payment_error
set fixed_fee = case
when (order_item_value * commission_rate)/100 <= 50 then -5
else 5 --<<< different value here
end;
You are setting the fixed_fee to -5 in both cases - I assume that is a typo, and you do mean to assign a different fee for the case when the comission is greater than 50
You cannot use update and select together. To update you can try this:
update payment_error
set fixed_fee = case when (order_item_value * commission_rate * .01) < 50 then -5
else 5 --In your case you have use both as -5 so change it as per your reqm

SQL Update syntax check

Writing a simple update statement in Teradata and I'm having trouble getting it to work. I'm getting a syntax error saying: Syntax error: expected something between the word 'First_name' and the 'FROM' keyword . This is reference to line 7. I have no idea what I'm missing.
Here's the code with some redacted object names:
UPDATE DATA.CONTACTS tgt
SET
tgt.LAST_NAME = TABLES.PART.LAST_NAME
,tgt.BPP_USER_ID = TABLES.PART.User_Id
,tgt.Email_Address = TABLES.PART.Email
,tgt.Last_name = TABLES.PART.Last_name
,tgt.First_name = TABLES.PART.First_name
FROM
(SELECT
C_C
, USER_ID
, Email
,Last_name
,First_name
FROM TABLES.PART) --ppage
WHERE EMAIL_ADDRESS IN (
SELECT Email
FROM DATA.CONTACTS
);
select * from mmbi_tables_data.crm_mmbi_contacts
I've tried deleting using that ppage alias but I still get the same error regardless of what I do.
This doesn't look so simple. I think the following will work in Teradata:
UPDATE tgt
FROM data.contacts tgt, tables.part ppage
SET LAST_NAME = TABLES.PART.LAST_NAME,
BPP_USER_ID = TABLES.PART.User_Id,
Email_Address = TABLES.PART.Email,
Last_name = TABLES.PART.Last_name,
First_name = TABLES.PART.First_name
WHERE tgt.email = ppage.email_address;