Jump to content

Recommended Posts

I'm looking for the most efficient way to return the first record of a person based on a column holding a unix timestamp. I have about 10,000 rows so far with about 7000 unique 'firstname' and 'lastname' combinations. I want to return only the first record for each person.

 

 

This is what I have, but I'm afraid that since the records are not in order of the timestamp, that the earliest time will not be selected.

 

SELECT DISTINCT concat(firstname,' ',lastname) AS name, id,  timestamp FROM reports

 

 

Any help would be appreciated. Thanks.

Link to comment
https://forums.phpfreaks.com/topic/205865-first-record-for-similar-rows/
Share on other sites

I finally got it to do what I want but It takes a whole minute to load. The query is actually going to be run 3 times on one script with different time periods so it would take 3 minutes for the page to load. I was also going to use it to generate customizable graphs but it'd be almost unbearable for my boss to wait a whole minute for each report to load. Can anyone think of a way to make it go faster?

 

I'll explain the table so you can understand it's purpose. This table holds a list of clients policies. I am trying to get count of how many new clients were enrolled during a time period and who their agent it. So first, it needs to get a list of client policies for the time period. Then it checks to see if this person was already a client before and if it is an old client, not count it.

 

'reports r' = the table where the policy information is held.

'reportagents a' = the table where agent names are held. Some agents listed in the reports table need to be counted under another name so thats why they are in a seperate table and not in a field.

 

SELECT 
    count(*) AS count, 
    a.user
FROM 
    reports r
LEFT JOIN 
    reportagents a ON r.agent = a.agent
WHERE (
    SELECT 
        min( q.submitted )
    FROM 
        reports q
    WHERE 
        concat( q.firstname, ' ', q.lastname ) = concat( r.firstname, ' ', r.lastname )
)
BETWEEN 
    '1262325600' AND '1277504715'
GROUP BY 
    a.user
LIMIT 
    0, 30 

I tried to explain it in my second post.... I have a table of insurance policies... I want to only count the first policy on record for each person in the table. The purpose is to generate a report for the number of NEW clients.

 

The table has their name, and a unix timestamp of the policy date, among others.

That's because you're using GROUP BY -- and you don't need to.  And that's not joining to the table to itself.  Don't have time at the moment to write the query for you, though.

 

Well i also need it to count by agent. IE. Agent 1 had 30 new clients in May... Agent 2 had 25 new clients in May. How do i join the table to itself and would it be better to run an individual query for each agent? Thanks for you help. I really appreciate this.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.