Sql: Subquery Has Too Many Columns

SQL: subquery has too many columns

You are projecting three columns in your subquery, but comparing a single one of them in the IN clause. Select just the required column (r1.king) for the IN in the subquery:

SELECT kingdom.king, dinasty.birth, dinasty.death
FROM kingdom, dinasty
WHERE kingdom.king = dinasty.name AND kingdom.king NOT IN
(
SELECT DISTINCT R1.king
FROM
(
SELECT DISTINCT R1.king, D1.birth, D1.death
FROM kingdom AS R1, dinasty AS D1, dinasty AS D2
WHERE R1.king=D1.name
) AS R1,
(
SELECT DISTINCT R1.king, D1.birth, D1.death
FROM kingdom AS R1, dinasty AS D1, dinasty AS D2
WHERE R1.king=D1.name
) AS R2
WHERE R1.death-R1.birth < R2.death-R2.birth
);

ERROR in PostgreSQL: Subquery has too many columns

  SELECT
gid,
max_height
FROM
my_table
where max_height not in
(
SELECT
max_height /* you must select only a column do the fact in where you have a column */
FROM
-- this part selects the most repeated max_height from my_table
(
SELECT gid, max_height,
ROW_NUMBER() OVER (PARTITION BY gid ORDER BY freq DESC) AS rn
FROM (
SELECT gid, max_height, COUNT(id) AS freq
FROM my_table
GROUP BY 1, 2
)hgt_freq
) ranked_hgt_req
WHERE rn = 1
)

Subquery has too many columns

If you want to combine two columns, you need to put them into parenthesis:

DELETE FROM tmp_grn 
WHERE (grn_code, item_skucode) IN (SELECT grn_code, item_skucode
FROM grn);

But suslov's answer using an exists is most probably faster - you need to check the execution plan to verify that.

PostgreSQL: subquery has too many columns when attempting to add filters

Your query is a bit inscrutable. I assume this is the logic that you really want:

SELECT u.id as user_id, b.id as boat_id
FROM users u CROSS JOIN
boats b
WHERE not exists (SELECT 1
FROM rentals r
WHERE r.user_id = u.id AND
r.boat_id = b.id
);

This returns the user/boat combinations that don't have a rental.

Django Error django.db.utils.ProgrammingError: subquery has too many columns

As the error states, you are selecting too many columns which your filter condition does not require. You subquery must select only Test1 table id field as you filter on id field. So basically you subquery should only project id field like this:

SELECT
test1.id
FROM
test1
WHERE
EXISTS (
SELECT
*
FROM
test2
JOIN test3 ON test2.test3_id = test3.id
AND test3.value = 'value'
JOIN test4 ON test2.test4_id = test4.id
AND test4.test1_id = test1.id
)
ORDER BY
test1.start_time DESC

So your final queryset should be like:

queryset = Test1.objects.filter(id__in=RawSQL("SELECT test1.id FROM test1 WHERE EXISTS (SELECT * FROM test2 JOIN test3 ON test2.test3_id = test3.id AND test3.value = %s JOIN test4 ON test2.test4_id = test4.id AND test4.test1_id = test1.id) ORDER BY test1.start_time DESC", params=[value]))

postgresql sub query has too many columns when checking against in condition

SELECT * FROM table1
WHERE col1 = ANY (
(SELECT array_agg(ARRAY[cola, colb, colc, cold])
FROM a
)::integer[]
);


Related Topics



Leave a reply



Submit