I have following table in postgres 11.0
col1
col2
col3
col4
NCT00001723
4894402
xenical (orlistat)capsules
xenical
NCT00001724
4894403
xenical (orlistat)capsules
orlistat
NCT00001725
4894404
capsules
capsules
NCT00001726
4894405
insulin
ins
I would like filter above rows such that either col3 = col4 or col3 exact content should be contained in col4.
The desired output is:
col1
col2
col3
col4
NCT00001723
4894402
xenical (orlistat)capsules
xenical
NCT00001724
4894403
xenical (orlistat)capsules
orlistat
NCT00001725
4894404
capsules
capsules
I am trying below query to get this output.
SELECT
*
FROM
table
where col3 = col4 or --exact match
regexp_matches(col3, '(.*).*\(') = col4 or --match content before brackets
regexp_matches(col3, '.*\(.*\).*') = col4 --match content in brackets
Any suggestions here will be really helpful. Thanks
If I followed you correctly, you could just use word boundaries escape \y:
\y: matches only at the beginning or end of a word
In your query:
select * from mytable where col3 ~ ('\y' || col4 || '\y')
Demo on DB Fiddle:
col1
col2
col3
col4
NCT00001723
4894402
xenical (orlistat)capsules
xenical
NCT00001724
4894403
xenical (orlistat)capsules
orlistat
NCT00001725
4894404
capsules
capsules
Table1 have columns col1 , col2 ,col3 , col4 , col5
Table2 have columns col1 , col3 , col5
I want to insert rows from Table2 to Table1
But col2 , col4 should be NULL datatype after inserting into Table2
How can I do it in HIVE , Currently I am using Hortonworks 3.1 version
You just use insert . . . select:
insert into table1 (col1, col2, col3, col4, col5)
select col1, null, col3, null, col5
from table2;
I have pivot query which will result one row of record. I will have to further filter that one row of record. One row of record has hour based columns and so, i have 24 columns for each hour.
How to pick columns which has only values
Lets say we have 5 columns
Col1 Col2 Col3 Col4 Col5
100 0 0 20 0
Col1, Col4 are eligible. I need total these two columns.
Col1 Col4 Total
100 20 120
create table #t
(
Col0 int,
Col1 int,
Col2 int,
Col3 int,
Col4 int,
Col5 int,
Col6 int,
Col7 int,
Col8 int,
Col9 int,
Col10 int,
Col11 int
)
insert into #t values
(0,100,0,0,0,0,20,10,0,0,0,0)
select * from #t
-- Expected Result
select Col1, Col6, Col7 from #t
You can not do that using a static pivot table, dynamic perhaps. It is not known when a pivot table is defined if all the values are null or not and by the time the data is piped in it is too late. You can either create a reporting project that supports omitting empty column groups or look for a more dynamic query alternative.
This question already has answers here:
Find a string by searching all tables in SQL Server
(12 answers)
Closed 6 years ago.
i have many tables that contains same column i want to search for a value and return all the line for example
Tab1
col1 col2 col3
val1 val2 val3
val7 val8 val9
Tab2
col1 col2 col3
val4 val2 val5
i want the sql syntax that return if i search in my java code for val2 the two lines
Tab1
col1 col2 col3
val1 val2 val3
and
Tab2
col1 col2 col3
val4 val2 val5
thank you
You can try doing a UNION between two queries each of which selects the records you want:
SELECT col1, col2, col3, 'Tab1' AS table_name
FROM Tab1
WHERE col2 = 'val2'
UNION ALL
SELECT col1, col2, col3, 'Tab2'
FROM Tab2
WHERE col2 = 'val2'
If you want to search for val2 in any column, then you can use this:
SELECT col1, col2, col3, 'Tab1' AS table_name
FROM Tab1
WHERE col1 = 'val2' OR col2 = 'val2' OR col3 = 'val2'
UNION ALL
SELECT col1, col2, col3, 'Tab2'
FROM Tab2
WHERE col1 = 'val2' OR col2 = 'val2' OR col3 = 'val2'
you also can try sp_MSforeachtable and temporary table.
CREATE TABLE #tt(col1 INT,col2 INT,col3 int)
INSERT INTO #tt
EXEC sp_MSforeachtable 'select * from ? where col1=''1'' and OBJECT_ID(''?'') in (OBJECT_ID(''table1''),OBJECT_ID(''table1''),OBJECT_ID(''table1'')) '
SELECT * FROM #tt
I am using a RIGHT(LEFT()) method of stripping a string as each character needs to be put into its own holder so I can access it and use it for a report (each character needs to be in its own box for some reason).
There are 16 characters usually but for space and to save repition I've slimmed down the code.
What I am trying to do is put the separated character value into the corresponding column of the temp table - how is this best achieved?
I have no other use for this data once used I'll destroy it.
Code
CREATE table #StringSeparate
(
col1 varchar(1),
col2 varchar(1),
col3 varchar(1),
col4 varchar(1),
col5 varchar(1),
col6 varchar(1),
col7 varchar(1),
col8 varchar(1),
)
declare #string varchar(16)
set #string = 'tpg22052015-1204'
SELECT
LEFT(#string,1),
RIGHT(LEFT(#string,2),1),
RIGHT(LEFT(#string,3),1),
RIGHT(LEFT(#string,4),1),
RIGHT(LEFT(#string,5),1),
RIGHT(LEFT(#string,6),1),
RIGHT(LEFT(#string,7),1),
RIGHT(LEFT(#string,8),1)
INTO
#String Separate
Just do it like:
CREATE table #StringSeparate
(
col1 varchar(1),
col2 varchar(1),
col3 varchar(1),
col4 varchar(1),
col5 varchar(1),
col6 varchar(1),
col7 varchar(1),
col8 varchar(1),
)
INSERT INTO #StringSeparate
SELECT
LEFT(#string,1),
RIGHT(LEFT(#string,2),1),
RIGHT(LEFT(#string,3),1),
RIGHT(LEFT(#string,4),1),
RIGHT(LEFT(#string,5),1),
RIGHT(LEFT(#string,6),1),
RIGHT(LEFT(#string,7),1),
RIGHT(LEFT(#string,8),1)
Or don't create temp table and do this:
SELECT
LEFT(#string,1) col1,
RIGHT(LEFT(#string,2),1) col2,
RIGHT(LEFT(#string,3),1) col3,
RIGHT(LEFT(#string,4),1) col4,
RIGHT(LEFT(#string,5),1) col5,
RIGHT(LEFT(#string,6),1) col6,
RIGHT(LEFT(#string,7),1) col7,
RIGHT(LEFT(#string,8),1) col8
INTO
#StringSeparate
It will automatically create that temp table, because INTO creates table.
Depending on you RDBMS I suppose I might prefer SUBSTRING:
INSERT INTO #StringSeparate
SELECT
LEFT(#string,1),
SUBSTRING(#string,2,1),
SUBSTRING(#string,3,1),
...
RIGHT(#string,1)
I made a big insert of your statement.
INSERT INTO #StringSeparate
VALUES
((LEFT(#string,1)),
(RIGHT(LEFT(#string,2),1)),
(RIGHT(LEFT(#string,3),1)),
(RIGHT(LEFT(#string,4),1)),
(RIGHT(LEFT(#string,5),1)),
(RIGHT(LEFT(#string,6),1)),
(RIGHT(LEFT(#string,7),1)),
(RIGHT(LEFT(#string,8),1)))