Solved

Trying to combine 2 Select statetments

Posted on 2009-04-13
3
170 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 14

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

In this article I will describe the Copy Database Wizard 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.
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

747 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

12 Experts available now in Live!

Get 1:1 Help Now