Solved

SQL Query with more that one "SELECT TOP 1"

Posted on 2011-09-11
4
238 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
  • 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

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.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

828 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