using comparision operator ALL() in DB2 - db2

I simple need some values depending on exactly two values , it's very simple but I don't know how to use it in DB2.
For example:
col1 col2
123 abc
123 def
124 ghi
123 ghj
select col1 from table where col2 =ALL('abc','def')

You're using ALL wrong...
I think you want IN
select col1 from table where col2 IN ('abc','def')

Related

how to get the index of an element in pyspark with the help of the second column?

how to use array_position to get the right index with the help of the second column??
example:
col1 col2
['apple','sib','mive','banana'] 'sib'
['jasmin','df','nagh'] 'nagh'
['jk'] na
output
col1 col2 index
['apple','sib','mive','banana'] 'sib' 1
['jasmin','df','nagh'] 'nagh' 2
['jk'] na na
You can use the expr function to achieve it.
df = df.withColumn('index', F.expr('array_position(col1, col2) - 1'))

How can I split a string to x number columns and y number of rows?

I have a string looks like 'ab bc 123 cd de ef 232' and I need to split this to look like :
col1
col2
col3
ab
bc
123
cd de
ef
232
Numbers have to be in the first column, the last string before the numbers has to be in the second column and all characters before that has to be in the first column.
I am working on PostreSQL and have no idea how to do that
step-by-step demo: db<>fiddle
You can use regular expressions to split your strings:
regexp_match(mystring,'^(.+)\s(.+)\s(\d+)\s(.+)\s(.+)\s(\d+)$')
(see how the RegExp works: demo: regex101)
This results in an array of strings you expect. This array can be used to fill your table:
WITH textblocks AS ( -- 1
SELECT
regexp_match(mystring,'^(.+)\s(.+)\s(\d+)\s(.+)\s(.+)\s(\d+)$') AS r
FROM mytable1
)
INSERT INTO mytable2 (col1, col2, col3)
SELECT
r[1], r[2], r[3]
FROM textblocks
UNION -- 2
SELECT
r[4], r[5], r[6]
FROM textblocks
Execute the RegExp which splits the original string into a text array
Create two records from the text array and insert it into your table

DB2 - Concat all values in a column into a Single string

Let's say I have a table like this:
test
Col1
Col2
A
1
B
1
C
1
D
2
I am doing query select col1 from test where col2 = 1;
This will return a column with values A B and C in 3 separate rows.
I want the SQL to return a single row with value A|B|C. Is this possible to do? If it is how should I do it?
you can use LISTAGG function like this:
SELECT LISTAGG(col1, ',')
If LISTAGG is not available, it can be reproduced with XMLAGG:
SELECT SUBSTR(XMLSERIALIZE(XMLAGG(XMLTEXT('|'||"Col1"))),2)
FROM "test"
WHERE "Col2" = 1

postgresql - unnest, for each result

I would like to create a view and I do some data handling for each result of unnest function.
In my column column2 I have :
"12345"
"123456"
"12345,123456" or more number
I would like to do some unnest(col2, ',') and for each result do something like this:
if length(col2) = 5 then treatment_1
else if length(col2) = 6 then treatment_2
Example in table:
col1 col2
-------------------
D1 12345, 123456
D3 12345
D4 123456
Expected Result into a view (with a treatment for each row in a col2) :
col1 col2
-------------------
D1 12345
D1 123456
D3 12345
D4 123456
You can use regexp_split_to_table to split the string into multiple rows:
select t1.col1
, case
when length(split.col2) > 5 then right(split.col2, 3)
else replace(split.col2, '123', '***')
end as col2
from Table1 t1
cross join
regexp_split_to_table(t1.col2, '\s*,\s*') split(col2)
Working example at SQL Fiddle.

PostgreSQL 9.3: Split one column into multiple

I want to split one column that is colb in the given below example into two columns
like column1 and column2.
I have a table with two columns:
Example:
create table t3
(
cola varchar,
colb varchar
);
Insertion:
insert into t3 values('D1','2021to123'),
('D2','112to24201'),
('D3','51to201');
I want to split the colb values into two columns like the following expected result:
Expected Result:
cola column1 column2
---------------------------------
D1 2021 123
D2 112 24201
D3 51 201
select cola
,split_part(colb, 'to', 1) col1
,split_part(colb, 'to', 2) col2
from t3
Quoted from the PostgreSQL Documentation:
split_part(string text, delimiter text, field int)
Split string on delimiter and return the given field (counting from
one)