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

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

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

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…

792 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