Query Help with date and comments (1 Viewer)

yvesdouville

Registered User.
Local time
Today, 12:09
Joined
May 21, 2014
Messages
24
Hello,

I have this table that has 3 fields: Partners name, Comment Date, and Comment

I want to query all the partners with the latest comment.

Only 1 per partners

Been trying for a while without result. Anyone can help?

I can upload the db if you need

Thank you

Yves
 

ebs17

Well-known member
Local time
Today, 21:09
Joined
Feb 7, 2020
Messages
1,935
SQL:
SELECT
   A.[Partners name],
   A.[Comment Date],
   A.Comment
FROM
   AnyTable AS A
      INNER JOIN
         (
            SELECT
               [Partners name],
               MAX([Comment Date]) AS MaxDate
            FROM
               AnyTable
            GROUP BY
               [Partners name]
         ) AS B
         ON A.[Partners name] = B.[Partners name]
            AND
         A.[Comment Date] = B.MaxDate

Eberhard
 

yvesdouville

Registered User.
Local time
Today, 12:09
Joined
May 21, 2014
Messages
24
OK, this is way out of my league.

Table is Commentaire
Query is Commentaire Requete

Can you help me install the sql in there.

Thank you in advance
 

Attachments

  • Database.zip
    181.6 KB · Views: 95

HiTechCoach

Well-known member
Local time
Today, 14:09
Joined
Mar 6, 2006
Messages
4,357
How are you using the data?

If it will be on a report, you can easily do this with a subreport. The subreport's record source would get the Top 1 comment by date sorted descending by date.
 

yvesdouville

Registered User.
Local time
Today, 12:09
Joined
May 21, 2014
Messages
24
at the end, i need to be able to send the data into an excel sheet
3 columns, only one entry per partner. the latest information



How are you using the data?

If it will be on a report, you can easily do this with a subreport. The subreport's record source would get the Top 1 comment by date sorted descending by date.
 

HiTechCoach

Well-known member
Local time
Today, 14:09
Joined
Mar 6, 2006
Messages
4,357
If you are needing to export the data to excel, then I agree with Eberhard (ebs17). Use a subquery to filter the records work reallyt well.

This may help: Subquery basics
 

yvesdouville

Registered User.
Local time
Today, 12:09
Joined
May 21, 2014
Messages
24
Can you help me make it happen with the database that i sent?

I woud'nt know where to start doing this. I'm not advanced enough.

Sorry


If you are needing to export the data to excel, then I agree with Eberhard (ebs17). Use a subquery to filter the records work reallyt well.

This may help: Subquery basics
 

ebs17

Well-known member
Local time
Today, 21:09
Joined
Feb 7, 2020
Messages
1,935
open the query in the design view
switch to the sql view
replace the full instruction with the new one
adapt the labels for fields and tables to the ones you use
save
switch to the data view

Eberhard
 
Last edited:

CJ_London

Super Moderator
Staff member
Local time
Today, 20:09
Joined
Feb 19, 2013
Messages
16,609
if you only need the partner name and comment (no date) you can use

Code:
SELECT DISTINCT
   [Partners Name],
   (SELECT TOP 1 Comment FROM Commentaire C WHERE [Partners Name]=Commentaire.[Partners Name] ORDER BY [Comment Date] Desc) AS LastComment
FROM Commentaire
 

yvesdouville

Registered User.
Local time
Today, 12:09
Joined
May 21, 2014
Messages
24
Good morning,
I entered the SQL and chaged the labels
But when i run it this prompt come up

I tried several things but it's not working.

What am i doing wrong.

Here is the SQL i did:

SELECT
A.[Clients],
A.[Date du commentaire],
A.Commentaire
FROM
AnyTable AS A
INNER JOIN
(
SELECT
[Clients],
MAX([Date du commentaire]) AS MaxDate
FROM
AnyTable
GROUP BY
[Clients]
) AS B
ON A.[Clients] = B.[Clients]
AND
A.[Comment Date] = B.MaxDate
 

Attachments

  • Capture.PNG
    Capture.PNG
    16.5 KB · Views: 83

yvesdouville

Registered User.
Local time
Today, 12:09
Joined
May 21, 2014
Messages
24
if you only need the partner name and comment (no date) you can use

Code:
SELECT DISTINCT
   [Partners Name],
   (SELECT TOP 1 Comment FROM Commentaire C WHERE [Partners Name]=Commentaire.[Partners Name] ORDER BY [Comment Date] Desc) AS LastComment
FROM Commentaire


Thank you for your reply, very interestig, but i do need to have the date as i need to nnow when is the last time i called the customer

Trying to figure out what i'm doing wrong with the other SQL
 

ebs17

Well-known member
Local time
Today, 21:09
Joined
Feb 7, 2020
Messages
1,935
Your original table is named "AnyTable"?

In addition to copying, you could also try to understand something about the SQL statement.
Alternatively, you would have to be very humble with your wishes.

Eberhard
 

CJ_London

Super Moderator
Staff member
Local time
Today, 20:09
Joined
Feb 19, 2013
Messages
16,609
do you really have a table called 'anytable'?
 

yvesdouville

Registered User.
Local time
Today, 12:09
Joined
May 21, 2014
Messages
24
do you really have a table called 'anytable'?
No, i saw my error. I changed it, but still not working

Here is a snapshot of what i have so far. I do understand a little as i did some dB course a long time ago. But it's not my profession
 

Attachments

  • Capture.PNG
    Capture.PNG
    22.4 KB · Views: 82

CJ_London

Super Moderator
Staff member
Local time
Today, 20:09
Joined
Feb 19, 2013
Messages
16,609
what does 'not working' mean? This is Eberhard's solution, so I'll leave him to work with you on it
 

yvesdouville

Registered User.
Local time
Today, 12:09
Joined
May 21, 2014
Messages
24
I tried your version and i end up with this screen (A) when i run it. Same as the other SQL from Eberhard

B is the code
C is the table

Is it because i use a french version of Acces?


if you only need the partner name and comment (no date) you can use

Code:
SELECT DISTINCT
   [Partners Name],
   (SELECT TOP 1 Comment FROM Commentaire C WHERE [Partners Name]=Commentaire.[Partners Name] ORDER BY [Comment Date] Desc) AS LastComment
FROM Commentaire
 

Attachments

  • Capture A.PNG
    Capture A.PNG
    30.8 KB · Views: 80
  • Capture B.PNG
    Capture B.PNG
    8 KB · Views: 79
  • Capture C.PNG
    Capture C.PNG
    24.1 KB · Views: 77

ebs17

Well-known member
Local time
Today, 21:09
Joined
Feb 7, 2020
Messages
1,935
One should work precisely: Comment Date or Date du Commentaire => last line
The jet engine does not understand a mishmash.

As well as: Clients is defined as a memo (long text). Why? Memo creates a lot of problems, so when grouping and linking.
Fields that come into question for such uses should definitely be able to use text. I would be interested in the special reason why you really need memo.

Eberhard
 

yvesdouville

Registered User.
Local time
Today, 12:09
Joined
May 21, 2014
Messages
24
One should work precisely: Comment Date or Date du Commentaire => last line
The jet engine does not understand a mishmash.

As well as: Clients is defined as a memo (long text). Why? Memo creates a lot of problems, so when grouping and linking.
Fields that come into question for such uses should definitely be able to use text. I would be interested in the special reason why you really need memo.

Eberhard


I don't need Memo. I have Long text on my end. What should i use instead?
 

ebs17

Well-known member
Local time
Today, 21:09
Joined
Feb 7, 2020
Messages
1,935
long text = memo (until about 64.000 characters)
short text = text (max. 255 characters)

Eberhard
 

Users who are viewing this thread

Top Bottom