Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2009-05-07
2
Medium Priority
?
330 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
[X]
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
2 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 1000 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

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…
Ready to get certified? Check out some courses that help you prepare for third-party exams.
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.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

722 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