Solved

Trying to combine 2 Select statetments

Posted on 2009-04-13
3
171 Views
Last Modified: 2012-05-06
SELECT     TOP (1) F1
FROM         WordList
WHERE     (F1 > '')
ORDER BY NEWID()


SELECT     TOP (1) F2
FROM         WordList
WHERE     (F2 > '')
ORDER BY NEWID()

I want to take both of those select statements and combine them so that the results are two colums with one row.  
0
Comment
Question by:arthurh88
3 Comments
 
LVL 39

Assisted Solution

by:appari
appari earned 150 total points
ID: 24134238
with A as (SELECT     TOP (1) F1
FROM         WordList
WHERE     (F1 > '')
ORDER BY NEWID()), B AS (
SELECT     TOP (1) F2
FROM         WordList
WHERE     (F2 > '')
ORDER BY NEWID()
)

select * from A,B
0
 
LVL 57

Assisted Solution

by:Raja Jegan R
Raja Jegan R earned 150 total points
ID: 24134279
In a simple manner:

SELECT (SELECT     TOP (1) F1 FROM  WordList WHERE     (F1 > '') ORDER BY NEWID()) F1,
(SELECT     TOP (1) F2 FROM WordList WHERE (F2 > '') ORDER BY NEWID()) F2
0
 
LVL 15

Accepted Solution

by:
rob_farley earned 200 total points
ID: 24134536
Yeah, go for the second one.

But... if your WordList is a large table, ordering by newid() is potentially very expensive. You're generating a new guid for each row, and then going through that list looking for the smallest random value.

It might be better to consider generating a single random number and then finding that row.

Rob
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Recently, when I was asked to create a new SQL 2005 cluster, Microsoft released a new service pack for MS SQL 2005 what is Service Pack 3. When I finished the installation of MS SQL 2005 I found myself troubled why the installation of SP3 failed …
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

863 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

20 Experts available now in Live!

Get 1:1 Help Now