subquery in a select statement

mcrmg
mcrmg used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
SQL Server Data Dude
Most Valuable Expert 2013
Author of the Year 2015
Commented:
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

Author

Commented:
I see. Thank you very much
Scott PletcherSenior DBA
Most Valuable Expert 2018
Top Expert 2014

Commented:
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
Ensure you’re charging the right price for your IT

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

Mark WillsTopic Advisor, Page Editor
Distinguished Expert 2018

Commented:
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)

Author

Commented:
Thank you very much.
Mark WillsTopic Advisor, Page Editor
Distinguished Expert 2018

Commented:
My pleasure :)

Try it out., and if it works for you, you might consider marking it as Helpful, or, as another Solution :)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial