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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:54:33+00:00 2026-06-18T07:54:33+00:00

I wrote a query in sql, where I will keep just one time the

  • 0

I wrote a query in sql, where I will keep just one time the Companies.ID, that is why I use Distinct.

SELECT DISTINCT Companies.ID, Companies.Company, Abo.Ende 
FROM Companies
LEFT JOIN Abo on Companies.ID = Abo.CompanyID 
ORDER BY Abo.Ende

The table Abo.Ende has more entries for the Abo.CompanyID and I would like to get as result the distinct Companies.ID with the most recent Abo.Ende. Note: Abo.Ende is a datetime field and I would like to get the newest.

I am wondering if this is the best way to do that. Is there another 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-18T07:54:35+00:00Added an answer on June 18, 2026 at 7:54 am

    You need group by, not distinct:

    SELECT Companies.ID, Companies.Company, max(Abo.Ende)
    FROM Companies
    LEFT JOIN Abo on Companies.ID = Abo.CompanyID 
    GROUP BY Companies.ID, Companies.Company
    

    A comment on your DISTINCT usage:

    DISTINCT operates on the whole row. So it will return distinct combinations of all column values in a single row. If you include a PK column (Probably companies.id in your case), distinct would not “remove” any rows as the PK is unique by definition and if one column is unique across all rows, then all the columns of the result are “unique” (in the sense of the DISTINCT operator) as well.

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

Sidebar

Related Questions

How would I write a SQL query that excludes a record if one (external)
My question is Write an SQL query that will determine which is the least
I need to write a SQL query that will generate a list of valid
I have to perform the following SQL query: select answer_nbr, count(distinct user_nbr) from tpoll_answer
Write a SQL query that will display the smallest, largest and range of values
This is actually the first time that i try to use a SQL database
I need to write a SQL query which will get me those rows from
A query I wrote in PHP for a SQL Server database is having an
I'm trying to write an SQL query that would search within a CSV (or
I've wrote HQL query like this: SELECT a FROM A a LEFT JOIN a.b

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.