Solved

SQL Select Multiple Items in one statement?

Posted on 2009-05-10
6
1,091 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
  • 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 25

Accepted Solution

by:
lwadwell earned 500 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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Join & Write a Comment

Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now