?
Solved

SQL Select Multiple Items in one statement?

Posted on 2009-05-10
6
Medium Priority
?
1,110 Views
Last Modified: 2012-05-06
What is the shortest, most efficient way to combine the following into one statement, if possible?

Select Word from Wordlist where location = w1 and number = 13
Select Word from Wordlist where location = w2 and number = 22
Select Word from Wordlist where location = w3 and number = 18
Select Word from Wordlist where location = w4 and number = 35
Select Word from Wordlist where location = w5 and number = 13
Select Word from Wordlist where location = w6 and number = 44
Select Word from Wordlist where location = w7 and number = 39
Select Word from Wordlist where location = w8 and number = 8

please note that number is not an identity column, and I cannot go by the identity column at all.
 
Thanks for your help!
0
Comment
Question by:arthurh88
[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
  • 4
  • 2
6 Comments
 
LVL 25

Expert Comment

by:lwadwell
ID: 24351037
Hi arthurh88,

two options come to mind ...

-- 1
Select Word from Wordlist where location = w1 and number = 13
UNION ALL
Select Word from Wordlist where location = w2 and number = 22
UNION ALL
Select Word from Wordlist where location = w3 and number = 18
UNION ALL
Select Word from Wordlist where location = w4 and number = 35
UNION ALL
Select Word from Wordlist where location = w5 and number = 13
UNION ALL
Select Word from Wordlist where location = w6 and number = 44
UNION ALL
Select Word from Wordlist where location = w7 and number = 39
UNION ALL
Select Word from Wordlist where location = w8 and number = 8

--2
Select Word from Wordlist where (location = w1 and number = 13)
or (location = w2 and number = 22)
or (location = w3 and number = 18)
or (location = w4 and number = 35)
or (location = w5 and number = 13)
or (location = w6 and number = 44)
or (location = w7 and number = 39)
or (location = w8 and number = 8)

lwadwell
0
 

Author Comment

by:arthurh88
ID: 24351046
hi there.  are you sure the OR statement is appropriate rather than an AND?

 I need to select all of those items and have 8 values returned.

I'm using an ASP.NET application and I need to grab all of those items from the dataserver, to dump the 8 values into a web page.
0
 

Author Comment

by:arthurh88
ID: 24351111
I need the output to be in the form of columns, W1 through W8.  I tried the UNION statement and it almost looked ok, except that the output was in one single column titled 'Word'.

I want the output to be a single row with Column Names W1, W2, W3, W4, W5, W6, W7, W8
0
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
LVL 25

Accepted Solution

by:
lwadwell earned 2000 total points
ID: 24351144
arthurh88,

First of all, it would need to be an OR, if it was an AND all statements must be true ... and the "number" cannot be all of the values 'at once'.

Leaving that alone ... you want a single row returned with the 8 values as values.  Try ...
SELECT
(Select Word from Wordlist where location = w1 and number = 13) as w1,
(Select Word from Wordlist where location = w2 and number = 22) as w2,
(Select Word from Wordlist where location = w3 and number = 18) as w3,
(Select Word from Wordlist where location = w4 and number = 35) as w4,
(Select Word from Wordlist where location = w5 and number = 13) as w5,
(Select Word from Wordlist where location = w6 and number = 44) as w6,
(Select Word from Wordlist where location = w7 and number = 39) as w7,
(Select Word from Wordlist where location = w8 and number = 8)  as w8

lwadwell
0
 

Author Comment

by:arthurh88
ID: 24351161
thank you iwadwell.  That worked great, and thanks for explaining the OR as well!  
0
 

Author Closing Comment

by:arthurh88
ID: 31580007
bravo!
0

Featured Post

Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

719 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