Solved

How do I randomize the DISTINCT TOP(4) returned SQL rows for this query?

Posted on 2009-05-07
2
319 Views
Last Modified: 2012-05-06
Hi, I'm trying to develop an SQL query that returns 4 random rows.

Basically it looks at 4 entries found in eord_line and then returns all matching items from epar_rels. It will return around 12 results but II only want 4 distinct randomly selected ones.

Any ideas as I can't use DISTINCT with ORDER BY NEWID().

Thanks
SELECT     TOP (4) epar_rels.relative, plis_brks.price, epar.descr

FROM         epar_rels LEFT OUTER JOIN

                      eord_line ON epar_rels.part = eord_line.part LEFT OUTER JOIN

                      plis_brks ON epar_rels.relative = plis_brks.part LEFT OUTER JOIN

                      epar_stoc ON epar_rels.relative = epar_stoc.part LEFT OUTER JOIN

                      epar ON epar_rels.relative = epar.part

WHERE     (eord_line.ref_no = 'TEST000663') AND (epar_stoc.stock > 0) AND (plis_brks.price_list = 'Q42004WEB')

ORDER BY NEWID()

Open in new window

0
Comment
Question by:joshgeake
2 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 250 total points
ID: 24323573
try this;:
SELECT TOP 4 *
FROM (
SELECT     DISTINCT epar_rels.relative, plis_brks.price, epar.descr
FROM         epar_rels LEFT OUTER JOIN
                      eord_line ON epar_rels.part = eord_line.part LEFT OUTER JOIN
                      plis_brks ON epar_rels.relative = plis_brks.part LEFT OUTER JOIN
                      epar_stoc ON epar_rels.relative = epar_stoc.part LEFT OUTER JOIN
                      epar ON epar_rels.relative = epar.part
WHERE     (eord_line.ref_no = 'TEST000663') AND (epar_stoc.stock > 0) AND (plis_brks.price_list = 'Q42004WEB')
) sq
ORDER BY NEWID()

Open in new window

0
 
LVL 2

Author Closing Comment

by:joshgeake
ID: 31578911
Super, thanks :D
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

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…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

757 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

17 Experts available now in Live!

Get 1:1 Help Now