Solved

SQL nested SELECT statement for column

Posted on 2010-11-23
3
325 Views
Last Modified: 2012-05-10
I am trying to select a column inside of a SELECT statement that pulls vlaues out where a different column equals a value.  This is an example of what I want to do but it is not correct:

SELECT column1, column2, (SELECT column 3 WHERE column6 <= 12 AND column10 >= 1 AS YearlySum), column 3, column4, column 5 FROM table1

As you can see I would like to select all of the columns from the table, but also select some of the columns based on certain conditions.  Any hlep is greatly appreciated.
0
Comment
Question by:zintech
3 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 34196828
you cannot optionally select a column.
you could return a NULL value optionally ...
SELECT column1, column2

, CASE WHEN column6 <= 12 AND column10 >= 1 THEN column3 ELSE NULL END AS YearlySum

, column 3, column4, column 5 

FROM table1

Open in new window

0
 
LVL 3

Expert Comment

by:ppango
ID: 34196846
To accomplish this you need to use the CASE clause

But my first question will be this one, what if the condition you specified is not met? lets say Column 6 is grater then 12 and column 10 less than 1?

Thanks

0
 
LVL 5

Expert Comment

by:logideepak
ID: 34196908
you can use a case statement

SELECT column1, column2, case when  column6 <= 12 AND column10 >= 1 then column3 else null end  AS YearlySum, column 3, column4, column 5 FROM table1
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
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.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
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.

914 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

18 Experts available now in Live!

Get 1:1 Help Now