SQL nested SELECT statement for column

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.
Who is Participating?
Guy Hengel [angelIII / a3]Connect With a Mentor Billing EngineerCommented:
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

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?


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
All Courses

From novice to tech pro — start learning today.