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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T07:52:49+00:00 2026-05-12T07:52:49+00:00

I have a data-analysis question, that I could easily solve with some T-SQL or

  • 0

I have a data-analysis question, that I could easily solve with some T-SQL or some scripting, but I was wondering if there was a clever SQL solution. The problem is that it messes a bit with SQL’s row-independence assumption a bit.

I have a table that consists of name-value pairs associated with a user and ordered by submission, for example:

ID      USERID  VARIABLE        VALUE   SUBMITTED
3115    2287    votech05    2   2009-02-02 15:34:00
3116    2287    comcol05    1   2009-02-02 15:34:00
3117    2287    fouryr05    1   2009-02-02 15:35:00
3118    2287    none05          2   2009-02-02 15:35:00
3119    2287    ocol1_05    2   2009-02-02 15:44:00
3120    2287    disnone         2   2009-02-02 15:45:00
3121    2287    dissense    2   2009-02-02 15:49:00
3122    2287    dismobil    3   2009-02-02 15:51:00
3123    2287    dislearn    3   2009-02-02 15:51:00
3124    2287    disment         3   2009-02-02 15:52:00
3125    2287    disother    2   2009-02-02 15:55:00
3126    2287    disrefus    7   2009-02-02 15:58:00

I’d like to be able to determine the value and count of the largest group of identical values (when the data is ordered the ID primary key). So, for the above example, because I have four value=2 appearing in sequence, and only three value=3, I would want to report:

USERID     VALUE      COUNT
2287       2          4

for the given user.

Again, this would could be done fairly-quickly using other tools, but since the data set is quite large (about 75 million records) and frequently changing, it would be nice to be able to solve this problem with a query. I’m working with SQL Server 2005.

  • 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-12T07:52:49+00:00Added an answer on May 12, 2026 at 7:52 am

    (Edited after comment)

    You can do that by assigning a “head” number to each group of consecutive values. After that you select the head number for each row, and do an aggregate per head.

    Here’s an example, with CTE’s for readability:

    WITH
    OrderedTable as (
        select value, rownr = row_number() over (order by userid, id)
        from YourTable
        where userid = 2287
    ),
    Heads as (
        select cur.rownr, CurValue = cur.value
        ,   headnr = row_number() over (order by cur.rownr)
        from OrderedTable cur
        left join OrderedTable prev on cur.rownr = prev.rownr+1 
        where IsNull(prev.value,-1) != cur.value
    ),
    ValuesWithHead as (
        select value
        ,   HeadNr = (select max(headnr) 
                    from Heads 
                    where Heads.rownr <= data.rownr)
        from OrderedTable data
    )
    select Value, [Count] = count(*)
    from ValuesWithHead
    group by HeadNr, value
    order by count(*) desc
    

    This will output:

    Value   Count
    2       4
    3       3
    1       2
    2       1
    2       1
    7       1
    

    Use “top 1” to select the first row only.

    Here’s my query to create the test data:

    create table YourTable (
        id int primary key,
        userid int,
        variable varchar(25),
        value int
    )
    insert into YourTable (id, userid, variable, value) values (3115, 2287, 'votech05', 2)
    insert into YourTable (id, userid, variable, value) values (3116, 2287, 'comcol05', 1)
    insert into YourTable (id, userid, variable, value) values (3117, 2287, 'fouryr05', 1)
    insert into YourTable (id, userid, variable, value) values (3118, 2287, 'none05', 2)
    insert into YourTable (id, userid, variable, value) values (3119, 2287, 'ocol1_05', 2)
    insert into YourTable (id, userid, variable, value) values (3120, 2287, 'disnone', 2)
    insert into YourTable (id, userid, variable, value) values (3121, 2287, 'dissense', 2)
    insert into YourTable (id, userid, variable, value) values (3122, 2287, 'dismobil', 3)
    insert into YourTable (id, userid, variable, value) values (3123, 2287, 'dislearn', 3)
    insert into YourTable (id, userid, variable, value) values (3124, 2287, 'disment', 3)
    insert into YourTable (id, userid, variable, value) values (3125, 2287, 'disother', 2)
    insert into YourTable (id, userid, variable, value) values (3126, 2287, 'disrefus', 7)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer To set the 'Escape' key to activate your button to… May 12, 2026 at 10:53 am
  • Editorial Team
    Editorial Team added an answer Check the documentation, and make sure you link to the… May 12, 2026 at 10:53 am
  • Editorial Team
    Editorial Team added an answer I emailed Tim Cools of the Custom Templating project on… May 12, 2026 at 10:53 am

Related Questions

Using SQL Server 2005 developer edition on Windows XP pro (32-bit) I notice that
I have an application that is scheduled and get some data from a another
I'm a modest graduate student in a high energy particle physics department. With an
We're embarking on a project for a client. They plan on having about 50k
I have a nightly build DOS batch script that invokes devenv.exe to build a

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.