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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T18:27:30+00:00 2026-05-24T18:27:30+00:00

I have a third party table that is being populated with some cluttered data

  • 0

I have a third party table that is being populated with some cluttered data that I’m needing to get the most recent distinct records out of. The table will be fed a new row every year, or every time the “Person” changes. The table works based on that the most recent ActiveDate is the correct person. I’ve created a mock table and data to show this.

CREATE TABLE `Persons` (
  `PersonId` varchar(200) NOT NULL,
  `Name` varchar(200) NOT NULL DEFAULT '',
  `ActiveDate` varchar(25) NOT NULL,
  `ExpireDate` varchar(25) DEFAULT NULL,
  `Job` varchar(200) NOT NULL DEFAULT '',
  `Position` varchar(200) NOT NULL DEFAULT ''
)

And some mock data:

Id       |`Name`        |ActiveDate              |ExpireDate             |Job       |`Position`
---------------------------------------------------------------------------------------------------
J1234    |Doe, John     |2010-08-15 00:00:00     |2011-08-15 00:00:00    |Worker    |Janitor
J1234    |Doe, John     |2011-08-15 00:00:00     |0000-00-00 00:00:00    |Worker    |Janitor
777      |Doe, Jane     |2010-06-04 00:00:00     |0000-00-00 00:00:00    |Boss      |Janitor
777      |Doe, Jane     |2011-04-30 00:00:00     |0000-00-00 00:00:00    |Boss      |Janitor
654G     |Smith, Jane   |2011-01-20 00:00:00     |0000-00-00 00:00:00    |Worker    |Janitor

The table also has and ExpireDate column which is actually set by the end user, and is not always set much to my dismay. Currently I’m using a dummy table to pull the distinct records out into and store for the day. I would use a temporary table but I’m not 100% sure how to in MySQL, plus I dislike them. The way I’m doing it is just temporary in hope for better SQL.

The data then has to be joined with a multitude of other tables to get the finished product. But I’m still needing to deal with the initial set of distinct data. And joining in the other table right from the start just wont work.

So here is how I’m pulling my data, storing it, and then pulling it again later and joing it to other tables:

INSERT INTO tmp_Person (Id, `Name`, Job, `Position`) 
    SELECT DISTINCT Id, `Name`, Job, `Position`
    FROM Person 

SELECT  tmp_Person.Id, 
    tmp_Person.`Name`, 
    tmp_Person.Job, 
    tmp_Person.`Position`,
    Pricing.Cost, 
    Pricing.Benefit

    FROM tmp_Person
    LEFT OUTER JOIN Pricing AS CL ON CL.PersonId = tmp_Person.Id 
        AND CL.PriceScredule = 'Major-Client' 
        AND CL.ExpireDate = '0000-00-00 00:00:00'
    LEFT OUTER JOIN Pricing AS Inter ON Inter.PersonId = tmp_Person.Id 
        AND Inter.PriceScredule = 'Internal-Client' 
        AND Inter.ExpireDate = '0000-00-00 00:00:00'

How can I write this to avoid the cost of processing out the duplicate rows using a temporary table (in any form)? HOpefully I’ve made this clear enough, if not I can gladly add, or clarify.

  • 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-24T18:27:30+00:00Added an answer on May 24, 2026 at 6:27 pm

    Replace tmp_Person with the code you have for the temp table:

    SELECT  tmp_Person.Id, 
        tmp_Person.`Name`, 
        tmp_Person.Job, 
        tmp_Person.`Position`,
        CL.Cost     AS MajorCost,              
        CL.Benefit  AS MajorBenefit,   
        Inter.Cost    AS InternalCost,
        Inter.Benefit AS InternalBenefit
    
        FROM 
          ( SELECT DISTINCT Id, `Name`, Job, `Position`
            FROM Person 
          )
          AS tmp_Person
        LEFT OUTER JOIN Pricing AS CL ON CL.PersonId = tmp_Person.Id 
            AND CL.PriceScredule = 'Major-Client' 
            AND CL.ExpireDate = '0000-00-00 00:00:00'
        LEFT OUTER JOIN Pricing AS Inter ON Inter.PersonId = tmp_Person.Id 
            AND Inter.PriceScredule = 'Internal-Client' 
            AND Inter.ExpireDate = '0000-00-00 00:00:00'
    

    As @Andriy spotted, using Pricing.Cost or Pricing.Benefit in the SELECT list would raise error. I guess you forgot to change it when you posted.

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

Sidebar

Related Questions

I have an application that receives most of its data from a third party.
I have a MySQL table from a third-party application that has millions of rows
I have a table that, due to the third party system we are using,
For example, I've seen third-party applications that have functions like this: $db->select('columns')->from('table')->where('condition'); That's just
I have a third party library that internally constructs and uses the SqlConnection class.
I have a third-party app that creates HTML-based reports that I need to display.
I have an third-party applet that requires JRE v1.5_12 to work correctly. THe user
I have a third-party product, a terminal emulator, which provides a DLL that can
I have a third party component I'm using and I'm seeing some issues with
I have a third-party library that is sometimes throwing an exception. So I decided

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.