Subquery not working with NULL data [duplicate] - postgresql

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 ?

Related

Handling NULL values in Postgresql Rust Select [duplicate]

This question already has an answer here:
Postgresql - Compare a string with a null value
(1 answer)
Closed last month.
I red a bunch of questions about handling NULL values in rust-postgresql, but they don't mention my personal case.
I need to select rows using a WHERE with a parameter that can be NULL, like
SELECT id FROM TABLE WHERE name=$1
If the Option used to set $1 is None, it doesn't retrieve any rows.
So i have to split the query under a IF, like
if myname.is_some()
SELECT id FROM TABLE WHERE name=$1
else
SELECT id FROM TABLE WHERE name=NULL
There is a better way to handle a NULL in a WHERE clause with a single line of code?
I tried to handle it with the "default" way.
I hope there is a faster way.
If a NULL parameter means "all rows" you can use an OR:
SELECT id
FROM TABLE
WHERE name = $1
OR $1 IS NULL;
If you want to find those where the name is NULL if the parameter is NULL, you can use IS NOT DISTINCT FROM:
SELECT id
FROM TABLE
WHERE name is not distinct from $1

Postgresql returns rows in a wrong order [duplicate]

This question already has answers here:
Postgres default sort by id - worldship
(4 answers)
Postgresql: row number changes on update
(1 answer)
Closed 3 months ago.
I found a really strange thing related to postgresql. Here is my query:
select *
from "bids"
inner join "users" on "users"."id" = "bids"."userId"
where "auctionId" = 151
And here is a result:
So, as you see, rows are sorted in descending order by id. If I add ordering in query:
select *
from "bids"
inner join "users" on "users"."id" = "bids"."userId"
where "auctionId" = 151
order by "bids"."id" asc
I get a result:
It looks like one of database settings was set incorrectly, but I couldn't find it in google. Please, put your thoughts here, any help is welcome.
Thanks.

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)

postgres `insert on conflict update` multiple columns [duplicate]

This question already has answers here:
How to update all columns with INSERT ... ON CONFLICT ...?
(2 answers)
Closed 4 years ago.
In this Postgres query,
INSERT INTO TB_PO
SELECT * FROM temporary_table
ON CONFLICT (id) DO UPDATE
SET id = excluded.id;
Since both the tables tb_po and temporary_table are identical with 26+ columns, is there a way I can specify after the SET, that it will set all columns of the affected row? So that I don't have to manually input each column with SET.
thanks
You could avoid some typing by generating your statement based on the results of
SELECT column_name
FROM information_schema.columns
WHERE table_name = 'TB_PO';

Update rows from another table [duplicate]

This question already has answers here:
How do I UPDATE from a SELECT in SQL Server?
(38 answers)
Closed 8 years ago.
I have a table with 2 columns, Country and Qty. the field country has distinct acronyms of all the countries. Now my job is to replace those acronyms with the actual country name. Now there is another table with acronyms and corresponding country names. I have to take the values from this second table and update the first one where Acronyms match. Please help..
UPDATE q
SET country = a.country
FROM quantity q
JOIN acronym a
ON a.acronym = q.country