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

  • SEARCH
  • Home
  • 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 4015732
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:39:17+00:00 2026-05-20T09:39:17+00:00

From Table Name GroupID a Null b 1 c 1 d 2 e Null

  • 0

From Table

Name GroupID 
a      Null
b       1
c       1
d       2
e      Null
f      Null
g       3

Result expected from random top 4 selection

Name GruopID
a  Null
b   1
e  Null
g   3

Resuming I want to get random names but only 1 kind of groupid if groupid is <> null
The select Newid() type returns
“Select Top(4) * FROM Table Order By NEWID()”

Name GruopID
a  Null
b   1
e  Null
c   1

I don´t want that. Hope i made my self clear!
Thanks in advance

  • 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-20T09:39:18+00:00Added an answer on May 20, 2026 at 9:39 am

    You can try this (on SQL Server 2005+).

    ;WITH CTE AS
    (
        SELECT *, ROW_NUMBER() OVER(PARTITION BY GroupId ORDER BY NEWID()) Corr
        FROM YourTable
    )
    
    SELECT TOP 4 Name, GroupId
    FROM CTE
    WHERE GroupId IS NULL OR Corr = 1
    ORDER BY NEWID()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For example, mysql quote table name using SELECT * FROM `table_name`; notice the `
According to select name from system_privilege_map System has been granted: SELECT ANY TABLE ...and
I have parent-child information in a table with GroupID (TreeID.) From this table I
$table = new Zend_Db_Table(array('name'=>'rules')); $select = $table->select(); $select->setTable($table); $select->setIntegrityCheck(false); $select = $select ->from(array('ru'=>'rules'),array('ru.*')) ->join(array('ro'=>'roles'),'ro.id=ru.role_id',array('role_id'=>'ro.id'))
i've being trying this without success: select * from table where name regexp '^[:alpha:]{2}$'
I have the following query: select column_name, count(column_name) from table group by column_name having
How do you rewrite this in Linq? SELECT Id, Name FROM TableA WHERE TableA.Id
I need a select from table which does not have column that tells when
Ever since switching from TABLE-layout to DIV-layout, one common problem remains: PROBLEM : you
I have a SqlServer2005 table Group similar to the following: Id (PK, int) Name

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.