DB script for oracle - database

I have below table structure
To delete the record user will pass the ID, but we have similar text in Data column all the records should be deleted, for example in the above table if users pass 1, we need to delete 3 records (ID 1,2,3), please help for the single delete query.

Use a subquery:
SQL> select * From test;
ID DAT
---------- ---
1 abc
2 abc
3 abc
4 def
5 ijk
6 lmn
6 rows selected.
SQL> delete from test t
2 where t.data = (select t1.data
3 from test t1
4 where t1.id = &id
5 );
Enter value for id: 1
old 4: where t1.id = &id
new 4: where t1.id = 1
3 rows deleted.
SQL> select * from test;
ID DAT
---------- ---
4 def
5 ijk
6 lmn
SQL>

Related

Insert into table with where condition

I have two tables
Table1
OfficeID OfficeName
-------------------
1 UK
2 JP
3 US1
4 US2
5 US3
6 US4
OfficeID is an identity auto increment column.
I need to add couple of more offices (e.g. US5,US6) into table1:
insert into Table1 (OfficeName)
values ('US5'), ('US6')
I have another table2
OrgID OfficeID
----------------
1 1
2 2
3 3
3 4
3 5
3 6
After inserting the US5 and US6 the new data in table 1 will be
OfficeID OfficeName
-------------------
1 UK
2 JP
3 US1
4 US2
5 US3
6 US4
7 US5
8 US6
After this, I would like to insert officeID into table 2 so that my table 2 would look like this:
OrgID OfficeID
----------------
1 1
2 2
3 3
3 4
3 5
3 6
3 7
3 8
Here is how I'm trying to do this
insert into Table2 (OfficeID)
select OfficeID
from table1
where OfficeID in ((7), (8))
and table2.OrgID = 3
How to achieve this? Thanks
You should define all columns you want to insert:
insert into Table2 (OfficeID, OrgID)
select OfficeID, 3 from table1 where OfficeID in ((7),(8))
If you want identity column to be inserted into Table2 try OUTPUT clause
Insert into Table1 (OfficeName)
OUTPUT inserted.OfficeID, 3 INTO Table2 (OfficeID, OrgID)
values
('US5'),
('UK6')
go
try to make a inner join to the table 2 because you are trying to filter by column on table 2 and are inaccesable in the select statement, try this
INSERT INTO Table2 (OfficeID)
SELECT OfficeID FROM table1 INNER JOIN table2 ON (CLAUSE) WHERE table1.OfficeID in ((7),(8))
AND table2.OrgID=3

Update only Top 1 OR anyone row of a table using join with another table

I want to Update only top 1 OR only 1 row of a column where a column values are same.
(Just logical explanation don't go on syntax)
LIKE:
Update [Total] = (value from a another table with a common column)
but need to update only top 1 row OR any one row to the current (updating) table not all rows matching column value...
e.g
Table 1:
Skill Value
abc 3
def 4
xyz 3.5
Table 2:
Name Skill MyValue MyValue2(ColumnNeedsToBeUpdated)
Ram abc 3
shyam abc 4
Mohan abc 5
Raju xyz 4
Ratan xyz 6
Now I want to Update MyValue2 based on Table1 column Skill Value = MyValue2 but I want to update anyone OR top 1 row in Table2 NOT ALL Please help
Expected Output:
Name Skill MyValue MyValue2(ColumnNeedsToBeUpdated)
Ram abc 3 3
shyam abc 4
Mohan abc 5
Raju xyz 4 3.5
Ratan xyz 6
OR Alternate output can be:
Name Skill MyValue MyValue2(ColumnNeedsToBeUpdated)
Ram abc 3 Value from Table1 / no. of records with skill abc (3/3)
shyam abc 4
Mohan abc 5
Raju xyz 4 Value from Table1 / no. of records with skill xyz (3.5/2)
Ratan xyz 6
In Table 2, give a row number based on group by Skill column and order by MyValue column. And then updated the rows which having row number1 with Value from Table 1.
Query
;with cte as(
select [rn] = row_number() over(
partition by Skill
order by [MyValue]
), *
from [Table2]
)
update t1
set t1.[MyValue2] = t2.[Value]
from cte t1
join [Table1] t2
on t1.[Skill] = t2.[Skill]
where t1.[rn] = 1;

Transitive Group Query on 2 Columns in SQL Server

I need help with a transitive query in SQL Server.
I have a table with [ID] and [GRPID].
I would like to update a third column [NEWGRPID] based on the following logic:
For each [ID], get its GRPID;
Get all of the IDs associated with the GRPID from (1);
Set [NEWGRPID] equal to an integer (variable that is incremented by 1), for all of the rows from step (2)
The idea is several of these IDs are "transitively" linked across different [GRPID]s, and should all be having the same [GRPID].
The below table is the expected result, with [NEWGRPID] populated.
ID GRPID NEWGRPID
----- ----- ------
1 345 1
1 777 1
2 777 1
3 345 1
3 777 1
4 345 1
4 999 1
5 345 1
5 877 1
6 999 1
7 877 1
8 555 2
9 555 2
Try this code:
IF OBJECT_ID('tempdb..#tmp') IS NOT NULL
BEGIN
DROP TABLE #tmp;
END;
SELECT GRPID, count (*) AS GRPCNT
INTO #tmp
FROM yourtable
GROUP BY GRPID
UPDATE TGT
SET TGT.NEWGRPID = SRC.GRPCNT
FROM yourtable TGT
JOIN #tmp ON #tmp.GRPID = TGT.GRPID
If the values are likely to change over time you should think about a computed column or a trigger.

Order by Specific id first then By rest

Consider a Sample Table with two Column RoleId and User Name
Role | Name
1 AB
3 A
1 ABC
2 D
2 B
3 Abb
1 E
4 TE
How can i use SQL queries to get following Output.
Role | Name
3 A
3 Abb
1 AB
1 ABC
1 E
2 B
2 D
4 TE
I just want to Order by Role Id 3 first then by remaining Roleid.
Currently i am using Union to achieve so //
SELECT * FROM (SELECT * From #temp
Where roleid=3
UNION ALL
SELECT * From #temp
Where roleid != 3
) as X
You can use case to make more complex ordering:
select *
from #temp
order by case when Role = 3 then 0 else 1 end, Role, Name
select *
from #temp
order by CASE WHEN Role = 3 THEN 0 ELSE Role END, Name
I usually use NULLIF, but case might be faster?
SELECT *
FROM #temp
ORDER BY NULLIF(Role,3), Name

Copying SQL database and fixing foreign keys

I am making an application which optimizes routes (kind of like a VRP). The user must be able to copy all input data, and make changes to this data. This way, they are able to find out what the impact is on the optimization.
Therefore I chose to let them be able to copy all input data, and have sort of like a version management of the data.
I want to copy the following tables:
TableOne
ID UUID WEIGHT CODE
1 abc 15 AB
2 abd 5 AC
TableTwo
ID UUID SIZE TABLE1_FK
1 abe 1 1
2 abf 3 2
The resulting tables (after copying):
TableOne
ID UUID WEIGHT CODE
1 abc 15 AB
2 abd 5 AC
3 abg 15 AB
4 abh 5 AC
TableTwo
ID UUID SIZE TABLE1_FK
1 abe 1 1
2 abf 3 2
3 abi 1 1
4 abj 3 2
Up to this point, I can manage. But now when I want to update the foreign keys of TableTwo to point to the copied lines in TableOne, I get stuck.
I want to do something like:
UPDATE TableOne
SET TABLE1_FK =
(SELECT t.ID
FROM TableOne t
WHERE t.WEIGHT = (SELECT t.WEIGHT
FROM TableOne t1
WHERE ...)
)
And this is where I get stuck.
The wanted result is:
TableTwo
ID UUID SIZE TABLE1_FK
1 abe 1 1
2 abf 3 2
3 abi 1 3
4 abj 3 4
Any suggestions?
After you insert in the first table , select it's ##IDENTITY and use it to insert
in the second table as foreign key.

Resources