• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 566
  • Last Modified:

Using a column from a subquery in where statement

Here's what I've got:

SELECT col1, col2, subquery(...) as col3count
FROM table
LEFT JOIN tables ON condition
WHERE col3count > 0

I get this error:

failed : column "col3count" does not exist

I've worked around this with the ORDER BY, where I can specify the column number instead of its name.

Here is my actual query:

select ev.event_id,
ef.event_family_name,
( select count(sa.numeric_value) from event e
  left join opportunity as o on o.event_id = e.event_id
  left join person_survey as ps on ps.oppty_id = o.oppty_id
  left join survey_answer as sa on ps.person_survey_id = sa.person_survey_id
  left join survey_question as sq on sq.survey_question_id = sa.survey_question_id
  left join speaker as spk on o.speaker_id = spk.speaker_id
  left join users as ta on ta.user_id=spk.tech_assist_id
  where ps.oppty_id = o.oppty_id
  and ps.survey_id = 2
  and sq.question_number = 24
  and sa.numeric_value = 5
  and e.event_id = ev.event_id
  and o.oppty_date between cast('2005-01-01' as date) and cast('2006-12-31' as date)
  and sa.numeric_value > 0 ) as exceeded,
( select count(sa.numeric_value) from event e
  left join opportunity as o on o.event_id = e.event_id
  left join person_survey as ps on ps.oppty_id = o.oppty_id
  left join survey_answer as sa on ps.person_survey_id = sa.person_survey_id
  left join survey_question as sq on sq.survey_question_id = sa.survey_question_id
  left join speaker as spk on o.speaker_id = spk.speaker_id
  left join users as ta on ta.user_id=spk.tech_assist_id
  where ps.oppty_id = o.oppty_id
  and ps.survey_id = 2
  and sq.question_number = 24
  and sa.numeric_value = 3
  and e.event_id = ev.event_id
  and o.oppty_date between cast('2005-01-01' as date) and cast('2006-12-31' as date)
  and sa.numeric_value > 0) as met,
( select count(sa.numeric_value) from event e
  left join opportunity as o on o.event_id = e.event_id
  left join person_survey as ps on ps.oppty_id = o.oppty_id
  left join survey_answer as sa on ps.person_survey_id = sa.person_survey_id
  left join survey_question as sq on sq.survey_question_id = sa.survey_question_id
  left join speaker as spk on o.speaker_id = spk.speaker_id
  left join users as ta on ta.user_id=spk.tech_assist_id
  where ps.oppty_id = o.oppty_id
  and ps.survey_id = 2
  and sq.question_number = 24
  and sa.numeric_value = 1
  and e.event_id = ev.event_id
  and o.oppty_date between cast('2005-01-01' as date) and cast('2006-12-31' as date)
  and sa.numeric_value > 0 ) as below,
( select count(o) from event e
left join opportunity as o on o.event_id = e.event_id
left join person_survey as ps on ps.oppty_id = o.oppty_id
left join survey_answer as sa on ps.person_survey_id = sa.person_survey_id
left join survey_question as sq on sq.survey_question_id = sa.survey_question_id
left join speaker as spk on o.speaker_id = spk.speaker_id
left join users as ta on ta.user_id=spk.tech_assist_id
where ps.oppty_id = o.oppty_id
and ps.survey_id = 2
and sq.question_number = 24
and sa.numeric_value > 0
and e.event_id = ev.event_id
and o.oppty_date between cast('2005-01-01' as date) and cast('2006-12-31' as date)
and sa.numeric_value > 0 ) as count
from event ev
left join event_family as ef on ef.event_family_id = ev.event_family_id
left join opportunity as o on ev.event_id = o.event_id
left join speaker as spk on o.speaker_id = spk.speaker_id
left join users as ta on ta.user_id=spk.tech_assist_id
left join person_survey as ps on ps.oppty_id = o.oppty_id
left join survey_answer as sa on ps.person_survey_id = sa.person_survey_id
where o.oppty_date between cast('2005-01-01' as date) and cast('2006-12-31' as date)
group by ev.event_id, ef.event_family_name
order by 4 desc
0
RandalBarnes
Asked:
RandalBarnes
  • 2
1 Solution
 
GGuzdziolCommented:
Maybe something like:

SELECT col1, col2, col3count
  FROM (
    SELECT col1, col2, subquery(...) as col3count
      FROM table
        LEFT JOIN tables ON condition
  )
  WHERE col3count > 0
0
 
earth man2Commented:
as an aside from the thrust of your problem
ps.oppty_id = o.oppty_id is implicit from your
left join person_survey as ps on ps.oppty_id = o.oppty_id

if you are testing for a count(...) > 0 then you could put that select claws in a where exists claws instead.
0
 
earth man2Commented:
does the following generate the right number of rows ?

select  ev.event_id, ef.event_family_name, count(sa.numeric_value) as exceeded
from event ev
left join event_family as ef on ef.event_family_id = ev.event_family_id
left join opportunity as o on ev.event_id = o.event_id
left join speaker as spk on o.speaker_id = spk.speaker_id
left join users as ta on ta.user_id=spk.tech_assist_id
left join person_survey as ps on ps.oppty_id = o.oppty_id
left join survey_answer as sa on ps.person_survey_id = sa.person_survey_id
where o.oppty_date between cast( '2005-01-01' as date ) and cast( '2006-12-31' as date )
  and ps.survey_id = 2
  and sq.question_number = 24
  and sa.numeric_value = 5
  and sa.numeric_value > 0
group by ev.event_id, ef.event_family_name;
0
 
rjkimbleCommented:
I'm pretty sure you can't use the alias in the where clause. What you have to do is repeat the entire expression for the column instead. It seems stupid to me, but that seems to be the SQL standard. The same thing holds in Oracle.

Another possibility is to use an inline view or whatever they call it in PostgreSQL:

select * from (
<your select statement>
) t where t.col3count > 0
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now