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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:56:49+00:00 2026-06-12T09:56:49+00:00

I am having to set up a query that retrieves the last comment made

  • 0

I am having to set up a query that retrieves the last comment made on a customer, if no one has commented on them for more than 4 weeks. I can make it work using the query below, but for some reason the comment column won’t display the latest record. Instead it displays the oldest, however the date shows the newest. It may just be because I’m a noob at SQL, but what exactly am I doing wrong here?

SELECT DISTINCT
    customerid, id, customername, user, MAX(date) AS 'maxdate', comment
FROM comments
WHERE customerid IN 
   (SELECT DISTINCT id FROM customers WHERE pastdue='1' AND hubarea='1') 
AND customerid NOT IN 
   (SELECT DISTINCT customerid FROM comments WHERE DATEDIFF(NOW(), date) <= 27)
GROUP BY customerid
ORDER BY maxdate

The first “WHERE” clause is just ensuring that it shows only customers from a specific area, and that they are “past due enabled”. The second makes sure that the customer has not been commented on within the last 27 days. It’s grouped by customerid, because that is the number that is associated with each individual customer. When I get the results, everything is right except for the comment column…any ideas?

  • 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-12T09:56:50+00:00Added an answer on June 12, 2026 at 9:56 am

    I think this might probably do what you are trying to achieve. If you can execute it and maybe report back if it does or not, i can probably tweak it if needed. Logically, it ‘ should’ work – IF i have understood ur problem correctly 🙂

    SELECT X.customerid, X.maxdate,  co.id, c.customername, co.user, co.comment
    FROM 
        (SELECT customerid, MAX(date) AS 'maxdate'
        FROM comments cm
        INNER JOIN customers cu ON cu.id = cm.customerid
        WHERE cu.pastdue='1' 
        AND cu.hubarea='1' 
        AND DATEDIFF(NOW(), cm.date) <= 27)
        GROUP BY customerid) X
    INNER JOIN comments co ON X.customerid = co.customerid and X.maxdate = co.date
    INNER JOIN customer c  ON X.customerid = c.id
    ORDER BY X.maxdate
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently, I am having one page that display query options and does the form
I'm having the following data set (note that my database/schemata is somewhat complex so
I need to make an outer join query that retrieves all of the definitions
Lets say I have query that gets following List Item Objects with Set of
I have a query that works perfectly in SQL, but I'm having the damnedest
I'm trying to set up a Sybase query that will give me the following
I'm having some issue with this query that seems to be too slow... SELECT
I'm new to linq and I'm having trouble writing a query that pulls back
Here is the issue I am having: I have a large query that needs
I have a query that retrieves location entities within a certain radius given a

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.