Solved

Random selection of records

Posted on 2004-10-05
6
270 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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 142

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
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, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

708 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

18 Experts available now in Live!

Get 1:1 Help Now