[PostgreSQL]: Subquery has too many columns - postgresql

I am trying to run a query, but the result is showing the error message "Subquery has too many columns". I have to change where the clause with array to string but the error is the same. could you help me, if you have any suggestions or different queries as modify to solve this?
the query is:
create table my_schema.table_1 as select a.*
, O.ID1
, J.ID2
, K.ID3
, L.ID4
, M.ID5
, S.ID6
, O.plan_name plan_name1
, J.plan_name plan_name2
, K.plan_name plan_name3
, L.plan_name plan_name4
, M.plan_name plan_name5
, S.plan_name plan_name6
from (
select
PRD_ID
, NBR
, SI
, START_TIME
, ATTR4
, G_ID
, BYTE_UP
, BYTE_DOWN
, LIST
, TYPE_ID1
, TYPE_ID2
, TYPE_ID3
, TYPE_ID4
, TYPE_ID5
, TYPE_ID6
, CHARGE1, CHARGE2, CHARGE3, CHARGE4, CHARGE5, CHARGE6
from my_schema.source where prd_id = '20200101'
and TYPE IN (3) ) a
left outer join (select distinct id, price_plan_name from my_schema.id_ref_v9) O on split_part(split_part(list,';',1),',',1) = O.id
left outer join (select distinct id, price_plan_name from my_schema.id_ref_v9) J on split_part(split_part(list,';',2),',',1) = J.id
left outer join (select distinct id, price_plan_name from my_schema.id_ref_v9) K on split_part(split_part(list,';',3),',',1) = K.id
left outer join (select distinct id, price_plan_name from my_schema.id_ref_v9) L on split_part(split_part(list,';',4),',',1) = L.id
left outer join (select distinct id, price_plan_name from my_schema.id_ref_v9) M on split_part(split_part(list,';',5),',',1) = M.id
left outer join (select distinct id, price_plan_name from my_schema.id_ref_v9) S on split_part(split_part(list,';',6),',',1) = S.id
WHERE (
O.id in (select * from my_schema.LIST_PRICEID4)or
J.id in (select * from my_schema.LIST_PRICEID4)or
K.id in (select * from my_schema.LIST_PRICEID4)or
L.id in (select * from my_schema.LIST_PRICEID4)or
M.id in (select * from my_schema.LIST_PRICEID4)or
S.id in (select * my_schema.LIST_PRICEID4))

Related

postgresql How to share cte among different tables in plain sql?

Say select id from some_expensive_query is the cte I want to share. Currently I write two sql in a transaction:
with t as (select id from some_expensive_query) select * from t1 join t on t.id =t1.id;
with t as (select id from some_expensive_query) select * from t2 join t on t.id =t2.id;
As you can see, the cte is executed twice but I want something like:
t = select id from some_expensive_query;
select * from t1 join t on t.id =t1.id;
select * from t2 join t on t.id =t2.id;
for portability, I don't want to use pgsql or functions, anyway to solve this?
Why don't you use union all ?
with t as (select id from some_expensive_query)
select * from t1 join t on t.id =t1.id
union all
select * from t2 join t on t.id =t2.id;

how to replace duplicated subquery?

I got a query
SELECT name AS name,
count(group_name) AS all_test_cases,
(SELECT count(*) FROM test_cases AS tc2 WHERE tc2.group_name = tg.name AND status = 'OK' ) AS passed_test_cases,
tg.test_value * (SELECT count(*) FROM test_cases AS tc2 WHERE tc2.group_name = name AND status = 'OK') AS total_value
FROM test_groups AS tg
LEFT JOIN test_cases AS tc
ON tg.name = tc.group_name
GROUP BY name, test_value
ORDER BY total_value DESC, name ASC
How I can replace duplicated subquery:
tg.test_value * (SELECT count(*) FROM test_cases AS tc2 WHERE tc2.group_name = name AND status = 'OK') AS total_value
with something more efficient in postgres without stored procedure?
Does this work for you?
with cte_test_cases as (
SELECT group_name,
count(*) filter (where status = 'OK') as passed_test_cases,
count(*) as all_test_cases
FROM test_cases
GROUP BY group_name
)
SELECT tg.name,
tc.all_test_cases,
tc.passed_test_cases,
tg.test_value * tc.passed_test_cases AS total_value
FROM test_groups AS tg
LEFT JOIN cte_test_cases AS tc
ON tg.name = tc.group_name
ORDER BY total_value DESC, name ASC

Find null or value(s)

Join to a table that stores multivalue information
Need to query for null or value(s)
Currently do it with a union
First get the null with an outer join
Second union to get the values
Is there a better approach?
select [docSVsys].[sID]
from [docSVsys] with (nolock)
left outer join [docEnum1] as [jointable] with (nolock)
on [jointable].[enumID] = '142'
and [jointable].[sID] = [docSVsys].[sID]
where [jointable].[sID] is null
Union
Select distinct([Table].[sID])
From [DocEnum1] as [Table] with (nolock)
Where 1 = 1
And [Table].[enumID] = '142'
and [Table].[valueID] in (1,2)
Try this:
select distinct
case
when [jointable].[valueID] in (1,2) then [jointable].[sID]
else [docSVsys].[sID]
end
from [docSVsys] with (nolock)
left outer join [docEnum1] as [jointable] with (nolock)
on [jointable].[enumID] = '142'
and [jointable].[sID] = [docSVsys].[sID]
where [jointable].[sID] is null or [jointable].[valueID] in (1,2)
Tell me if it's OK

ordering by rows

OK so I have a query I am trying to build.. I have 2 tables, table1 has a bunch of regular records as normal with a unique ID (auto increment) and table2 has records that include some of those ids from table1. I am trying to order by the highest records with that same ID in table1.. Heres what I've got:
SELECT * FROM table1
WHERE table1.status = 1
AND (SELECT COUNT(*) FROM table2 WHERE table2.tbl1_id = table1.id)
ORDER BY table1.id DESC
Thanks :)
SELECT table1.id
FROM table1
LEFT JOIN table2 ON table2.tbl1_id = table1.id
WHERE table1.status = 1
GROUP BY table1.id
ORDER BY COUNT(table2.tbl1_id) DESC
Try this:
SELECT a.*, b.cnt
FROM table1 a LEFT JOIN
(
SELECT tbl1_id, COUNT(*) cnt
FROM table2
GROUP BY tbl1_id
) b
ON a.id = b.tbl1_id
WHERE table1.status = 1
ORDER BY cnt DESC

TSQL A problem with categories tree

I have a problem with recursive CTE query
Let's say that I have that category tree (Category table)
In my CTE query, I search for all children of the 1 category:
(that query works fine)
with mq as
(
select c.Id as parent, c.Id as child
from dbo.Category c
where c.Id = 1
union all
select q.child, c.Id
from mq q
inner join dbo.Category c on q.child = c.IdParentCategory
)
The output
Then, I want to get that Category ID, wchih doesn't have a child: categories 9,10,12,14,15
with mq as
(
select c.Id as parent, c.Id as child
from dbo.Category c
where c.Id = 1
union all
select q.child, c.Id
from mq q
inner join dbo.Category c on q.child = c.IdParentCategory
where child in
(
select c1.Id
from dbo.Category c1
where not exists(select c2.Id
from dbo.Category c2
where c2.Id = c1.IdParentCategory)
)
)
but the output is wrong:
why ? Any ideas will be helpful !
if I separate the query from CTE, everything is OK
declare #tab table
(parent int, child int);
insert into #tab
select * from mq
delete from #tab
where child in (
select c1.parent
from #tab c1
where not exists(select c2.parent from #tab c2 where c2.parent = c1.child)
)
with mq as
(
select c.Id as parent, c.Id as child
from dbo.Category c
where c.Id = 1
union all
select q.child, c.Id
from mq q
inner join dbo.Category c on q.child = c.IdParentCategory
)
select child from mq where child not in (select parent from mq)
Would seem to give the output you want - in fact your description of the problem almost took this form.