?
Solved

Nested Selects in SQL Query

Posted on 2015-01-05
3
Medium Priority
?
107 Views
Last Modified: 2015-01-05
Hi all,

I need to filter the results of a table query based on the result of a nested select.
I currently have the following:
SELECT 
(SELECT Ok1  = case Latitude  when '-' then '0' when '' then '0' else '1' end) as Ok1,	
(SELECT Ok2 = case Longitude when '-' then '0' when '' then '0' else '1' end) as Ok2
FROM tblEquipment

Open in new window

This selects the data from the tblEquipment correctly and creates two new columns 'Ok1' and 'Ok2'

I also wish to filter based on the result of these by adding something like
WHERE Ok1='1' AND Ok2='1'

Open in new window


But I get Invalid column name 'Ok1'
But I get Invalid column name 'Ok2'

Am I doing something that I am not supposed to or do I have the syntax wrong?

Is there a better or more correct way to do this?

Thanks for the help,

James
0
Comment
Question by:James Atkin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 1000 total points
ID: 40531426
the "ad-hoc" columns are only available in the ORDER BY part, unless you do this:

select sq.* 
from (
SELECT 
(SELECT Ok1  = case Latitude  when '-' then '0' when '' then '0' else '1' end) as Ok1,	
(SELECT Ok2 = case Longitude when '-' then '0' when '' then '0' else '1' end) as Ok2
FROM tblEquipment
) sq
where sq.Ok1='1' 
  and sq.Ok2='1'

Open in new window


however, I will tell you a first optimization: avoid that SELECT keyword:
select sq.* 
from (
SELECT  case Latitude  when '-' then '0' when '' then '0' else '1' end as Ok1,	
  , case Longitude when '-' then '0' when '' then '0' else '1' end as Ok2
FROM tblEquipment
) sq
where sq.Ok1='1' 
  and sq.Ok2='1'

Open in new window


next: if you need to use this expression a lot, you shall make that a computed column in your table...
0
 
LVL 66

Assisted Solution

by:Jim Horn
Jim Horn earned 1000 total points
ID: 40531430
For starters, you probably don't need the nested select
SELECT 
   CASE Latitude  WHEN '-' THEN '0' WHEN '' THEN '0' ELSE '1' END as Ok1,	
   CASE Longitude WHEN '-' THEN '0' WHEN '' THEN '0' ELSE '1' END as Ok2
FROM tblEquipment

Open in new window

Also, be advised that '0' and '1' are string values, and not a numeric or bit 0 and 1.  If you need numeric or bit, then lose the single quote marks '.

>I also wish to filter based on the result of these by adding something like
Throw the whole thing into a subquery, and WHERE off of that
SELECT a.Latitude, a.Longitude
FROM (
	SELECT 
		CASE Latitude  WHEN '-' THEN '0' WHEN '' THEN '0' ELSE '1' END as Ok1,	
		CASE Longitude WHEN '-' THEN '0' WHEN '' THEN '0' ELSE '1' END as Ok2
	FROM tblEquipment) a
WHERE a.Ok1 = '1' AND a.Ok2 = '1'

Open in new window

0
 
LVL 4

Author Closing Comment

by:James Atkin
ID: 40531466
Thanks to both of you for really fast and detailed solutions.
I've split the points down the middle - hope this is okay.
Best regards,
James
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Suggested Courses

777 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