A regular expression to check if a column contains another column value - sql

I have a specific scenarios which is stated like below :
Table T1 contains Name and status. Table T2 contains column Name_status. status will have values of pass and fail. Name_status should have values like <Name>_<status>ed.
Can we think of a regular expression which would fetch all the values in <Name>_<status>ed format.
Any help would be appreciated. Thanks

You may try this
SELECT *
FROM T1, T2
WHERE REGEXP_REPLACE(T2.Name_Status, '^(.+?)_(.+?)ed$', '\1') = T1.name
AND REGEXP_REPLACE(T2.Name_Status, '^(.+?)_(.+?)ed$', '\2') = T1.status

This would work out fine
SELECT name_status
FROM(SELECT * FROM tbl1, tbl2 WHERE tbl2.name_status
REGEXP concat(tbl1.name,"_",tbl1.status)) as temp

Related

Query not reading the quoted string values stored in the table

I have stored some quoted values in a separate table and based on the value in this table. I am trying to filter the rows in another table
by using the values in this table in a subquery. But it is not reading the values for the subquery and returns a blank table in output.
The value is in column override and resolves to 'HCC11','HCC12'.
When I just copy the value from the column and paste it in place of the subquery it is fetching the data correctly. I am not able to understand the issue here. I have tried using the trim() function here but still its not working
Note-: I have attached the pic for your reference:
select *
from table1
where column1 in (select override from table 2 )
Storing comma separated values in a single column is a really poor database to begin with enclosing them in quotes makes things even wors. The proper solution to your problem is a better design.
However, if you are forced to work with that bad design, you can convert them to a proper list of values using
select *
from table1
where column1 in (select trim(both '''' from w.word)
from table2 t2
cross join unnest(string_to_array(t2.override, ',')) as w(word)
This assumes that table1.column1 only contains a single value without any quotes and that the override values never contain a comma in the real value (e.g. the above would break on a value like 'A,B', 'C')
You have the override column value as 'HCC11','HCC12' which can not match with single value 'HCC11'. You should better use the LIKE operator as follows:
select * from table1 t1
where exists
(select 1 from table2 t2
where t2.override like concat('%''', t1.column1, '''%'));
According to your image, the value of table1.column1 has to be 'HCC11','HCC12' (one string) to get the match from subquery.
If the table1 has 2 rows with values HCC11 and HCC12 then you might use the exists keyword in your subquery.
Something like
select *
from table1 t1
where exists
(select 1
from table2 t2
where instr( t2.override, concat("'",t1.column1,"'") ) >=1
);
You can do this like -
1.
select * from table1
where column1 in
(select regexp_replace(unnest(string_to_array(override, ',')),'''', '', 'g') from table2)
Or
2.
select * from table1
where '''' || column1 || '''' in
(select unnest(string_to_array(override, ',')) from table2)
Although, I would just recommend not storing your data like this, since you want to query using it.

Is there a way to query a specific data point if it exists, otherwise query everything else?

Say I have a table with a column called "Names" and with values "Mike", "John", "Kelly", and "Tina". Every day the values might change.
How would I structure the query so that if the table has the name "Tina", it only displays "Tina", but if it doesn't contain "Tina", it'll display everything else?
Another option to consider (BigQuery Standard SQL)
#standardSQL
SELECT * EXCEPT(flag) FROM (
SELECT *, names = 'Tina' OR COUNTIF(names = 'Tina') OVER() = 0 AS flag
FROM `project.dataset.table`
)
WHERE flag
I would expected this version is significantly cheaper than another one with implicit join
One option is union all ad not exists:
select name from mytable where name = 'Tina'
union all
select t.name from mytable t where not exists (select 1 from mytable t1 where t1.name = 'Tina')

oracle sql get multiple rows want to set into one single row

I get the following error: ORA-01427: single-row subquery returns more than one row
SQL:
SELECT t.test
FROM device t
JOIN device_interface v on t.pe = v.end_test
WHERE v.test = upper('IE5656') and t.device_type like '%tin%'
Output:
T.TEST:
XEM5454
XEM7646
I would like to display it like this in order to not get an error :
T.TEST:
XEM5454 | XEM7646
OR
T.TEST:
XEM5454, XEM7646
What need to be consider into the sql in order to display it like the example above?
Thank yo uso much for your help and support.
Input -
CREATE TABLE tabl1
(COL1 varchar2(7))
;
INSERT ALL
INTO tabl1 (COL1)
VALUES ('XEM5454')
INTO tabl1 (COL1)
VALUES ('XEM7646')
SELECT * FROM dual
;
Query -
select listagg(col1,',')WITHIN GROUP (ORDER BY col1) as value
FROM tabl1;
Output -
VALUE
XEM5454,XEM7646

If condition in SQL file vertica

I need to execute insertions for around 10 tables, before inserting I have to check for a condition, condition remains the same for each of the tables, instead of giving that condition within insert query, I wish I could give in if condition (a select query), if satisfied then execute insert statements, is there a way to give if condition in Vertica SQL file ? If condition is not satisfied I dont want to execute any of the insert queries.
If the condition is, for example, that you only insert the data on a Sunday, try this:
a) a test table:
CREATE LOCAL TEMPORARY TABLE input(id,name)
ON COMMIT PRESERVE ROWS AS
SELECT 42,'Arthur Dent'
UNION ALL SELECT 43,'Ford Prefect'
UNION ALL SELECT 44,'Tricia McMillan'
KSAFE 0;
From this table, select with a WHERE condition that tests whether it's sunday -- that's all, see here:
SELECT
*
FROM input
WHERE TRIM(TO_CHAR(CURRENT_DATE,'Day'))='Sunday'
;
id|name
42|Arthur Dent
43|Ford Prefect
44|Tricia McMillan
With a different value for the week day (I'm writing this on a Sunday...), you get this:
SELECT
*
FROM input
WHERE TRIM(TO_CHAR(CURRENT_DATE,'Day'))='Monday'
;
id|name
select succeeded; 0 rows fetched
I use this technique in SQL generating SQL to create a script or an empty file determining on circumstances, and then to call that script (full or empty), implementing a conditional SQL execution that way....
I know it is an old post, but i want to share what i recently solved my problem.
I need to insert in one table or another based on some condition. You first should have a field or value what will be your search condition.
create table tmp1 (
Col1 int null
,Col2 varchar(100) null
)
--Insert values
insert into tmp (Col1,Col2) Values
(1,'Text1')
,(2,'Text2')
--Insert into table001
insert into table001
select
t.field1
,t.field2
,......
from table1 t
inner join tmp t2
on t2.col1 = t.ColX
where 1 = case when t2.Col2 = 'Text1' then 1 else 0 end --Search condition; if 1<>0 then it doesn't do anything; otherwise insert.
--Insert into table002
insert into table002
select
t.field1
,t.field2
,......
from table2 t
inner join tmp t2
on t2.col1 = t.ColX
where 1 = case when t2.Col2 = 'Text2' then 1 else 0 end --Search condition; if 1<>0 then it doesn't do anything; otherwise insert.
Or you can use an UNION/UNION ALL based on this if it is the same working table.
Regards!

How to verify if two queries contain exact same data

I have a table that maintains a "Gold Standard" set of data that another table should match if the table was processed correctly.
Both of these tables have almost 1,000,000 records of data.
For example. I have table (table1) that have PrimaryKey1, ColumnA, ColumnB, ColumnC, ColumnD, and Column E.
I have another table (table2) with ForeignKey1, ColumnF, ColumnG, ColumnH, ColumnI, ColumnJ.
I need to check that all the data in these two table are exactly the same except for a few columns.
What I mean by that is that ColumnA from table1 has to have all of the same as columnF in table2, and ColumnC from table1 has to matchup with ColumnI from table2 FOR THE SAME RECORD (lets call this primaryKey1). The other columns in the table do not matter.
Also, if there is a mismatch between the datasets, I need to know where the mismatch is.
I think your best bet is SUBSTRACT(). Select x, y, z from A substract select x,y,z from B. If it returns nothing, you're good to go.
Hope this helps!
A quick trick that I use is just comparing row counts. This will at least show you if you have a problem (it won't show you where the problem is).
A union query can join two queries together and display the combined result. Common rows are treated as 1 row. So, if the first query returns exactly 1 million rows, the UNION query (both queries combined) should return exactly 1 million rows. If it doesn't there is a problem.
select ColumnA 'Col1'
, ColumnC 'Col2'
from Table1
UNION
select ColumnF 'Col1'
, ColumnI 'Col2'
from TableB
Something like
select
*
from
gold_copy a
join my_copy b on a.primary_key = b.primary_key
and
a.field1 <> b.field1
or a.field_a <> b.field_f
or a.field_c <> b.field_i
or a.field_x <> b.field_y
I think the following will helps you to get the unmatched records.
select * from table1 where not exists (select * from table2);
so instead of all columns you can check with the columns what you need from the two tables,but i think the column names should be same.
Thank you.
You could use symetric difference for this
(select 'table1', col
from table1
UNION ALL
select 'table2', col
from table2)
EXCEPT
(select 'table1', col
from table1
INTERSECT
select 'table2', col
from table2)
This query returns only those rows that are only in one table and it says in which table it was found