[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

SQL nested SELECT statement for column

Posted on 2010-11-23
3
Medium Priority
?
331 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 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

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

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…
Ready to get certified? Check out some courses that help you prepare for third-party exams.
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
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

649 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