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
Solved

SQL Select Multiple Items in one statement?

Posted on 2009-05-10
6
1,098 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

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…
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…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

808 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