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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:07:42+00:00 2026-05-25T22:07:42+00:00

I have 2 tables – 1 with parent records, 1 with child records. For

  • 0

I have 2 tables – 1 with parent records, 1 with child records. For each parent record, I’m trying to return a single child record with the MAX(SalesPriceEach).

Additionally I’d like to only return a value when there is more than 1 child record.

parent - SalesTransactions table:
+-------------------+---------+
|SalesTransaction_ID|   text  |
+-------------------+---------+
| 1                 |  Blah   |
| 2                 |  Blah2  |
| 3                 |  Blah3  |
+-------------------+---------+

child - SalesTransactionLines table 
+--+-------------------+---------+--------------+
|id|SalesTransaction_ID|StockCode|SalesPriceEach|
+--+-------------------+---------+--------------+
| 1|   1               |  123    | 99           |
| 2|   1               |   35    | 50           |
| 3|   2               |   15    | 75           |
+--+-------------------+---------+--------------+


 desired results
+-------------------+---------+--------------+
|SalesTransaction_ID|StockCode|SalesPriceEach|
+-------------------+---------+--------------+
|   1               |  123    | 99           |
|   2               |   15    | 75           |
+-------------------+---------+--------------+

I found a very similar question here, and based my query on the answer but am not seeing the results I expect.

WITH max_feature AS (
   SELECT c.StockCode,
          c.SalesTransaction_ID,
          MAX(c.SalesPriceEach)  as feature
     FROM SalesTransactionLines c
 GROUP BY c.StockCode, c.SalesTransaction_ID)
   SELECT p.SalesTransaction_ID,
          mf.StockCode,
          mf.feature
     FROM SalesTransactions p
LEFT JOIN max_feature mf ON mf.SalesTransaction_ID = p.SalesTransaction_ID

The results from this query are returning multiple rows for each parent, and not even the highest value first!

  • 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-25T22:07:43+00:00Added an answer on May 25, 2026 at 10:07 pm
    select stl.SalesTransaction_ID, stl.StockCode, ss.MaxSalesPriceEach
    from SalesTransactionLines stl
    inner join 
    (
        select stl2.SalesTransaction_ID, max(stl2.SalesPriceEach) MaxSalesPriceEach
        from SalesTransactionLines stl2 
        group by stl2.SalesTransaction_ID
        having count(*) > 1
    ) ss on (ss.SalesTransaction_ID = stl.SalesTransaction_ID and 
             ss.MaxSalesPriceEach = stl.SalesPriceEach)
    

    OR, alternatively:

    SELECT stl1.*
    FROM SalesTransactionLines AS stl1
    LEFT OUTER JOIN SalesTransactionLines AS stl2
    ON (stl1.SalesTransaction_ID = stl2.SalesTransaction_ID 
        AND stl1.SalesPriceEach < stl2.SalesPriceEach)
    WHERE stl2.SalesPriceEach IS NULL;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have tables set up like so: Parent ------ id, ... Child ----- id,
I have tables like these two test_table date student test 2012-05-31 Alice Math 2012-05-31
I have tables like this: tblUsers int UserID string UserName tblUsersInRoles int UserID int
hi i have tables like this: Persons: person_id, name then i have many langauge
I have tables like this in SQL Server Users UserId (Unique) Name Age Friends
I have tables like products product_attributes ================== ======================================== | id | name | |
I have tables like that in Datagridview: Name Money ------------- Hi 100 //here Combobox
I have tables like this: from sqlalchemy import Column, Integer, select from sqlalchemy.ext.declarative import
I have tables like this License ------------ Id LicenseName DriversLicense ------------- Id LicenseId DriverId
I have tables like: 'profile_values' userID | fid | value -------+---------+------- 1 | 3

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.