postgreSQL: field separated by semicolon [duplicate] - postgresql

This question already has answers here:
how to split a string in a column field value of a table to multiple rows in select query in postgresql
(1 answer)
Postgres split string with double quotes to multiple rows?
(1 answer)
regexp_split_to_table and row_number
(2 answers)
Closed 3 years ago.
I have a field called client_no_all:
client_no_all
14521;555555;636582142;
I want to separate the values and assign them to client_no:
client_no
14521
555555
636582142
Should I use a cursor to loop or pivot?

You can use following query.
The function string_to_array() splits a string by a delimiter into an array.
With the function unnest() you can expand an array to a set of rows.
SELECT unnest(string_to_array('1;2;3;4;5', ';')) AS val

Related

LISTAGG equivalent in PostgreSQL [duplicate]

This question already has answers here:
How to concatenate strings of a string field in a PostgreSQL 'group by' query?
(14 answers)
Postgresql GROUP_CONCAT equivalent?
(9 answers)
What's the equivalent for LISTAGG (Oracle database) in PostgreSQL?
(2 answers)
Closed 2 years ago.
I'm having problems converting this from Oracle to PostgreSQL. I've tried using STRING_AGG, but i'm not having any success. I believe there's also an issue with REGEXP_REPLACE. Can someone help?
REGEXP_REPLACE(
LISTAGG(column_name, ',') WITHIN GROUP (ORDER BY column_name),
'([^,]+)(,\1)*(,|$)',
'\1\3'
)
Perhaps you want something like this:
string_agg(DISTINCT column_name, ',' ORDER BY column_name)

Conditional expression, postgresql, get the result from the case [duplicate]

This question already has answers here:
Why can't I use column aliases in the next SELECT expression?
(4 answers)
Join Alias Columns SQL
(1 answer)
PostgreSQL does not accept column alias in WHERE clause
(1 answer)
Closed 4 years ago.
Looking at conditional expressions in Postgresql. To be more precise, this example.
SELECT * FROM test;
a
---
1
2
3
SELECT a,
CASE WHEN a=1 THEN 'one'
WHEN a=2 THEN 'two'
ELSE 'other'
END AS amod
FROM test;
a | amod
---+-------
1 | one
2 | two
3 | other
I cant seem to find out how to now access the result under amod(one, two and other). After searching around, I haven't been able to find a way to solve it. Any pointers on how to do it?
EDIT: What I'm looking for is to be able to SELECT amod FROM the case expression and get it out as a table.

LEFT JOIN in Postgres [duplicate]

This question already has answers here:
What is the difference between single quotes and double quotes in PostgreSQL?
(3 answers)
Closed 4 years ago.
Ok what's wrong with this SQL query:
select j.* from judge as j
left join user as u on j.user_id = u.id
where u.email="john#example.com";
ERROR: column "john#example.com" does not exist
You are using double quotes where you should actually be using single quotes.
Single quotes denote values, while double quotes represent columns and tables, etc.
You can find out more about the difference here.

Pyspark writing to PostgreSQL using JDBC changes order of date field [duplicate]

This question already has answers here:
Tuples are not inserted sequentially in database table?
(3 answers)
The order of a SQL Select statement without Order By clause
(4 answers)
How keep data don't sort?
(3 answers)
Closed 5 years ago.
I have a PySpark dataframe with a date field and I'm exporting this dataframe to a PostgreSQL database using the JDBC driver. I dataframe has types
df.dtypes
dt: timestamp
value: double
indi: integer
and before writing to PostgreSQL I order the dataframe by date:
df = df.orderBy('dt')
df.write.jdbc(postgre_conn, table='mytable', properties=properties)
but the table is not ordered in PostgreSQL by date. Is there a way to keep the dataframe order in the PostgreSQL table?

Subquery not working with NULL data [duplicate]

This question already has answers here:
How does 'in' clause works in oracle
(5 answers)
NOT IN (subquery) producing zero rows
(2 answers)
SQL "select where not in subquery" returns no results
(12 answers)
Why is nothing selected in this select query?
(5 answers)
Correct way to use "NOT IN" Postgres
(4 answers)
Closed 5 years ago.
Today I have seen one weird issue in PostgreSQL query. In which I have 2 tables Products and Parameters.
Purpose:
It's very simple query, I just want to list all products which are not
there in parameter table.
Query:
select
id
from product_proudct
where active=True and
id not in
(
select distinct product_id from parameter_view
)
Issue:
It won't list anything if there is null value in
parameter table in product_id column. I have verified by just removing null record from the parameter table by adding where clause.
select distinct product_id from parameter_view where product_id is not null
And then it's working fine but with null it won't work, it's really
strange for me.
I would like to know the reason why subquery not working well with null ?