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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:49:48+00:00 2026-05-26T16:49:48+00:00

I have a table which represents a Contract between two rows in another table.

  • 0

I have a table which represents a “Contract” between two rows in another table. Given the data below, how can I get the most common Distributer for each EndUser?

Contracts

EndUserId | DistributerId | StartDate | EndDate 
-----------------------------------------
1         | 8             | ...       | ...
1         | 9             | ...       | ...
1         | 9             | ...       | ...
2         | 8             | ...       | ...
2         | 8             | ...       | ...
2         | 9             | ...       | ...   
3         | 8             | ...       | ...   
3         | 9             | ...       | ...   

The query I’m after must return the following:

EndUserId | DistributerId
-------------------------
1         | 9
2         | 8
3         | 8 or 9, it is of no consequence.

Thanks in advance! Searching hasn’t been much help here because it’s hard to describe the goal without sample data etc.

  • 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-26T16:49:48+00:00Added an answer on May 26, 2026 at 4:49 pm

    Untested, but I think this will do it:

    WITH ContractCounts AS 
    ( --First Get the counts for each distributer
      SELECT EndUserID, DistributerID, Count(*) As ContractCount
      FROM Contracts
      GROUP BY EndUserID, DistributerID
    ),
    ContractMax AS 
    (  -- Then find out how many contracts the largest distributed for each user had
      SELECT EndUserID, Max(ContractCount) As MaxContractCount
      FROM ContractCounts 
      GROUP BY EndUserID
    )
    -- and finally select only the distributor for each user who's count matches the prior query
    SELECT c.EndUserID, MAX(c.DistributerID) AS DistributerID
    FROM ContractCounts c
    INNER JOIN ContractMax m on m.EndUserID=c.EndUserID AND c.ContractCount = m.MaxContractCount
    GROUP BY c.EndUserID
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table (which represents a queue of items) whose rows can be
I have a table of data which represents a series of events that persons
We have a (currently InnoDB) table which contains roughly 500,000 rows. This represents a
I have a table which represents a line-by-line dump of the data read from
I have a very large (millions of rows) SQL table which represents name-value pairs
If I have a table in MySQL which represents a base class, and I
I have a html table in which each line ( <tr> ) represents a
I have a table which defines a child-parent relationship between nodes: CREATE TABLE node
I have a table which holds flight schedule data. Every schedule have an effective_from
I have a table which has two varchar(Max) columns Column 1 Column 2 -----------------------

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.