?
Solved

Using a column from a subquery in where statement

Posted on 2006-05-25
4
Medium Priority
?
562 Views
Last Modified: 2011-09-20
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
Comment
Question by:RandalBarnes
  • 2
4 Comments
 
LVL 14

Accepted Solution

by:
GGuzdziol earned 2000 total points
ID: 16767013
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
 
LVL 22

Expert Comment

by:earth man2
ID: 16768177
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
 
LVL 22

Expert Comment

by:earth man2
ID: 16773339
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
 
LVL 9

Expert Comment

by:rjkimble
ID: 16779408
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

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many developers have database experience, but are new to PostgreSQL. It has some truly inspiring capabilities. I have several years' experience with Microsoft's SQL Server. When I began working with MySQL, I wanted a quick-reference to MySQL (htt…
By, Vadim Tkachenko. In this article we’ll look at ClickHouse on its one year anniversary.
Steps to create a PostgreSQL RDS instance in the Amazon cloud. We will cover some of the default settings and show how to connect to the instance once it is up and running.
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

579 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question