I am working on a SQL Server 2008 database. I need to get a specific order for a customer and their latest order ID and latest order data. My challenge here is, I need the data in a single row. Currently, I’m trying the following query:
SELECT
o.*,
c.[FirstName],
c.[LastName],
c.[PlayerName],
(SELECT TOP 1 o2.CreatedDate, o2.ID FROM [Order] o2 ORDER BY [CreatedOn] DESC)
FROM
[Order] o,
[Customer] c
WHERE
o.[ID]=c.[CustomerID]
When I execute this query, I get the following error:
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.
As you can imagine, there are scenarios where a customer will NOT have a previous order. How do I do this and get the result into a single row?
Thank you!
Slightly different way to do it using a CTE.