Solved

SQL Select Multiple Items in one statement?

Posted on 2009-05-10
6
1,104 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

624 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