?
Solved

SQL Query with more that one "SELECT TOP 1"

Posted on 2011-09-11
4
Medium Priority
?
243 Views
Last Modified: 2012-05-12
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!
0
Comment
Question by:MaxKroy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 4

Accepted Solution

by:
degaray earned 2000 total points
ID: 36520613
You can use two subqueries for that, see the code:

I hope it helps you

Cheers!
SELECT(
   SELECT TOP 1 'Column1' 
   FROM TableA WHERE Z LIKE '%str%)
   AS x, (
   SELECT TOP 1 'Column3'
   FROM TableA WHERE Z LIKE '%str%
   ) AS y

Open in new window

0
 
LVL 4

Expert Comment

by:degaray
ID: 36520624
Depending on the data on your tables you might need to add a LIMIT 1, 0

So this should be the code then.
SELECT(
   SELECT TOP 1 'Column1' 
   FROM TableA WHERE Z LIKE '%str% LIMIT 1,0
   ) AS x, (
   SELECT TOP 1 'Column3'
   FROM TableA WHERE Z LIKE '%str% LIMIT 1,0
   ) AS y

Open in new window

0
 
LVL 4

Expert Comment

by:degaray
ID: 36520627
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

Open in new window

0
 

Author Closing Comment

by:MaxKroy
ID: 36520693
Thank you Obi-one. You are my only hope.
0

Featured Post

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating and Managing Databases with phpMyAdmin in cPanel.
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question