Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 284193
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T05:26:15+00:00 2026-05-12T05:26:15+00:00

I need to get 5 random records from a table plus a further record

  • 0

I need to get 5 random records from a table plus a further record based on data from the users preferences as stored in a second table.

Here are the two statements i have created so far:

Gets favourite charity based on key set in TBL_MEMBERS:

SELECT DISTINCT TBL_CHARITIES.* FROM TBL_CHARITIES JOIN TBL_MEMBERS ON TBL_CHARITIES.cha_Key = TBL_MEMBERS.members_Favourite WHERE TBL_MEMBERS.members_Id = 16

Gets 5 random charities:

SELECT TOP 5 * FROM TBL_CHARITIES WHERE cha_Active = ‘TRUE’ AND cha_Key != ‘1’ ORDER BY NEWID();

When used in a stored procedure it only returns the first SELECT statement to my .Net page. How can i combine the two statements whilst ensuring that no results are repeated (Eg the favourite is not present in the 5 random records?

Many Thanks!


Ok! So now ive updated things and got the following:

CREATE PROCEDURE web.getRandomCharities ( @tmp_ID bigint --members ID ) AS BEGIN

WITH    q AS
 (
 SELECT  TOP 5 *
 FROM    TBL_CHARITIES
 WHERE   cha_Active = 'TRUE'
         AND cha_Key != '1'
 ORDER BY NEWID()
 )

SELECT * FROM q 
 UNION ALL 
 SELECT TOP 1 * FROM ( 
         SELECT * FROM TBL_CHARITIES WHERE TBL_CHARITIES.cha_Key IN 
         ( SELECT members_Favourite FROM TBL_MEMBERS WHERE members_Id = @tmp_ID )        EXCEPT SELECT * FROM q ) tc

END

Now i need to be able to the record “cha_Key == ‘1’” but only if its not the users favourite. Is this possible?

Thanks for everything so far. ITs truly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T05:26:15+00:00Added an answer on May 12, 2026 at 5:26 am

    Just UNION ALL them:

     WITH    q AS
             (
             SELECT  TOP 5 *
             FROM    TBL_CHARITIES
             WHERE   cha_Active = 'TRUE'
                     AND cha_Key != '1'
             ORDER BY NEWID()
             )
     SELECT  *
     FROM    q
     UNION ALL
     SELECT  TOP 1 *
     FROM    (
             SELECT  *
             FROM    TBL_CHARITIES
             WHERE   TBL_CHARITIES.cha_Key IN
                     (
                     SELECT  members_Favourite
                     FROM    TBL_MEMBERS
                     WHERE   members_Id = 16
                     )
             EXCEPT
             SELECT  *
             FROM    q
             ) tc
    

    Update:

    Unfortunately, the query above will not work as intended, since CTE‘s are reevaluated in SQL Server, and the second instance of q will give different records.

    See this post in my blog for more detail:

    • SQL Server: random records avoiding CTE reevaluation

    You need to rewrite the query as this:

     WITH    q AS
             (
             SELECT  TOP 5 *
             FROM    TBL_CHARITIES
             WHERE   cha_Active = 'TRUE'
                     AND cha_Key != '1'
             ORDER BY
                     NEWID()
             ),
             r AS
             (
             SELECT  *
             FROM    TBL_CHARITIES
             WHERE   TBL_CHARITIES.cha_Key IN
                     (
                     SELECT  members_Favourite
                     FROM    TBL_MEMBERS
                     WHERE   members_Id = 16
                     )
             )
     SELECT  TOP 6 *
     FROM    q
     FULL OUTER JOIN
             r
     JOIN    TBL_CHARITIES t
     ON      t.id = COALESCE(q.id, r.id)
     ORDER BY
             q.id
    

    , assuming that id is the PRIMARY KEY of TBL_CHARITIES.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 312k
  • Answers 312k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Well, this works: using System; using System.Configuration; using System.IO; using… May 13, 2026 at 10:37 pm
  • Editorial Team
    Editorial Team added an answer LINQ to objects for this scenario? You can do a… May 13, 2026 at 10:37 pm
  • Editorial Team
    Editorial Team added an answer "GPL-compatible" only matters if you're writing GPLed or LGPLed code.… May 13, 2026 at 10:37 pm

Related Questions

I need to submit data from a form to an SQL2005 database via a
I have written a function that gets a given number of random records from
I'm joining 3 tables. the last one contains several photos per item. The idea
I need a quick algorithm to select 5 random elements from a generic list.

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.