[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 115
  • Last Modified:

Nested Selects in SQL Query

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
James Atkin
Asked:
James Atkin
2 Solutions
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
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
 
James AtkinSenior Principle Software EngineerAuthor Commented:
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now