Solved

SQL nested SELECT statement for column

Posted on 2010-11-23
3
326 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
always on switch back after failover 2 35
ms sql + get number in list out of total 7 29
Writing comments on <p></P> or paragraph 2 14
Merge two rows in SQL 4 17
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
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.
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.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

832 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