Solved

Nested Selects in SQL Query

Posted on 2015-01-05
3
106 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:jatkin
[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 250 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 250 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:jatkin
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

Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

Question has a verified solution.

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

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
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.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

729 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