Remove comma seprated values in postgresql - postgresql

one table alphabet has column values nvarchar and values are (A,B,C,D,E,F,G,H)
we have another table that is linked with the previous one and this table has one column
which has values (C, H).
so These two values will be deleted from the previous table columns.
ID
values
1
A,B,C,D,E,F,G,H,I,J,K,L,M
2
A,C,F,G,H,J,U,V,W,X,Y,Z
second table.
ID
values
1
C,F
1
A,Z
FINAL OUTPUT .
TABLE A.
ID
values
1
B,D,E,G,H,I,J,K,L,M
2
A,C,F,G,H,J,U,V,W,X,Y,Z

Sample Query:
select
t1.id,
string_agg(t1.val1, ',') as "values"
from
(select "id", unnest(('{' || "values" || '}')::text[]) as val1 from table1
) as t1
left join
(select "id", unnest(('{' || "values" || '}')::text[]) val2 from table2
) t2 on t1.id = t2.id and t1.val1 = t2.val2
where t2.id is null
group by t1.id
-- Return:
id values
---------------------------------
1 B,D,E,G,H,I,J,K,L,M
2 A,C,F,G,H,X,J,U,V,W,Y,Z

Related

Single sql query to select a record from parent table only if all it's records in child table has values

I have above tables:
I need a query to check if in T3, for each Id1 we have all Id2s according to T2, return result table above
in the table T2 there are only all id2 for id1 so in the result we just have All for id2 that is in the same row as id1 = 1 and Name3 is arrayed only in that row
SELECT T2.Id1
, CASE WHEN count(DISTINCT Id2) > 1 THEN 'All'::text
ELSE min(Id2)::text END AS Id2
, string_agg(DISTINCT Name3, ',') AS Name3
FROM T2
LEFT JOIN T3 USING (Id1, Id2)
GROUP BY T2.Id1
HAVING bool_or(T3.Id1 IS NULL) IS FALSE;
Assuming all identifiers unquoted, so effectively case-insensitive.
Normally I add more explanation, but I'll match the silence in the question.

Joining Null values

Table 1
Table 2
select col1, col2, etc
from table 1
left join table 2
on table1.col1 = table2.col1
If table2.col1 has null values, will this somehow screw up my join results?
No, table2 null values do not matter in this case. Left join will match all rows from table1 to table2. If a row from table2 does not have a match in table1 it will not appear in the result set.

Join two tables with count from first table

I know there is an obvious answer to this question, but I'm like a noob trying to remember how to write queries. I have the following table structure in Postgresql:
CREATE TABLE public.table1 (
accountid BIGINT NOT NULL,
rpt_start DATE NOT NULL,
rpt_end DATE NOT NULL,
CONSTRAINT table1_pkey PRIMARY KEY(accountid, rpt_start, rpt_end)
)
WITH (oids = false);
CREATE TABLE public.table2 (
customer_id BIGINT NOT NULL,
read VARCHAR(255),
CONSTRAINT table2 PRIMARY KEY(customer_id)
)
WITH (oids = false);
The objective of the query is to display a result set of accountid's, count of accountid's in table1 and read from table2. The join is on table1.accountid = table2.customer_id.
The result set should appear as follows:
accountid count read
1234 2 100
1235 9 110
1236 1 91
The count column reflect the number of rows in table1 for each accountid. The read column is a value from table2 associated with the same accountid.
select accountid, "count", read
from
(
select accountid, count(*) "count"
from table1
group by accountid
) t1
inner join
table2 t2 on t1.accountid = t2.customer_id
order by accountid
SELECT table2.customer_id, COUNT(*), table2.read
FROM table2
LEFT JOIN table1 ON (table2.customer_id = table1.accountid)
GROUP BY table2.customer_id, table2.read
SELECT t2.customer_id, t2.read, COUNT(*) AS the_count
FROM table2 t2
JOIN table1 t1 ON t1.accountid = t2.customer_id
GROUP BY t2.customer_id, t2.read
;

postgresql where clause behavior

I made two queries that I thought should have the same result:
SELECT COUNT(*) FROM (
SELECT DISTINCT ON (id1) id1, value
FROM (
SELECT table1.id1, table2.value
FROM table1
JOIN table2 ON table1.id1=table2.id
WHERE table2.value = '1')
AS result1 ORDER BY id1)
AS result2;
SELECT COUNT(*) FROM (
SELECT DISTINCT ON (id1) id1, value
FROM (
SELECT table1.id1, table2.value
FROM table1
JOIN table2 ON table1.id1=table2.id
)
AS result1 ORDER BY id1)
AS result2
WHERE value = '1';
The only difference being that one had the WHERE clause inside SELECT DISTINCT ON, and the other outside that, but inside SELECT COUNT. But the results were not the same. I don't understand why the position of the WHERE clause should make a difference in this case. Can anyone explain? Or is there a better way to phrase this question?
here's a good way to look at this:
SELECT DISTINCT ON (id) id, value
FROM (select 1 as id, 1 as value
union
select 1 as id, 2 as value) a;
SELECT DISTINCT ON (id) id, value
FROM (select 1 as id, 1 as value
union
select 1 as id, 2 as value) a
WHERE value = 2;
The problem has to do with the unique conditions and what is visible where. It is behavior by design.

PostgreSQL compare with substring doesn't work

I want to select from 2 table where 2nd id is equal with first 4 character from first id
SELECT a.*, b.*, substring(a.my_id from 1 for 4)::integer as number
FROM table1 as a
INNER Join table2 as b ON(b.id_2=number) where my_id = 101
^
It produces an error here |
ERROR: column "number" does not exist
SQL state: 42703
Character: 189
You can't use an alias like that, you need a derived table:
select *
from (
SELECT t1.*,
substring(t1.my_id::text from 1 for 4)::integer as number
FROM table1 t1
) as a
inner join table2 as b ON (b.id_2 = a.number)
where my_id = 101
Storing a number that is used as a foreign key as a part in a varchar column is a really, really ugly design. That number should be a column of its own in table1.