Solved

SQL Query with more that one "SELECT TOP 1"

Posted on 2011-09-11
4
242 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 500 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

Get Database Help Now w/ Support & Database Audit

Keeping your database environment tuned, optimized and high-performance is key to achieving business goals. If your database goes down, so does your business. Percona experts have a long history of helping enterprises ensure their databases are running smoothly.

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

728 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