I want to update a row in a table by incrementing by one the integer value of one of the field..
The current doesn't work, why?
Update htmIndex SET numObs = numObs+1 where ...
Simple case, update one row:
SQL> select name
2 , age
3 from t23
4 where id = 2
5 /
NAME AGE
------------ ----------
MR KNOX 47
SQL> update t23
2 set age = age + 6
3 where id = 2
4 /
1 row updated.
SQL> select name
2 , age
3 from t23
4 where id = 2
5 /
NAME AGE
------------ ----------
MR KNOX 53
SQL>
Update a row when the column has a null value:
SQL> select name
2 , age
3 from t23
4 where id = 6
5 /
NAME AGE
------------ ----------
SALLY
SQL> update t23
2 set age=age+5
3 where id = 6
4 /
1 row updated.
SQL> select name
2 , age
3 from t23
4 where id = 6
5 /
NAME AGE
------------ ----------
SALLY
SQL> update t23
2 set age = nvl(age,0) +5
3 where id = 6
4 /
1 row updated.
SQL> select name
2 , age
3 from t23
4 where id = 6
5 /
NAME AGE
------------ ----------
SALLY 5
SQL>
Equally straightforward when updating multiple rows:
SQL> select name
2 , age
3 from t23
4 where age > 20
5 /
NAME AGE
------------ ----------
MR KNOX 53
FOX IN SOCKS 37
CAT 23
LORAX 443
SQL> update t23
2 set age = age + 1
3 where age > 20
4 /
4 rows updated.
SQL> select name
2 , age
3 from t23
4 where age > 20
5 /
NAME AGE
------------ ----------
MR KNOX 54
FOX IN SOCKS 38
CAT 24
LORAX 444
SQL>
It should work. However if the current column value is null then + 1 will return null.
try: Update htmIndex SET numObs = nvl(numObs,0)+1 where ...
Related
I got it with self join:
select a.id, a.name, b.id, b.name, nvl(a.name,b.name)
from names a
join names b
on a.id = b.id
where a.name is null and b.name is not null;
But don't no how do I update it in the table.
I tried update statement but did not work
[]
Problem is that there are no unique [id, name] combinations in rows that don't have empty name columns.
SQL> select * From test;
ID NAME
---------- ------
1 naya
1 naya
2 amar
3 dhruv --> here
4 shyla
4 shyla
3 diya --> here
5 ananya
6 ishaan
5
2
2
1
3
14 rows selected.
SQL>
So, which name would you want to get for ID = 3? You can't have both, so - one option is to select any, e.g. max.
SQL> update test a set
2 a.name = (select max(b.name)
3 from test b
4 where b.id = a.id
5 )
6 where a.name is null;
5 rows updated.
Result:
SQL> select * From test;
ID NAME
---------- ------
1 naya
1 naya
2 amar
3 dhruv
4 shyla
4 shyla
3 diya
5 ananya
6 ishaan
5 ananya
2 amar
2 amar
1 naya
3 diya
14 rows selected.
SQL>
From Oracle 12, you can use:
UPDATE names n
SET name = (SELECT name
FROM names x
WHERE x.id = n.id
ORDER BY name NULLS LAST
FETCH FIRST ROW ONLY)
WHERE name IS NULL
db<>fiddle here
I need to update a table as :
ID | START_DATE | response| FINAL_TREND
1 14-10-2021 4
1 15-10-2021 3
1 16-10-2021 2
1 17-10-2021 2
1 18-10-2021 3
1 19-10-2021 2
OUTPUT AS:
ID | START_DATE | response| FINAL_TREND
1 14-10-2021 4 NULL
1 15-10-2021 3 4
1 16-10-2021 2 3
1 17-10-2021 2 2
1 18-10-2021 3 2
1 19-10-2021 2 3
So, when running a code:
SELECT LAG(RESPONSE,1) OVER (ORDER BY START_DATE) AS NEW
FROM DUMMY_YC
Output as:
NULL
4
3
2
3
2
2
But when using same code in update AS:
UPDATE DUMMY_YC A SET A.RESPONSE = (SELECT LAG(B.RESPONSE,1) OVER (ORDER BY B.START_DATE) AS NEW
FROM DUMMY_YC B WHERE B.START_DATE=A.START_DATE)
output as:
7 rows updated.
But the actual updated value is
RESPONSE|
(null)
(null)
(null)
(null)
(null)
(null)
(null)
Helps will be appreciated. Working On Oracle SQL Developer.
I'd go with merge.
Before:
SQL> SELECT *
2 FROM test
3 ORDER BY id, start_date;
ID START_DATE RESPONSE FINAL_TREND
---------- ---------- ---------- -----------
1 14.10.2021 4 0
1 15.10.2021 3 0
1 16.10.2021 2 0
1 17.10.2021 2 0
1 18.10.2021 3 0
1 19.10.2021 2 0
6 rows selected.
Merge:
SQL> MERGE INTO test a
2 USING (SELECT b.id,
3 b.start_date,
4 b.response,
5 LAG (b.response, 1) OVER (ORDER BY b.start_date) AS final_trend
6 FROM test b) x
7 ON ( x.id = a.id
8 AND x.start_date = a.start_date)
9 WHEN MATCHED
10 THEN
11 UPDATE SET a.final_trend = x.final_trend;
6 rows merged.
After:
SQL> SELECT *
2 FROM test
3 ORDER BY id, start_date;
ID START_DATE RESPONSE FINAL_TREND
---------- ---------- ---------- -----------
1 14.10.2021 4
1 15.10.2021 3 4
1 16.10.2021 2 3
1 17.10.2021 2 2
1 18.10.2021 3 2
1 19.10.2021 2 3
6 rows selected.
SQL>
How do I get values in Table 1 to Table2; that would duplicate the row based on Table1's column (Orders)
Table1:
ID TICKETID USERNAME FIRSTNAME LASTNAME ORDERS STATUS
SEL00007 Hema1 Hema Sri 3 New
SEL00008 Romi1 Romi T 2 New
Table2:
ID TICKETID USERNAME FIRSTNAME LASTNAME ORDERS STATUS
SEL00007 Hema1 Hema Sri 3 New
SEL00007 Hema1 Hema Sri 3 New
SEL00007 Hema1 Hema Sri 3 New
SEL00008 Romi1 Romi T 2 New
SEL00008 Romi1 Romi T 2 New
This is what I was able to come up with after referring to lot of other posts
CREATE OR REPLACE TRIGGER Duplicate_Rows
AFTER INSERT OR UPDATE ON Table1
REFERENCING NEW AS New OLD AS Old
FOR EACH ROW
DECLARE N_Ord NUMBER;
BEGIN
N_Ord := (SELECT (Orders) from Table2);
INSERT INTO UserTable2 (TICKETID,username,firstName,lastName,Status)
SELECT :New.TICKETID,:New.username,:New.firstName,:New.lastName,:New.Status
FROM dual
CONNECT BY LEVEL <= N_Ord;
END Duplicate_Rows;
/
Please help!!
Close; no need to separately select orders into a variable; it'll fail anyway because table is mutating so trigger can't see it.
Trigger:
SQL> CREATE OR REPLACE TRIGGER trg_ai_t1 AFTER
2 INSERT ON table1
3 FOR EACH ROW
4 BEGIN
5 INSERT INTO table2(
6 id,
7 ticketid,
8 username,
9 firstname,
10 lastname,
11 orders,
12 status
13 )
14 SELECT :new.id,
15 :new.ticketid,
16 :new.username,
17 :new.firstname,
18 :new.lastname,
19 :new.orders,
20 :new.status
21 FROM dual CONNECT BY
22 level <= :new.orders;
23
24 END;
25 /
Trigger created.
Insert into table1:
SQL> INSERT INTO table1(
2 id,
3 ticketid,
4 username,
5 firstname,
6 lastname,
7 orders,
8 status
9 )VALUES(
10 'SEL00007',
11 NULL,
12 'Hema1',
13 'Hema',
14 'Sri',
15 3,
16 'New'
17 );
1 row created.
Result:
SQL> SELECT *
2 FROM table1;
ID TICKETID USERNAME FIRST LASTN ORDERS STATU
---------- ---------- ---------- ----- ----- ---------- -----
SEL00007 Hema1 Hema Sri 3 New
SQL> SELECT *
2 FROM table2;
ID TICKETID USERNAME FIRST LASTN ORDERS STATU
---------- ---------- ---------- ----- ----- ---------- -----
SEL00007 Hema1 Hema Sri 3 New
SEL00007 Hema1 Hema Sri 3 New
SEL00007 Hema1 Hema Sri 3 New
SQL>
I have two tables:
x_products
id name image price
-- ------- ------ -----
1 name1 path 10
2 name2 path 8
3 name3 NULL 7
4 name4 path 10
5 name5 path 5
x_user_products
id userId productId
-- ------ ---------
1 100 1
2 100 2
3 105 1
4 105 3
5 100 5
6 102 2
how to SELECT all product of a user (for example user 100) in x_products?
the result should be like this:
id name image price
-- ------- ------ -----
1 name1 path 10
2 name2 path 8
5 name5 path 5
You are looking for the INNER JOIN keyword which selects records that have matching values in both tables.
The code:
SELECT xp.*
FROM x_products xp
INNER JOIN x_user_products xup ON xp.id = xup.productId
WHERE xup.userId = 100
You appears to want :
select xp.*
from x_products xp
where exists (select 1
from x_user_products xup
where xup.productId = xp.id and xup.userId = 100);
My Order table is
**O_ID Emp_ID Bill**
-------------------------------------------
1 1 20
2 1 20
3 2 10
4 3 20
5 2 10
i want to insert data in Bill table like with respect to EmpID
**Bill_ID Emp_ID TotalBill**
-------------------------------------------
1 1 40
2 2 20
3 3 20
but when new row is added in Order table
**O_ID Emp_ID Bill**
-------------------------------------------
1 1 20
2 1 20
3 2 10
4 3 20
5 2 10
6 3 50
updated Bill table should be like
**Bill_ID Emp_ID TotalBill**
-------------------------------------------
1 1 40
2 2 20
3 3 70
i want Insert OR Update Query.
You first need to check row exists or not, If exists then update or do insert operation.
IF NOT EXISTS ( SELECT 1 FROM Bill WHERE Bill_ID = 1 AND EMP_ID= 1 )
BEGIN
INSERT statement
END
ELSE
BEGIN
UPDATE statement
END
All you need is the simple view with the following
Create view total_bill
as
select emp_id, sum(bill) as bill from Order
group by emp_id