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
Oracle SQL Date Range Intersections
Sql Query to Sum Fields from Different Tables
How to Drop All Foreign-Key Constraints on a Table in SQL Server 2000
Referencing a Composite Primary Key
SQL Server - Asynchronous Query Execution
How This SQL Injection Works? Explanation Needed
Postgres - Comparing Two Arrays
In SQL Server, How to Create While Loop in Select
Codeigniter - Continue on SQL Error
Oracle Pls-00363: Expression '' Cannot Be Used as an Assignment Target
Recursive Query Challenge - Simple Parent/Child Example
Best Practices for Multithreaded Processing of Database Records
Case Statement in Where Clause - SQL Server
How to Get the Sum of All Column Values in the Last Row of a Resultset
Add Indexes to Speed Up Geocoder Near Search
Using a View with No Primary Key with Entity
How to Use Left & Right Functions in SQL to Get Last 3 Characters