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 6158019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:53:02+00:00 2026-05-23T20:53:02+00:00

Technologies: SQL Server 2008 So I’ve tried a few options that I’ve found on

  • 0

Technologies: SQL Server 2008

So I’ve tried a few options that I’ve found on SO, but nothing really provided me with a definitive answer.

I have a table with two columns, (Transaction ID, GroupID) where neither has unique values. For example:

TransID | GroupID
-----------------
23      | 4001
99      | 4001
63      | 4001
123     | 4001   
77      | 2113
2645    | 2113
123     | 2113
99      | 2113   

Originally, the groupID was just chosen at random by the user, but now we’re automating it. Thing is, we’re keeping the existing DB without any changes to the existing data(too much work, for too little gain)

Is there a way to query “GroupID” on table “GroupTransactions” for the next available value of GroupID > 2000?

  • 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-23T20:53:02+00:00Added an answer on May 23, 2026 at 8:53 pm

    I think from the question you’re after the next available, although that may not be the same as max+1 right? – In that case:

    Start with a list of integers, and look for those that aren’t there in the groupid column, for example:

    ;WITH CTE_Numbers AS (
        SELECT n = 2001
        UNION ALL
        SELECT n + 1 FROM CTE_Numbers WHERE n < 4000 
    )
    SELECT top 1 n 
    FROM CTE_Numbers num
    WHERE NOT EXISTS (SELECT 1 FROM MyTable tab WHERE num.n = tab.groupid)
    ORDER BY n
    

    Note: you need to tweak the 2001/4000 values int the CTE to allow for the range you want. I assumed the name of your table to by MyTable

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

Sidebar

Related Questions

I've been trying to specialize into the MS SQL server technologies, but my job
I am using SQL Server 2008 Enterprise. And I am using Linked Server technologies
We have a normalized SQL Server 2008 database designed using generic tables. So, instead
I am currently crossing the jungle of SQL Server scale-out technologies like replication, log-shipping,
We currently have a 10 year old nasty, spaghetti-code-style SQL Server database that we
Using Sql Server 2008, .Net 3.5 & C#. I am designing a desktop application
I've asked about each of these technologies separately, and really haven't found a suitable
I realize that this question spans many technologies, but I am only looking for
I am using these technologies: SQL Server 2005, ASP.NET MVC, NHibernate/sharp architecture and would
Our product currently spans a large number of technologies, including Java, PL/SQL, VB.Net and

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.