Solved

Nested Selects in SQL Query

Posted on 2015-01-05
3
101 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
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

813 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

10 Experts available now in Live!

Get 1:1 Help Now