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

Random selection of records

Posted on 2004-10-05
6
274 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
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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 450 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

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