I wanted to update columns based on condition: There is this table with columns A, B, C, D, E.
These columns with values are to be updated with null. I do not want to touch the columns without any values.
...
UPDATE
Table
SET
A = CASE WHEN A!=NULL THEN A=NULL
B= CASE WHEN B!=NULL THEN B=NULL
C= CASE WHEN C!=NULL THEN C='U'
D= CASE WHEN D!=NULL THEN D=NULL ELSE END,
WHERE
where condition;
...
PS:: The column values are not same.
Your query was almost close.Below one should work,
update test_Data
set A = CASE WHEN A is not NULL THEN NULL END,
B = CASE WHEN B is not NULL THEN NULL END ,
C = CASE WHEN C is not NULL THEN NULL END,
D = CASE WHEN D is not NULL THEN NULL END,
E = CASE WHEN E = 'abc' THEN 'QWE' ELSE E END
where (A is not NULL or B is not NULL or C is not NULL or D is not null or E is not null)
UPDATE:
If any of the column has NOT NULL Constraint, then column has to be altered to take NULL.
alter table test_Data modify E null;
for demo with example refer DB fiddle link - https://dbfiddle.uk/?rdbms=oracle_18&fiddle=7f8bfc2f5edda71a9eb5af5ccb2065de
Related
I need to add a column that Sums the value column of all columns that have a common id. However, any id = null is not summed, but equal to the value column.
The above example should result in:
TopPaymendId JournalLineNetAmount TopAmount
fcbcd407-ca26-4ea0-839a-c39767d05403 -3623.98 -7061.23
fcbcd407-ca26-4ea0-839a-c39767d05403 -3437.25 -7061.23
ce77faac-1638-40e9-ad62-be1813ce9031 -88.68 -88.68
531d9bde-3f52-47f3-a9cf-6f3566733af2 -152.23 -152.23
8266dfef-dd14-4654-a6d2-091729defde7 229.42 229.42
f8b97a47-15ef-427d-95e0-ce23cc8efb1f -777 -777
null -3.01 -3.01
null -2.94 -2.94
null 3312.5 3312.5
This code should work:
let
Source = Excel.CurrentWorkbook(){[Name="Data"]}[Content],
group = Table.Group(Source, {"TopPaymendId"}, {"TopAmount", each List.Sum([JournalLineNetAmount])}),
join = Table.Join(Source,{"TopPaymendId"},group,{"TopPaymendId"}),
replace = Table.ReplaceValue(join,each [TopAmount],each if [TopPaymendId] = null
then [JournalLineNetAmount] else [TopAmount],Replacer.ReplaceValue,{"TopAmount"})
in
replace
I have two tables in Bigquery Table A and Table B.
Table A has two columns - name(String) & value(Float). The Name columns is can contain null values.
Table B has 3 columns - start_value(Float), end_value(FLoat) and name(String). These 3 columns won't be empty at any cost.
My aim is to update Table A for the rows having name as null. The logic is basically identify the value for which name is null and then find the corresponding row in Table B where
a.value >= b.start_value and a.value <= b.end_value
In this way, I have to update all the rows in Table A in a single query. How can I achieve this?
Note: No two rows in Table A will be same.
UPDATE `project.dataset.tableA` a
SET a.name = b.name
FROM `project.dataset.tableB` b
WHERE a.name IS NULL
AND value BETWEEN start_value AND end_value
Here you have a code that works perfectly on my end:
UPDATE `project.dataset.tableA` a
SET a.name = (
SELECT b.name
FROM `project.dataset.tableB` b
WHERE value BETWEEN start_value AND end_value)
WHERE a.name IS NULL
I need a trigger to update a table DIRECTORY_NUMBER when one value of DN_NUM column matches with MSISDN column value of a different table (RNPH_REQUETS_DETAILS) under a different schema(NKADM). The trigger will run every time there's a new entry in the DIRECTORY_NUMBER table. Based upon several conditions, the values of the DN_STATUS column and a few other columns need to be updated. The updated value of the DN_STATUS column will be 'r' if the conditions are met, and 'w' if the conditions are not met. Active portion of my code is given below:
UPDATE d
SET d.DN_STATUS = CASE WHEN EXISTS (SELECT 1 from NKADM.RNPH_REQUESTS_DETAILS n where n.MSISDN = d.DN_NUM AND n.PROCESS_STATE_ID = 4 AND n.ACTION='IN' AND n.FAILED_STATUS IS NULL AND TRUNC(n.MODIFICATION_DATE) = TRUNC(SYSDATE))
THEN 'r'
ELSE 'w'
END,
d.DN_MODDATE = SYSDATE,
d.BUSINESS_UNIT_ID = 2,
d.HLCODE = 5
WHERE d.DN_ID =: NEW.DN_ID
AND d.PLCODE = 1004
AND d.DN_STATUS = 'f'
FROM DIRECTORY_NUMBER d;
I am getting the following error:
Error(48,1): PL/SQL: SQL Statement ignored
Error(60,3): PL/SQL: ORA-00933: SQL command not properly ended
The errors get resolved only if I remove the references. But that gives a different result than intended. When the code is as follows:
UPDATE DIRECTORY_NUMBER
SET DN_STATUS = CASE WHEN EXISTS (SELECT 1 from NKADM.RNPH_REQUESTS_DETAILS where MSISDN = DN_NUM AND PROCESS_STATE_ID = 4
AND ACTION='IN' AND FAILED_STATUS IS NULL AND TRUNC(MODIFICATION_DATE) = TRUNC(SYSDATE))
THEN 'r'
ELSE 'w'
END,
DN_MODDATE =SYSDATE,
BUSINESS_UNIT_ID=2,
HLCODE =5
WHERE DN_ID =:NEW.DN_ID
AND PLCODE =1004
AND DN_STATUS ='f';
COMMIT;
Even when the CASE WHEN EXISTS condition is true (returns result when run independently), the value of DN_STATUS gets updated to 'w'.
Update: I tried with the following code:
UPDATE DIRECTORY_NUMBER
SET DN_STATUS = 'r',
DN_MODDATE =SYSDATE,
BUSINESS_UNIT_ID=2,
HLCODE =5
WHERE DN_ID =:NEW.DN_ID
AND PLCODE =1004
AND DN_STATUS ='f';
AND DN_NUM in (select MSISDN from NKADM.RNPH_PROCESS_DETAILS where PROCESS_STATE_ID = 4);
This isn't working either. If I remove the last condition, the resultant row has DN_STATUS value of 'f', and the MSISDN is in NKADM.RNPH_PROCESS_DETAILS table with PROCESS_STATE_ID = 4. I don't understand why it's not working.
What am I doing wrong?
In BEFORE update/insert trigger for EACH ROW you can modify data of record which is currently processed. You don't need to call an extra UPDATE to change the data.
In other words you can do something like this
IF :NEW.PLCODE = 1004 AND :NEW.DN_STATUS = 'f' THEN
:NEW.DN_MODDATE := SYSDATE;
:NEW.BUSINESS_UNIT_ID := 2;
:NEW.HLCODE := 5;
-- this query you can wrap in a function and call this function
SELECT COUNT(1)
INTO lv_count
FROM NKADM.RNPH_REQUESTS_DETAILS n
WHERE n.MSISDN = :NEW.DN_NUM
AND n.PROCESS_STATE_ID = 4
AND n.ACTION = 'IN'
AND n.FAILED_STATUS IS NULL
AND TRUNC(n.MODIFICATION_DATE) = TRUNC(SYSDATE);
IF lv_count > 0 THEN
:NEW.DN_STATUS := 'r';
ELSE
:NEW.DN_STATUS := 'w';
END IF;
END IF;
How can I LEFT and add WHERE condition?
It doesn't read the where it brings all the ridedriver object?
SELECT p
FROM RideDriverEmployeeBundle:Ridedriver p
LEFT JOIN Chaya3niUserBundle:Bookings b WITH b.idridedriver = p.id
WHERE p.frequency IS NOT NULL
AND p.nbrplaces > 0
AND b.iduser != 2
OR b.iduser IS NULL
If I understand your question correctly it is probably an issue with your WHERE clause. Try using parantheses with the OR portion of the clause.
SELECT p
FROM RideDriverEmployeeBundle:Ridedriver p
LEFT JOIN Chaya3niUserBundle:Bookings b WITH b.idridedriver = p.id
WHERE p.frequency IS NOT NULL
AND p.nbrplaces > 0
AND (b.iduser != 2 OR b.iduser IS NULL)
To answer the question you asked in the comments..
i want to select all the driverrides except the one that the user already booked in
Sounds like you just do not want the other conditions you have in your WHERE clause.
SELECT p
FROM RideDriverEmployeeBundle:Ridedriver p
LEFT JOIN Chaya3niUserBundle:Bookings b WITH b.idridedriver = p.id
WHERE b.iduser IS NULL
By doing a LEFT JOIN and then only taking records that did not exist in the joined table with WHERE .. IS NULL, this is doing a LEFT OUTER JOIN.
In your case Ridedriver is Table A and Bookings is Table B, so this will return records in Ridedriver that do not join to anything in Bookings.
I have seen a SAS data like this.
data combined;
merge demo(in=d) history(in=p); by id;
if d and not p;
run;
What does if d and not p mean? My textbook says it is the same as
a. if d>p or
b. if d^=p and d
c. if d^=p and not p
d. All of above.
My guess was b, but I am not sure what the correct understanding of the logic is.
This is using the dataset option IN
When merging:
If a record is found in DEMO, D is set to 1, otherwise 0.
If a record is found in HISTORY, P is set to 1, otherwise 0.
For evaluating SAS logic, 1 can be considered TRUE and 0 is false.
Answers to evaluate to true:
If d>p -> only true when D=1, P=0
If d ne p and d -> D=1, P=0
For an AND to be TRUE both components must be true which means D =1.
If d ne p and NOT P -> D=1, P=0
NOT P to be TRUE means P=0
All resolve to the same so the answer is D.