0

I want to return extra dynamic column based on some query. Suppose i have a table named as Table_A having columns A,B,C

If i want to return extra column i will do as below

SELECT A,B,C,D=1
FROM Table_A

D is the extra column with 1 value. Is there any way to put query on D like:

Select SELECT A,B,C,D = (//i want to put query which will return 1 or 0) FROM Table_A

Can i do this?

2
  • You can directly write a query instead of D =1 Commented Dec 28, 2015 at 7:18
  • 3
    The best answer depends on specific details you haven't provided. You might be able to use a CASE expression. You might be able to use a query inline. Maybe you need to join another table or use a subquery. There aren't really enough details to answer your question more than "Yes, you can do it." Post your query, a sample of your data, and your desired results. Commented Dec 28, 2015 at 7:20

2 Answers 2

1

Yeah like this

SELECT A,
       B,
       C,
       (SELECT somecol
        FROM   sometable
        ORDER  BY somecol
        limit 1) AS D
FROM   Table_A 

LIMIT: To avoid sub-query returns more than one row if any

Sign up to request clarification or add additional context in comments.

Comments

0

You can use a subquery.

SELECT A,B,C, (
   SELECT Some_Column FROM Some_Table) AS D
FROM Table_A

You can also use columns from Table_A in the subquery's where clause. Note that subquery's can lead to bad performance, as they go off for each result row.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.