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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T23:37:43+00:00 2026-06-02T23:37:43+00:00

I know there must be a better way to do this and I’m brain

  • 0

I know there must be a better way to do this and I’m brain dead today.

I have two tables :

Reference
Id         Label
1          Apple
2          Banana
3          Cherry  

Elements
Id    ReferenceId    P1   P2   Qty
1     1              1    2    8
2     2              2    3    14
3     1              3    2    1
4     3              2    1    6
5     3              1    2    3

I want to group these up primarily by (P1, P2) but independent of the order of P1 and P2 – so that (1,2) and (2,1) map to the same group. That’s fine.

The other part is I want to get the label that has the large sum(qty) for a given P1, P2 pair – in other words, I want the result set to be:

 P1   P2  TotalQty  MostRepresentativeLabel
 1    2   17        Cherry
 2    3   15        Banana

All I can come up with is this awful mess:

select endpoint1, endpoint2, totalTotal, mostRepresentativeLabelByQty from 
(
select SUM(qty)as total
,case when (p1<p2) then p1 else p2 end as endpoint1
,case when (p1<p2) then p2 else p1 end as endpoint2
,reference.label as mostRepresentativeLabelByQty

from elements inner join reference on elements.fkId = reference.id
group by case when (p1<p2) then p1 else p2 end
,case when (p1<p2) then p2 else p1 end
,label
) a inner join 
(
select MAX(total) as highestTotal, SUM(total) as totalTotal from 
(
select SUM(qty)as total
,case when (p1<p2) then p1 else p2 end as endpoint1
,case when (p1<p2) then p2 else p1 end as endpoint2
,reference.label as mostRepresentativeLabelByQty

from elements inner join reference on elements.fkId = reference.id
group by case when (p1<p2) then p1 else p2 end
,case when (p1<p2) then p2 else p1 end
,label
) byLabel
group by endpoint1, endpoint2
) b
on a.total = b.highestTotal

Which .. works … but I’m not convinced. This ultimately is going to be running on much larger datasets (200,000 rows or so) so I’m not liking this approach – is there a simpler way to express “use the value from this column where some other column is maximized” that I’m totally blanking on?

(SQL Server 2008 R2 by the way)

  • 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-06-02T23:37:45+00:00Added an answer on June 2, 2026 at 11:37 pm

    I use the sum of the BINARY_CHECKSUM’s of P1 and P2 to uniquely identify each group. This SUM is identified
    by the BC alias, and permits the grouping needed to find the largest group labels.

    DECLARE @Reference TABLE(ID INT, Label VARCHAR(10));
    DECLARE @Elements TABLE(ID INT, ReferenceID INT, P1 INT, P2 INT, Qty INT);
    
    INSERT INTO @Reference VALUES
    (1,'Apple')
    , (2,'Banana')
    , (3,'Cherry');
    
    INSERT INTO @Elements VALUES
    (1,1,1,2,8)
    , (2,2,2,3,14)
    , (3,1,3,2,1)
    , (4,3,2,1,6)
    , (5,3,1,2,3);
    
    ; WITH a AS (
        SELECT
        P1, P2=P2, Qty, BC=ABS(BINARY_CHECKSUM(CAST(P1 AS VARCHAR(10))))+ABS(BINARY_CHECKSUM(CAST(P2 AS VARCHAR(10))))
        , Label
        , LabelSum=SUM(Qty)OVER(PARTITION BY ABS(BINARY_CHECKSUM(CAST(P1 AS VARCHAR(10))))+ABS(BINARY_CHECKSUM(CAST(P2 AS VARCHAR(10)))),Label)
        , GroupSum=SUM(Qty)OVER(PARTITION BY ABS(BINARY_CHECKSUM(CAST(P1 AS VARCHAR(10))))+ABS(BINARY_CHECKSUM(CAST(P2 AS VARCHAR(10)))))
        FROM @Elements e
        INNER JOIN @Reference r on r.ID=e.ReferenceID
    )
    , r AS (
        SELECT *, rnk=RANK()OVER(PARTITION BY BC ORDER BY LabelSum DESC)
        FROM a
    )
    SELECT P1=MIN(P1)
    , P2=MAX(P2)
    , TotalQty=GroupSum
    , MostRepresentativeLabel=Label
    FROM r
    WHERE rnk=1
    GROUP BY GroupSum,Label
    ORDER BY GroupSum DESC;
    GO
    

    Result:

    enter image description here

    EDIT Wrap each BINARY_CHECKSUM in ABS to maximize the entropy of the sums of each group’s BINARY_CHECKSUM. Because BINARY_CHECKSUM is a signed BIGINT, this will decrease
    the chances of a collision between two different groups where a positive BINARY_CHECKSUM is summed with
    a negative BINARY_CHECKSUM.

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

Sidebar

Related Questions

I know there must be a really simple answer to this question, but I
I'm just to figure out what does this method do, I know there must
I know that there must be some differences. I have a Silverlight component (
Does anyone know a good way to solve this other problem I have. My
I know there has to be a way: I need help getting any hot
I know there is ongoing work for in this regards but what is the
I know there are implementations of Python and of Perl for .NET. Have you
I have a little problem because I don't know whats the best way to
Do you know the nicest way to make this work : let toTableau2D (seqinit:seq<'a*'b*'c>)
I know there is a lot of controversy (maybe not controversy, but arguments at

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.