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

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.

Related

How to use LIKE in these cases? [duplicate]

This question already has answers here:
Regular expression in PostgreSQL LIKE clause
(2 answers)
Closed 10 months ago.
There is a list of countries.
I want to get the list of the countries that:
consist of the letter U and A -or- S
consist of the letter U and any character except S.
These structures are correct for the MS SQL but don't work for Postgres
For MS SQL Server:
SELECT
country
FROM world.country
WHERE country LIKE 'U[AS]%'
SELECT
country
FROM world.country
WHERE country LIKE 'U[S]%'
For Postgres?
You can use SIMILAR TO Documentation
select * from (values ('UAA'),('MUA'),('UUA'),('USA')) as test (country)
where country SIMILAR TO 'U(A|S)%'
returns 'UAA' and 'USA'
You can use also POSIX regular expression with the same result.
select * from (values ('UAA'),('MUA'),('UUA'),('USA')) as test (country)
where country ~ '^U(A|S)'

postgreSQL: field separated by semicolon [duplicate]

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

Postgresql count unique conversation [duplicate]

This question already has answers here:
MYSQL select DISTINCT values in two columns
(8 answers)
Closed 5 years ago.
Hello i have some table like this:
Text | from | to
A | 1 | 2
B | 2 | 1
C | 3 | 1
D | 1 | 4
And i would like to get number of conversations, so for this example it should be 3. Do anyone know how to do it? Thank you.
One more information - i decided to use UUID so i switched to PostgreSQL and there is no least and greatest.
SELECT * FROM your_table_name name_of_you_like_to_select
WHERE name_of_you_like_to_select.from = 3;
You can use some SQL query like this.

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 ?

EXISTS subquery: SELECT 1 or SELECT * FROM X performant in Postgres? [duplicate]

This question already has answers here:
What is easier to read in EXISTS subqueries? [closed]
(3 answers)
Closed 6 years ago.
Is it better (in terms of performance, speed, etc.) to write
SELECT * FROM a WHERE (EXISTS (SELECT * FROM b))
or
SELECT * FROM a WHERE (EXISTS (SELECT 1 FROM b))
in PostgreSQL?
p.s.
This question answers my question for MS SQL Server, but what about PostgreSQL?
Per the documentation:
Since the result depends only on whether any rows are returned, and
not on the contents of those rows, the output list of the subquery is
normally unimportant.