1

trying to put a condition in the main where clause that checks a value from a subquery.

SELECT total, (select my_count from x where .. etc) as XXX

FROM sales

WHERE XXX > 0;

I get an unknown column error. Any ideas why?

Brettski
  • 1,061
  • 2
  • 12
  • 25

2 Answers2

2

What about

select total, xxx
from (
    SELECT total, (select my_count from x where .. etc) as XXX
    FROM sales
     )
WHERE XXX > 0;
Amir Pashazadeh
  • 7,170
  • 3
  • 39
  • 69
0

This is a really strange query.
Why don't you put the WHERE condition inside the subquery?

SELECT total, (select my_count from x where .. AND my_count > 0) as XXX
FROM sales

And you're getting an error because you can't use the result of a subquery as a column name.

rgafonso
  • 56
  • 4
  • I have several columns from subqueries, basically orders in last 1, 3, 6 months, wanted to do some filtering based on results – Brettski Apr 02 '12 at 00:17
  • But you are not joining anything with the main query. Can you provide a better example? – rgafonso Apr 02 '12 at 00:47
  • This query will return `total, null` if `my_count <= 0`, and will not filter the record. I believe in `..` part of the sub-query, he is doing something with `sales` table. – Amir Pashazadeh Apr 02 '12 at 19:12