Update query in oracle sql - sql

create table table1(accountno number);
insert into table1 values (1);
insert into table1 values (2);
insert into table1 values (3);
insert into table1 values (4);
insert into table1 values (5);
insert into table1 values (6);
create table table2(accountno number,check_y_n varchar2(20));
insert into table2 (accountno) values (4);
insert into table2 (accountno) values (5);
insert into table2 (accountno) values (6);
insert into table2 (accountno) values (7);
insert into table2 (accountno) values (8);
insert into table2 (accountno) values (9);
I need below two update query in single query using joins. Can anyone help me on this
UPDATE TABLE2 SET check_y_n ='YES' WHERE accountno IN (SELECT accountno FROM TABLE1);
UPDATE TABLE2 SET check_y_n ='NO' WHERE accountno NOT IN (SELECT accountno FROM TABLE1);

Use a CASE expression:
UPDATE TABLE2
SET check_y_n = CASE
WHEN accountno IN (SELECT accountno FROM TABLE1)
THEN 'YES'
ELSE 'NO'
END;
db<>fiddle here

Related

SQL to Mongo query

I am looking to fetch the records from my mongo db which can be extracted from MySQL using a query like this:
select * from tableA where col1 between
(select col1 from tableA where col1 >= 0 and col2 = 'A001') and
(select col1 from tableA where col1 <= 1000 and col2 = 'A008')
In other words, I need to find the data between the records which match the queries for between.
Dataset:
create table tableA (
col1 int,
col2 varchar(255)
);
insert into tableA values(1, "A001");
insert into tableA values(2, "A002");
insert into tableA values(3, "A003");
insert into tableA values(4, "A004");
insert into tableA values(5, "A005");
insert into tableA values(6, "A006");
insert into tableA values(7, "A007");
insert into tableA values(8, "A008");
(Above query tested on MariaDB database)

Removing count column from query output

select top (5) t3.Model, t3.Manufacturer, t1.Colour, t1.RegistrationNumber, t1.DailyRentalPrice, count(t2.TruckID) as RentedAmount
from [IndividualTruck-PB] t1
inner join [TruckRental-PB] t2 on t1.TruckID = t2.TruckID
inner join [TruckModel-PB] t3 on t1.TruckModelID = t3.ModelID
group by t3.Model, t3.Manufacturer, t1.Colour, t1.RegistrationNumber, t1.DailyRentalPrice
order by RentedAmount desc
Bsically, I'm trying to get the top 5 most rented but don't want the actual count column as output only as a means of ordering the output. Is this possible?
You can try remove the count column and give the formula to order by part:
select top (5) t3.Model, t3.Manufacturer, t1.Colour, t1.RegistrationNumber, t1.DailyRentalPrice
from [IndividualTruck-PB] t1
inner join [TruckRental-PB] t2 on t1.TruckID = t2.TruckID
inner join [TruckModel-PB] t3 on t1.TruckModelID = t3.ModelID
group by t3.Model, t3.Manufacturer, t1.Colour, t1.RegistrationNumber, t1.DailyRentalPrice
order by count(t2.TruckID) desc
My test:
create table A (
col1 varchar(255)
);
insert into A (col1) values ('A');
insert into A (col1) values ('A');
insert into A(col1) values ('A');
insert into A(col1) values ('A');
insert into A(col1) values ('A');
insert into A(col1) values ('A');
insert into A(col1) values ('A');
insert into A(col1) values ('B');
insert into A(col1) values ('B');
insert into A(col1) values ('B');
insert into A(col1) values ('B');
insert into A(col1) values ('B');
insert into A(col1) values ('B');
insert into A(col1) values ('C');
insert into A(col1) values ('C');
insert into A(col1) values ('C');
insert into A(col1) values ('C');
insert into A(col1) values ('C');
insert into A(col1) values ('D');
insert into A(col1) values ('D');
insert into A(col1) values ('D');
insert into A(col1) values ('D');
insert into A(col1) values ('D');
insert into A(col1) values ('E');
insert into A(col1) values ('E');
insert into A(col1) values ('E');
Select for MS SQL Server 2017:
select top(2) col1 from A group by col1 order by count(col1) desc;
Output:
col1
A
B

How do you count the number of rows in a table that have the same value in value in a column?

Suppose I have the following simple table:
create table mytable (
desid bigint not null,
ancid bigint not null
);
insert into mytable (ancid,desid) values (1,10);
insert into mytable (ancid,desid) values (1,20);
insert into mytable (ancid,desid) values (1,21);
insert into mytable (ancid,desid) values (1,22);
insert into mytable (ancid,desid) values (2,30);
insert into mytable (ancid,desid) values (3,40);
insert into mytable (ancid,desid) values (3,41);
insert into mytable (ancid,desid) values (3,42);
insert into mytable (ancid,desid) values (3,43);
What is the SQL command that will print out the following information:
4 rows with ancid=1
1 rows with ancid=2
4 rows with ancid=3
You can get the information by grouping by the ancid:
SELECT ancid, COUNT(*)
FROM mytable
GROUP BY ancid

Output elements between specific element

Suppose I have a table with a column which looks like:
SELECT Col1
FROM table;
Col1
A
A
B
B
C
C
D
B
E
B
F
I would like to output elements that are between "B"s, which are C, D, E
How can I do that with a query?
declare #t table (ID INT IDENTITY(1,1),col1 VARCHAR(10))
insert into #t (col1) values ('A')
insert into #t (col1) values ('A')
insert into #t (col1) values ('B')
insert into #t (col1) values ('B')
insert into #t (col1) values ('C')
insert into #t (col1) values ('C')
insert into #t (col1) values ('B')
insert into #t (col1) values ('E')
insert into #t (col1) values ('B')
insert into #t (col1) values ('F')
select ID,col1 from #t
where ID between (select MIN(id) from #t WHERE col1 = 'B') and
(select MAX(id) from #t WHERE col1 = 'B')
and col1<>'B'

Updated SQL Conditional INSERT

I have a situation where I need to insert data from table1 to table2. Before insert check if a certain row already exist in the table2, if it does then just update col2, col4 of the row. If it doesn't exist then insert a new row.
I am using SQLSERVER 2008 R2. How could I achieve this?
The situation has changed a bit now. I need something like this.
DECLARE #table1 TABLE
(id int not null, ahccs int not null, info varchar(25), flag varchar(2))
DECLARE #table2 TABLE
(id int not null, ahccs int not null, info varchar(25), flag varchar(2))
INSERT INTO #table1
VALUES(1, 1223, 'et', 'X')
INSERT INTO #table1
VALUES(2, 321, 'et', 'X')
INSERT INTO #table1
VALUES(3, 134, 'et', 'X' )
INSERT INTO #table1
VALUES(4, 168, 'et', 'X' )
INSERT INTO #table1
VALUES(5, 123, 'et', 'X' )
INSERT INTO #table2
VALUES(1, 1223, 'dt', 'y' )
INSERT INTO #table2
VALUES(2, 456, 'dt', 'y' )
INSERT INTO #table2
VALUES(3, 123, 'dt', 'y' )
INSERT INTO #table2
VALUES(4, 193, 'dt', 'y' )
--SELECT * FROM #table1
SELECT * FROM #table2
MERGE
INTO #table2 t2
USING #table1 t1
ON t2.id = t1.id or t2.ahccs = t1.ahccs
WHEN NOT MATCHED THEN
UPDATE
SET flag = 'z'
INSERT VALUES (100, t1.ahccs, t1.info, 'l');
The two issues I am having are:
1) Merge doesn't support multiple steps, I believe.
2) Update is not allowed in WHEN NOT MATCHED case.
Please advise.
Thank You.
You need to use merge, it lets you match the data that you are trying to update or insert ("upsert") against the data that is currently in the table, and perform different actions based on the presence or absence of a match.
MERGE Stock S
USING Trades T ON S.Stock = T.Stock
WHEN MATCHED THEN
UPDATE SET Qty += Delta
WHEN NOT MATCHED THEN
INSERT VALUES (Stock, Delta);
This example is from here.
MERGE
INTO table2 t2
USING table1 t1
ON t2.id = t1.t2_id
WHEN NOT MATCHED THEN
INSERT
VALUES (t1.col1, t1.col2, ...)
WHEN MATCHED THEN
UPDATE
SET col2 = t1.col2,
col4 = t1.col4