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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:01:12+00:00 2026-05-23T18:01:12+00:00

To begin with I have 4 tables I am dealing with. I have a

  • 0

To begin with I have 4 tables I am dealing with.

I have a classes table that is a 1->N relationship with a sections table which also has a 1->N relationship with a lessons table.

So to put it in perpective:

  • Classes
    • Sections
      • Lessons

The last table is an activityLog, when the student accesses a lesson this is recorded using the following:

ActivityLog Row -> actorID (user ID), classID, sectionID, lessonID

I want to pull out the last 5 unique lessons the student has visited. I tried using both DISTINCT and GROUP BY without success.

The same records are being returned each time, not the latest classes that they have visited.

Using GROUP BY

SELECT activityLog.actorID, activityLog.activityDate,
        strClasses.classID, strClasses.className,
        strSections.sectionID, strSections.sectionName,
        strLessons.lessonID, strLessons.lessonName

FROM activityLog            
LEFT JOIN strClasses ON strClasses.classID = activityLog.classID
LEFT JOIN strSections ON strSections.sectionID = activityLog.sectionID
LEFT JOIN strLessons ON strLessons.lessonID = activityLog.lessonID
WHERE activityLog.activityTypeID = 6 AND activityLog.actorID = 3
GROUP BY activityLog.lessonID
ORDER BY activityLog.activityDate DESC
LIMIT 5

Using DISTINCT

SELECT DISTINCT activityLog.actorID,
        strClasses.classID, strClasses.className,
        strSections.sectionID, strSections.sectionName,
        strLessons.lessonID, strLessons.lessonName
FROM activityLog            
LEFT JOIN strClasses ON strClasses.classID = activityLog.classID
LEFT JOIN strSections ON strSections.sectionID = activityLog.sectionID
LEFT JOIN strLessons ON strLessons.lessonID = activityLog.lessonID
WHERE activityLog.activityTypeID = 6 AND activityLog.actorID = 3
ORDER BY activityLog.activityDate DESC
LIMIT 5

I cannot figure out why the latest records are not being displayed.

  • 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-23T18:01:13+00:00Added an answer on May 23, 2026 at 6:01 pm

    Based on your change, how does this suit you?

    SELECT activityLog.actorID, activityLog.activityDate,
            strClasses.classID, strClasses.className,
            strSections.sectionID, strSections.sectionName,
            strLessons.lessonID, strLessons.lessonName
    FROM activityLog            
    LEFT JOIN strClasses ON strClasses.classID = activityLog.classID
    LEFT JOIN strSections ON strSections.sectionID = activityLog.sectionID
    LEFT JOIN strLessons ON strLessons.lessonID = activityLog.lessonID
    WHERE activityLog.activityTypeID = 6 AND activityLog.actorID = 3
    AND activityLog.activityDate = (SELECT MAX(activityDate) FROM activityLog AS lookup WHERE lessonID = activityLog.lessonID)
    ORDER BY activityLog.activityDate DESC
    LIMIT 5
    

    Based on your description, I’m not sure why you’re using LEFT JOIN, but I’ve left it in just in case.

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

Sidebar

Related Questions

I have a LaTeX table that looks like this: \begin{table}[!ht] \centering \small \caption{ \bf{Caption}}
I have periods table that has start_at:datetime end_at:datetime . Because each period begins and
So, I've got some history tables that have a begin date and an end
I have three tables : ImportBatch PatientChartImages PatientChartBatchMapping (this table has foreign key to
I have two tables, Table_A and Table_B. Table_A has a one-to-many relationship with Table_B.
I have the following SQL Server stored procedure : BEGIN TRAN CREATE TABLE #TempTable
I have the following awk command which works perfectly: awk 'BEGIN { ORS =
So here's the setup. I have two tables: CREATE TABLE dbo.TmpFeesToRules1(Name varchar, LookupId int)
I have two tables with lists of files: table1 has column1 with file names
I have 3 tables ( tblPreference , tblCustomer , tblCustomerPreference ) that look something

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.