subquery in a select statement

Hi,

Is there a way to return more than one value in a subquery inside a select? for example:

SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate, (select x from table z where x=1) as abc
FROM Orders
INNER JOIN Customers ON Orders.CustomerID=Customers.CustomerID

Instead of using  (select x from table z where x=1) as abc and (select y from table z where x=1) as def to just one query (select x,y from table z where x=1)

thanks
mcrmgAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Not in the SELECT clause.   If the logic is the same then you'll have to make it two separate columns.

SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate, 
   (select x from table z where x=1) as abc, 
   (select y from table z where x=1) as def
FROM Orders
INNER JOIN Customers ON Orders.CustomerID=Customers.CustomerID

Open in new window


or if there is a common ID value between Orders and z, then JOIN them..

SELECT o.OrderID, c.CustomerName, o.OrderDate, 
   z.x as abc, 
   z.y as def
FROM Orders o
    JOIN Customers c ON Orders.CustomerID=Customers.CustomerID
   LEFT JOIN z ON o.id = z.id

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mcrmgAuthor Commented:
I see. Thank you very much
0
Scott PletcherSenior DBACommented:
Instead of using  (select x from table ...) ... and (select y from table ...) ... just one query (select x,y from table ...)

Yes.

SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate, Z.abc, Z.y
FROM Orders
INNER JOIN Customers ON Orders.CustomerID=Customers.CustomerID
CROSS JOIN ( SELECT x AS  abc, y FROM [table] z WHERE x = 1 ) AS Z
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Mark WillsTopic AdvisorCommented:
The other way is to use cross apply

SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate, Z.X, Z.Y
FROM Orders
INNER JOIN Customers ON Orders.CustomerID=Customers.CustomerID
CROSS APPLY  (select x,y from table z where x=1) Z(x,y)


If there is something specific to an order, or a customer you can include that inside the cross apply


SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate, Z.X, Z.Y
FROM Orders
INNER JOIN Customers ON Orders.CustomerID=Customers.CustomerID
CROSS APPLY  (select x,y from table z where x=1 and z.orderID = orders.orderid) Z(x,y)
0
mcrmgAuthor Commented:
Thank you very much.
0
Mark WillsTopic AdvisorCommented:
My pleasure :)

Try it out., and if it works for you, you might consider marking it as Helpful, or, as another Solution :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.