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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T17:05:57+00:00 2026-06-16T17:05:57+00:00

I have a 1 to many relation between two tables that I want to

  • 0

I have a 1 to many relation between two tables that I want to combine in a record. Let’s say I have one table called Foo and another table called Bar. Foo contains some content and Bar contains comments to this content. Now I want to list all contents together with the first comment.

This is what I have tried so far:

SELECT T0.[Content], T1.[Comment]
FROM [Foo] I0 
    INNER JOIN [Bar] T1 ON T1.[ContentCode] = T0.[Code]

That gives me all contents multiple times with many comments. In other words I am saying “Combine all comments with their representing content”. What I am trying to achieve is to say “Only combine one comment with the content”. So I tried this:

SELECT DISTINCT T1.[CommentCode], T0.[Content], T1.[Comment]
FROM [Foo] I0 
    INNER JOIN [Bar] T1 ON T1.[ContentCode] = T0.[Code]

This works fine. Using ORDER BY I can tell the query to use either the first or the last record found in Bar.

But for some reason DISTINCT is not supported by the database adapter we are forced to use.

Do you face another possibility to limit the results of the right side of the joined relation?

Thanks in advance! 🙂

EDIT:

I am on MSSQL Server 2008 (R2), but accessing it using the SAP Business One DI API. This limits the usage of T-SQL.

  • 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-16T17:05:58+00:00Added an answer on June 16, 2026 at 5:05 pm

    A fairly portable way is to query a single comment’s ID in a subquery:

    SELECT  T0.Content
    ,       T1.Comment
    FROM    Foo I0 
    JOIN    Bar T1 
    ON      T1.ID = 
            (
            SELECT  max(ID)
            FROM    Bar T2
            WHERE   T2.ContentCode = T0.Code
            )
    

    If SQL Server executes the subquery for every row, consider this version instead:

    SELECT  T0.Content
    ,       T1.Comment
    FROM    Foo T0
    JOIN    (
            SELECT  ContentCode
            ,       max(ID) as MaxID
            FROM    Bar
            GROUP BY
                    ContentCode
            ) filter
    ON      filter.ContentCode = T0.ContentCode
    JOIN    Bar T1
    ON      T1.ID = filter.MaxID
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two classes with many-to-many relation so I created a join table in-between
I have the following one-to-many relation between two objects. Parent --> IList<Child> Now, I
I have a many to many relation between two tables in my database (MySql)
I have a many-to-one relation mapped between two entities A and B. I will
I am trying to create a relation/table in Oracle that is between two many
In my DB I have two relation between two tables : A many to
I have a question about One-To-Many relationship between two tables when there is the
I have a one-to-many relation between Parent and Child table. In the parent object
i have an entity Entity1 that have one to many relation with Entity2 as
I have 3 tables: Book, Section and Content. I want to add many-to-many relation

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.