Solved

Nested Selects in SQL Query

Posted on 2015-01-05
3
97 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
3 Comments
 
LVL 142

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 65

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Join & Write a Comment

Suggested Solutions

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
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.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now