MaxKroy
asked on
SQL Query with more that one "SELECT TOP 1"
Greetings,
I am trying to generate a SQL query where I can select more than one "TOP 1".
For instance, I might want the top 1 from one column and the top 1 from another column, both being in different rows.
SELECT TOP 1 'Column1' as x, TOP 1 'Column3' as y, Z FROM TableA WHERE Z LIKE '%str%'
Please let me know if this requires more explanation. Thanks in advance!
I am trying to generate a SQL query where I can select more than one "TOP 1".
For instance, I might want the top 1 from one column and the top 1 from another column, both being in different rows.
SELECT TOP 1 'Column1' as x, TOP 1 'Column3' as y, Z FROM TableA WHERE Z LIKE '%str%'
Please let me know if this requires more explanation. Thanks in advance!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Sorry like this:
SELECT(
SELECT TOP 1 'Column1'
FROM TableA WHERE Z LIKE '%str% LIMIT 0, 1
) AS x, (
SELECT TOP 1 'Column3'
FROM TableA WHERE Z LIKE '%str% LIMIT 0, 1
) AS y
ASKER
Thank you Obi-one. You are my only hope.
So this should be the code then.
Open in new window