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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:43:08+00:00 2026-05-13T22:43:08+00:00

Given the following query, how do I return the p_name with the most transactions?

  • 0

Given the following query, how do I return the p_name with the most transactions? And similarly, how do I return the t_amount with the most transactions. I’d like to do it all in this one query of course.

SELECT t.*, p.* 
FROM transactions t
LEFT JOIN partners p ON p.id=t.partner_id

which can return something like:

t_amount     t_platform      t_user     p_id      p_name

100.00       windows         122         20       simmons
200.00       windows         211         20       simmons
100.00       mac             200         18       smith
100.00       linux           190         20       simmons
100.00       mac             100         18       smith

So given that result set, I’d get back best_partner = simmons and also best_amount = 100.00

Thanks!

  • 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-13T22:43:08+00:00Added an answer on May 13, 2026 at 10:43 pm

    I assume “best partner”= the parner with the highest number of transactions and “best amount” = the most frequently occurring transaction amount.

    To count the transactions you can use the Count() function and group by. Something like this:

    SELECT p.name,count(t.id) as transactionCount
    FROM transactions t
    LEFT JOIN partners p ON p.id=t.partner_id
    GROUP BY p.name
    ORDER BY 2 DESC 
    LIMIT 1
    

    Similar for “best amount”:

    SELECT t.amount, Count(t.id) as transactionAmountCount
    FROM transactions t
    LEFT JOIN partners p ON p.id=t.partner_id
    GROUP BY t.amount
    ORDER BY 2 DESC 
    LIMIT 1
    

    Edit: Combined as two sub queries:

    SELECT
    (SELECT p.name
    FROM transactions t
    LEFT JOIN partners p ON p.id=t.partner_id
    GROUP BY p.name
    ORDER BY count(t.id) DESC 
    LIMIT 1) as best_partner
    ,
    (SELECT t.amount
    FROM transactions t
    LEFT JOIN partners p ON p.id=t.partner_id
    GROUP BY t.amount
    ORDER BY Count(t.id) DESC 
    LIMIT 1) as most_occuring_transaction_amount
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given following Statment: String query = "Select * from T_spareParts where SparePartPK IN (?
Given a query like the following: DELETE FROM FOO WHERE ID in (1,2,3,4,....); Is
As shown at: How to limit an SQL query to return at most one
The following query will return the id associated with a given document, a human
Given the following table structure, how can I use a Linq query to return
Given the following MySQL query: SELECT `show`.`id` , GROUP_CONCAT( `showClips`.`clipId` ORDER BY `position` ASC
Given the following query let $a := xs:dateTime(2012-01-01T00:00:00.000+00:00) let $b := xs:dateTime($a) let $c
Given the following recursive query: WITH DepartmentHierarchy (DepartmentID, Name, IsInactive, IsSpecial, ParentId, HierarchyLevel) AS
I have the following SQL query I was given, but I am not sure
I need an sql query to give me the following: All the values in

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.