How to Match a string against patterns in Oracle SQL - sql

I have a bit of different scenario. there is one column (Oracle table) in the table which stores patterns.another column with unique id.
Now, i have to match those patterns against a string and have to find out which patterns are matching that string.then i have to pick out those matched patterns along with the ids
Can anybody guide me on how to efficiently do it?
Sample Data
Table 1
-------
Column1 Column2
1 AB%
2 A%
3 %c%
Now, there is a string comes like ABC (take it as an item number. It gets inserted in DB and then a trigger fires that has to do the rest of the job as provided in sample below)
Table 2
---------
Column1 Column2
ABC AB%,A%
or more efficient(desired) Table 2 would be like -
Table 2(desired)
---------
Column1 Column2
ABC 1,2
This is the desired result.

In Oracle 11g you could use function listagg
and simple before insert or update trigger:
Sample data:
create table table1 (column1 number(3), column2 varchar2(10));
insert into table1 values (1, 'AB%');
insert into table1 values (2, 'A%');
insert into table1 values (3, '%c%');
create table table2 (column1 varchar2(10), column2 varchar2(500));
Trigger:
create or replace trigger tg_table2_ins
before insert or update of column1 on table2 for each row
declare
v_list table2.column2%type;
begin
select listagg(t1.column1, ', ') within group (order by t1.column1)
into v_list from table1 t1
where :new.column1 like t1.column2;
:new.column2 := v_list;
end tg_table2_ins;
Test:
insert into table2 (column1) values ('ABC');
insert into table2 (column1) values ('Oracle');
insert into table2 (column1) values ('XYZ');
insert into table2 (column1) values ('Ascii');
select * from table2;
COLUMN1 COLUMN2
---------- ----------
ABC 1, 2
Oracle 3
XYZ
Ascii 2, 3

Related

Inserting records in one column as different rows in table1 from different columns in one row of table2 in sql-server

I have below tables:
table1 #TempImagepath
column1 Path nvarchare(800)
table2 SiteImage
column1 SiteID bigint,
column2 Facebookurl nvarchare(800),
column3 Twitterurl nvarchare(800),
column4 Instaurl nvarchare(800)
I want to insert data from table2 as different rows into table1 for (Facebookurl,Twitterurl,Instaurl) Where SiteID='10'
Lets say there is one record in table2(SiteImage) as:
(10,"/uploads/Sites/1/CategoryImages/WebImages/7ec79e1a-92c2-4d7c-9139-6d177004d766-201701311804409066.jpg","/uploads/Sites/1/CategoryImages/MobileImages/e5ae525f-7dcf-4051-8463-6bb15f520860-201701311804425434.jpg","/uploads/Sites/1/CategoryImages/MobileImages/31d89a5e-5593-4074-881f-d3326b5cf105-201701311804444181.jpg")
Then my result shoul give records for table1(#TempImagepath) Something Like:
"/uploads/Sites/1/CategoryImages/WebImages/7ec79e1a-92c2-4d7c-9139-6d177004d766-201701311804409066.jpg"
"/uploads/Sites/1/CategoryImages/MobileImages/e5ae525f-7dcf-4051-8463-6bb15f520860-201701311804425434.jpg"
"/uploads/Sites/1/CategoryImages/MobileImages/31d89a5e-5593-4074-881f-d3326b5cf105-201701311804444181.jpg"
Try this,i think this might be useful to you
IF OBJECT_ID('Tempdb..#TempImagepath')IS NOT NULL
DROP TABLE #TempImagepath
IF OBJECT_ID('dbo.SiteImage')IS NOT NULL
DROP TABLE SiteImage
CREATE TABLE #TempImagepath
([Path] nvarchar(800))
CREATE TABLE SiteImage
(
SiteID bigint IDENTITY,
Facebookurl nvarchar(800),
Twitterurl nvarchar(800),
Instaurl nvarchar(800)
)
INSERT INTO SiteImage
SELECT 'Facebookurl','Twitterurl','Instaur'
INSERT INTO #TempImagepath
SELECT 'Row'+ CAST(ROW_NUMBER()OVER(ORDER BY (SELECT 1))AS Varchar(10))+': '+ [Path]
FROM SiteImage
CROSS APPLY (VALUES (Facebookurl),(Twitterurl),(Instaurl)
)AS A ([Path])
SELECT * FROM #TempImagepath
Result
Path
------------------
Row1: Facebookurl
Row2: Twitterurl
Row3: Instaur

SQL Server: How to append a string to a value in a virtual column

I am creating a virtual column in a SQL Server query named TempField.
I would like each value in the virtual column to have the same value as the field1 column PLUS append a _suffix string (which is constant/same for all values).
Said another way, the XXX represents a string from field1 and _suffix is a string that I would like to append to XXX (the appended string is the same for all values).
SELECT field1, field2, 'XXX_suffix' as TempField
FROM table1
declare #temp as table (FName varchar(100) , LName varchar(100), Email varchar(100) )
insert into #temp (FName,LName,Email) values ('A','B','C')
insert into #temp (FName,LName,Email) values ('A1','B1','C1')
insert into #temp (FName,LName,Email) values ('A2','B2','C2')
insert into #temp (FName,LName,Email) values ('A','B','C')
insert into #temp (FName,LName,Email) values ('A1','B1','C1')
insert into #temp (FName,LName,Email) values ('A1','B1','C2')
select FName,LName,Email, (FName +'_suffix') as NewColumn
from #temp
If you mean a computed column, then this will do that for you:
create table test(
column1 varchar(20),
column2 as column1 + '_suffix' -- this is calculated when needed and not stored
);
insert test (column1) values ('adam'),('burt');
select * from test;
-- result:
column1 column2
------- -----------
adam adam_suffix
burt burt_suffix

Insert 1000 rows in single sql statment or procedure

How should i write a single sql statement or a stored procedure,
To insert 1000 values in 1000 rows and same column with each column having different values (among those 1000)
Here is the query i wrote,
INSERT INTO a_b values
(
(SELECT max(a_b_id) + 1 from a_b),
1111,
(SELECT s_id FROM a_b WHERE s_id in ('0','1','2','3','4')),
0,
1,
sysdate,
sysdate,
0,
1,
null
);
like say, i have 1000 s_id's i want select them one by one and insert them in one particular column, each time creating a new row.
EX, in first row s_id should be 0 then in second row it should be 1 like that goes on till thousand, attached an image of sample database i am working with.
You can use connect by for this:
INSERT INTO a_b (s_id, col2, col3, ....)
select level, --<< this value will change for every row
1111,
sysdate,
... more columns ...
from dual
connect by level <= 1000;
you can use cross apply to get 1000 rows along with 1000 other columns to insert 1000 rows as below:
insert into a_b (column names...)
select (max(a_b_id) over()) +1 as MaxId, s_id from a_b a cross apply (select 0, 1,SYSDATETIME, SYSDATETIME, 0, 1, null) b where a.s_id('111','222')--condition
The below is a syntax error. You will never get something like that to work.
create table fff
( id int not null
);
insert fff values (select 1,7777,select 3, select 3);
So you need to break it up into chunks
DROP PROCEDURE IF EXISTS uspRunMe;
DELIMITER $$
CREATE PROCEDURE uspRunMe()
BEGIN
insert into a_b select max(a_b_id) + 1 from a_b;
insert into a_b values (1111);
insert into a_b SELECT s_id FROM a_b WHERE s_id in ('0','1','2','3','4');
insert into a_b values (0,1);
insert into a_b select sysdate,sysdate;
insert into a_b values (0,1,null);
END;$$
DELIMITER ;
Test it:
call uspRunMe();
The above is for MySQL. You have a few db engines tagged here.

combining resultset of many select queries

I have four Select queries for four different tables, each extracting only one record. For example:
Select * from table where col1 = 'something'
gives one row having 3 columns.
The second select query also gives one record having two columns(fields). Same for third and fourth select query.
I want to combine all four result sets into one having one row. How is it possible?
I will write the queries for you.
1st one:
Select Top 1 column1, column2
from table 1
where column 1 = 'something'
and col1 = (Select max(col1) where column 1 = 'something')
2nd query:
Select Top 1 column1, column3
from table 2
where column 1 = 'something'
and column3 = (Select max(column3) where column 1 = 'something')
3rd query uses the result obtained from query 2:
Select column4
from table 3
where column3 = (obtained from 2nd query) (there is only one row)
4th:
Select column5
from table 4
where column3 = (obtained from 2nd query) (there is only one row)
This means I have to join 2nd, 3rd, 4th query, then resulting set in 1st.
I can't use union since columns are different.
So only problem is with joining the result set.
You can use CROSS JOINs to accomplish this.
CREATE TABLE table1 (id int, column1 varchar(5), column2 varchar(15));
CREATE TABLE table2 (column3 varchar(5), column4 varchar(15));
CREATE TABLE table3 (id int, column5 varchar(5), column6 varchar(15));
INSERT INTO table1 VALUES (1, 'aaa', 'row1')
INSERT INTO table2 VALUES ('bbb', 'table2')
INSERT INTO table3 VALUES (1, 'ccc', 'table3')
INSERT INTO table1 VALUES (1, 'ddd', 'table1')
SELECT * FROM (SELECT * FROM table1) a
CROSS JOIN (SELECT * FROM table2) b
CROSS JOIN (SELECT * FROM table3) c
Result:
id column1 column2 column3 column4 id column5 column6
1 aaa row1 bbb table2 1 ccc table3
1 ddd table1 bbb table2 1 ccc table3
Update after clarification:
CREATE TABLE table1
(
id int IDENTITY(1,1)
, searchstring nvarchar(25)
);
CREATE TABLE table2
(
id2 int IDENTITY(10, 10)
, searchstring2 nvarchar(25)
, newsearchstring nvarchar(50)
);
CREATE TABLE table3
(
id3 int IDENTITY(100, 100)
, id2 int
, table3srow nvarchar(25)
)
INSERT INTO table1 VALUES ('something');
INSERT INTO table1 VALUES ('something else');
INSERT INTO table1 VALUES ('something'); -- ID = 3, this row will be selected by 1st query
INSERT INTO table2 VALUES ('something', 'newvalue1');
INSERT INTO table2 VALUES ('something else', 'this will not be shown');
INSERT INTO table2 VALUES ('something', 'this will be returned by query 2'); -- ID = 30, this row will be selected by 2nd query
INSERT INTO table3 VALUES (10, 'not relevant');
INSERT INTO table3 VALUES (20, 'not relevant');
INSERT INTO table3 VALUES (30, 'This is from table 3'); -- This row will be returned by 3rd query
SELECT * FROM
(SELECT TOP 1 id, searchstring FROM table1 WHERE searchstring = 'something' and id = (SELECT MAX(id) FROM table1 WHERE searchstring = 'something')) AS query1,
(SELECT TOP 1 id2, newsearchstring FROM table2 WHERE searchstring2 = 'something' and id2 = (SELECT MAX(id2) FROM table2 WHERE searchstring2 = 'something')) AS query2,
(SELECT id2, table3srow FROM table3) as query3
WHERE query3.id2 = query2.id2
Use the same approach for table4 as indicated for table3.

SQL to find rows where two columns have the same value

I have 3 columns in Oracle database having table mytable and i want records having only duplicate values in 2nd and 3rd column.
SQL> select * from mytable ;
column1 column2 column3
A 50 50----required output
A 10 20----have different values i.e. 10 and 20
A 50 50----required output
A 30 70----have different values i.e. 30 and 70
B 20 20----required output
B 40 30----have different values i.e. 40 and 30
I want the following output with count(*):
column1 column2 column3
A 50 50
A 50 50
B 20 20
Any help is much appreciated
select column1, count (*)
from mytable
where column2 = column3
group by column1, column2;
From your question it is not clear about primary key as A in First Column is being repeated many times.
You can try the following:
select column1, column2, column3, count(*) from
mytable where column2 = column3 group by column1, column2, column3;
Here are sample example , i am doing this SQL Server but i am sure this query work in ORACLE also
EXAMPLE :
Create table #Test (colA int not null, colB int not null, colC int not null, id int not null identity) on [Primary]
GO
INSERT INTO #Test (colA,colB,colC) VALUES (1,1,1)
INSERT INTO #Test (colA,colB,colC) VALUES (1,1,1)
INSERT INTO #Test (colA,colB,colC) VALUES (1,1,1)
INSERT INTO #Test (colA,colB,colC) VALUES (1,2,3)
INSERT INTO #Test (colA,colB,colC) VALUES (1,2,3)
INSERT INTO #Test (colA,colB,colC) VALUES (1,2,3)
INSERT INTO #Test (colA,colB,colC) VALUES (4,5,6)
GO
Select * from #Test
GO
select count(colA) as tot_duplicate_count , colA ,colB ,colC from #Test where id <=
(Select Max(id) from #Test t where #Test.colA = t.colA and
#Test.colB = t.colB and
#Test.colC = t.colC)
group by colA ,colB ,colC
having count(colA) > 1
This query this total count of duplicate record per data row