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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:24:23+00:00 2026-05-24T01:24:23+00:00

I have a vague idea of how to do this using CURSOR s but

  • 0

I have a vague idea of how to do this using CURSORs but am still trying to spend some time thinking about how to do this without using them. I have a table like this:

CREATE TABLE #MATCHEDADDITION(GroupNo int, FirstName varchar(255), Value int)

INSERT INTO #MATCHEDADDITION VALUES(1, 'john', 60)
INSERT INTO #MATCHEDADDITION VALUES(1, 'john', 50)
INSERT INTO #MATCHEDADDITION VALUES(1, 'john', 40)
INSERT INTO #MATCHEDADDITION VALUES(1, 'john', 30)
INSERT INTO #MATCHEDADDITION VALUES(1, 'john', 20)
INSERT INTO #MATCHEDADDITION VALUES(1, 'john', 10)

INSERT INTO #MATCHEDADDITION VALUES(1, 'adam', 80)
INSERT INTO #MATCHEDADDITION VALUES(1, 'adam', 50)
INSERT INTO #MATCHEDADDITION VALUES(1, 'adam', 40)
INSERT INTO #MATCHEDADDITION VALUES(1, 'adam', 30)
INSERT INTO #MATCHEDADDITION VALUES(1, 'adam', 20)
INSERT INTO #MATCHEDADDITION VALUES(1, 'adam', 10)

INSERT INTO #MATCHEDADDITION VALUES(2, 'jill', 60)
INSERT INTO #MATCHEDADDITION VALUES(2, 'jill', 50)
INSERT INTO #MATCHEDADDITION VALUES(2, 'jill', 40)
INSERT INTO #MATCHEDADDITION VALUES(2, 'jill', 30)
INSERT INTO #MATCHEDADDITION VALUES(2, 'jill', 20)
INSERT INTO #MATCHEDADDITION VALUES(2, 'jill', 10)

INSERT INTO #MATCHEDADDITION VALUES(2, 'toni', 90)
INSERT INTO #MATCHEDADDITION VALUES(2, 'toni', 50)
INSERT INTO #MATCHEDADDITION VALUES(2, 'toni', 40)
INSERT INTO #MATCHEDADDITION VALUES(2, 'toni', 30)

INSERT INTO #MATCHEDADDITION VALUES(2, 'tami', 80)
INSERT INTO #MATCHEDADDITION VALUES(2, 'tami', 50)
INSERT INTO #MATCHEDADDITION VALUES(2, 'tami', 40)
INSERT INTO #MATCHEDADDITION VALUES(2, 'tami', 30)
INSERT INTO #MATCHEDADDITION VALUES(2, 'tami', 20)

DROP TABLE #MATCHEDADDITION

which has the following values:

1   john    60
1   john    50
1   john    40
1   john    30
1   john    20
1   john    10
1   adam    80
1   adam    50
1   adam    40
1   adam    30
1   adam    20
1   adam    10
2   jill    60
2   jill    50
2   jill    40
2   jill    30
2   jill    20
2   jill    10
2   toni    90
2   toni    50
2   toni    40
2   toni    30
2   tami    80
2   tami    50
2   tami    40
2   tami    30
2   tami    20

What I am trying to do is to apply an operator on values that are obtained in a certain way: The values in each group should first be aligned column-wise based on the minimum number of records available for a group member. An example should explain this. In the above table, I want to first arrange the values like this:

Members of Group 1
                -
60 80        |
50 50        |
40 40        |    Arrange these like this and apply a custom 
30 30        |    operator on the row elements i.e. say addition 
20 20        |    on 60,80 and 50,50 and 40,40 etc.
10 10        |
                -
Members of Group 2

            -
60 90 80     |
50 50 50     |
40 40 40     |   The size of this is only 4 because that is the min size of a 
30 30 30     |   member 'toni' in this group
            -

I don’t consider myself an expert in SQL but wanted to know if this is even possible to do using a query or I should go for the conventional approach of cursors or perhaps operate offline using a scripting language. Any suggestions?

  • 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-24T01:24:24+00:00Added an answer on May 24, 2026 at 1:24 am

    This gets you six results to work with in Group 1 and 4 results to work with in Group 2. Within each group, the rn column describes “matching” rows. I’m still not sure how to apply the function you’re trying to describe to get to a final output yet (nor even how to derive a column number, cn, to correspond with the row numbers rn assigned):

    ;with Numbered as (
        select *,
          ROW_NUMBER() OVER (
               PARTITION BY GroupNo,FirstName
               ORDER BY Value desc) as rn
        from #MATCHEDADDITION
    ), RNs as (
        select GroupNo,rn,COUNT(*) as Cnt
        from Numbered
        group by GroupNo,rn
    ), MaxGroups as (
        select GroupNo,MAX(rn) as maxRN
        from RNs
        where Cnt = (select MAX(Cnt) from RNs n where n.GroupNo = RNs.GroupNo)
        group by GroupNo
    )
    select
        n.*
    from
        Numbered n
            inner join
        MaxGroups mg
            on
                n.GroupNo = mg.GroupNo and
                n.rn <= mg.maxRN
    

    (I also have a nagging feeling I can make this prettier, use fewer CTEs, etc)

    Results (thus far):

    GroupNo     FirstName Value       rn
    ----------- --------- ----------- --------------------
    1           adam      80          1
    1           adam      50          2
    1           adam      40          3
    1           adam      30          4
    1           adam      20          5
    1           adam      10          6
    1           john      60          1
    1           john      50          2
    1           john      40          3
    1           john      30          4
    1           john      20          5
    1           john      10          6
    2           jill      60          1
    2           jill      50          2
    2           jill      40          3
    2           jill      30          4
    2           tami      80          1
    2           tami      50          2
    2           tami      40          3
    2           tami      30          4
    2           toni      90          1
    2           toni      50          2
    2           toni      40          3
    2           toni      30          4
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have only a vague idea on phrasing this, so question as needed: I
Sorry about the vague question title, but I have these typedefs here: typedef std::list<AnimationKeyframe>
Sorry about the vague title, but I really don't know how to describe this
Anyone have idea how to pass value from iPhone application to the REST-Webservice using
I have a vague understanding of the yield keyword in c# , but I
Sorry about the extremely vague question title (any suggestions for improvements welcome) I have
This is a bit of a vague notion which I have been running over
OK the question title is vague, but here's the problem. I have a list
I apologize if this question is vague, but I want to build a drop
I'm toying with the idea of embarking on a cloud-based client/server spare-time project using

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.