[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Random selection of records

Posted on 2004-10-05
6
Medium Priority
?
293 Views
Last Modified: 2008-03-06
hi

Is there any way to select a random, top "n"  no: of records from a table so that records with certain values are compulsory..

this is the view ..

CREATE VIEW dbo.QN_RANDOM_ORDER
AS
SELECT   TOP 15 CTestQuestionId, CTestQBQuestionComp
FROM         dbo.WHIRE_CTestQuestion
WHERE     (CTestId = 4)
ORDER BY CTestQBQuestionComp DESC, NEWID()

Here records with "CTestQBQuestionComp=1" is compulsory. the condition is that if we have "n" no: of questions to be selected, and we have enough no: of qns with CTestQBQuestionComp=1, others will not be selected.

using above view compulsory records are listed first and i do not want that. Again we can randomly select from this view to avoid this, here the problem is no: of qns to be selected are not fixed.

Any idea

thanks in Advance
Riyasjef
0
Comment
Question by:riyasjef
[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
6 Comments
 
LVL 6

Expert Comment

by:RaisinJ
ID: 12226323
If the number of questions being pulled back is dynamic, maybe you should be using a Stored Procedure instead of a view to pull this data.  What does "CTestID" represent in the WHIRE_CTestQuestion table?  From what I understand, you trying to pull records which contain a certain ID.  If the record count in not enough, then pull back more records with different ID values, if the record count in enough, then pull back no more records.

Am I understanding this correctly?
0
 
LVL 12

Expert Comment

by:ill
ID: 12226371
-- you nedd unin i think, but i'm not sure about the synatax
CREATE VIEW dbo.QN_RANDOM_ORDER
AS
select top 15 * from
(
SELECT   TOP 15 CTestQuestionId, CTestQBQuestionComp
FROM         dbo.WHIRE_CTestQuestion
WHERE     (CTestId = 4 AND CTestQBQuestionComp=1)
UNION
SELECT   TOP 15 CTestQuestionId, CTestQBQuestionComp
FROM         dbo.WHIRE_CTestQuestion
WHERE     (CTestId = 4 )

ORDER BY CTestQBQuestionComp DESC, NEWID()
) as a
0
 
LVL 9

Author Comment

by:riyasjef
ID: 12226422
hi
"CTestQuestionId" represents the table.
So I have to query twice?

riyasjef
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 12

Expert Comment

by:ill
ID: 12226536
-- in first approach yes, sorry not to be more helpful, but i'm in a hurry now( as always).
if i understand correcttly:
if  CTestQBQuestionComp=1 exists, you want them
else display random from CTestQBQuestionComp="n"

-- just a try
SELECT   TOP 15 CTestQuestionId, CTestQBQuestionComp
FROM         dbo.WHIRE_CTestQuestion
WHERE     (CTestId = 4)
ORDER BY case CTestQBQuestionComp when 1 then CTestQBQuestionComp DESC  else NEWID() end
0
 
LVL 9

Author Comment

by:riyasjef
ID: 12226703
Actually CTestQuestionId is of type "int" and
CTestQuestionComp - bit

here the

Error

---------------------------
SQL Server Enterprise Manager
---------------------------
[Microsoft][ODBC SQL Server Driver][SQL Server]Implicit conversion from data type uniqueidentifier to bit is not allowed. Use the CONVERT function to run this query.
---------------------------
OK   Help  
---------------------------


Riyasjef
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 1800 total points
ID: 12227130
What about this:

CREATE VIEW dbo.QN_RANDOM_ORDER
AS
select L.* from
(
SELECT   TOP 15 CTestQuestionId, CTestQBQuestionComp
FROM         dbo.WHIRE_CTestQuestion
WHERE     (CTestId = 4)
ORDER BY CTestQBQuestionComp DESC, NEWID()
) as L
ORDER BY NEWID()

Cheers
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
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…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

650 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