Solved

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

Posted on 2009-05-07
2
322 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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Removing SQL Replication from Microsoft SQL Server 2008 R2 2 23
TSQL - IF ELSE? 3 29
SQL Server stored proc 2 14
Update data using formula 22 23
In this article I will describe the Backup & Restore 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.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

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

23 Experts available now in Live!

Get 1:1 Help Now