If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > How to get the first data from a multiple or duplicate records

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-18-11, 10:26
cwiggler cwiggler is offline
Registered User
 
Join Date: Oct 2011
Posts: 6
How to get the first data from a multiple or duplicate records

Hello everyone, I would like to ask for idea and help on how to achieve my concern. Below is my SQL statement. Im joining 2 tables to get my results. Here's the sample results of what im getting.

Code:
Name  | Desc | Issue | ATime | Back | TotalTime | Ack | Res
 
123 | test | error | 2011-10-18 17:09:26 | 2011-10-18 17:11:33 | 00:02:07 | user@home.net | fixed
234 | test | error | 2011-10-18 17:09:26 | 2011-10-18 17:11:33 | 00:02:07 | user@home.net | fixed
123 | test | error | 2011-10-18 17:09:26 | 2011-10-18 18:20:33 | 00:02:07 | user@home.net | fixed
234 | test | error | 2011-10-18 17:09:26 | 2011-10-18 19:21:33 | 00:02:07 | user@home.net | fixed
223 | test | error | 2011-10-18 17:09:26 | 2011-10-18 17:11:33 | 00:02:07 | user@home.net | fixed
234 | test | error | 2011-10-18 17:09:26 | 2011-10-18 19:25:33 | 00:02:07 | user@home.net | fixed
223 | test | error | 2011-10-18 17:09:26 | 2011-10-18 18:20:33 | 00:02:07 | user@home.net | fixed
234 | test | error | 2011-10-18 17:09:26 | 2011-10-18 19:29:33 | 00:02:07 | user@home.net | fixed
What I want to achieve is to get only the first entry of data based on Back and do my calculation. how can I do that?

Code:
123 | test | error | 2011-10-18 17:09:26 | 2011-10-18 17:11:33 | 00:02:07 | user@home.net | fixed
234 | test | error | 2011-10-18 17:09:26 | 2011-10-18 17:11:33 | 00:02:07 | user@home.net | fixed
223 | test | error | 2011-10-18 17:09:26 | 2011-10-18 17:11:33 | 00:02:07 | user@home.net | fixed
Here's my statement.

Code:
SELECT t1.name, 
t1.Description, 
t1.Issue, 
t1.Dateres AS ATime, 
t2.Dateres AS BAck, 
TIMEDIFF(t2.Dateres,t1.Dateres) AS TotalTime, 
t2.Acknowledge, t2.Resolution 
FROM t1 LEFT JOIN t2 ON t1.name = t2.name 
AND t1.IPAddress = t2.IPAddress 
AND t1.Description = t2.Description 
AND t1.Issue = t2.Issue 
AND t1.Severity = t2.Severity 
AND t1.Timestamp = t2.Timestamp 
WHERE t1.Dateres is NOT NULL AND t2.Dateres is NOT NULL 
AND t2.Acknowledge = 'user@home.net' 
AND t2.Dateres >= '2011-10-18 00:00:00' 
AND t2.Dateres <= '2011-10-23 23:59:59' 
GROUP BY ATime ORDER by BAck ASC;
Thanks.
Reply With Quote
  #2 (permalink)  
Old 10-19-11, 03:51
it-iss.com it-iss.com is offline
Registered User
 
Join Date: Sep 2009
Location: San Sebastian, Spain
Posts: 620
Try using the DISTINCT:

Code:
SELECT DISTINCT t1.name, 
t1.Description, 
t1.Issue, 
t1.Dateres AS ATime, 
t2.Dateres AS BAck, 
TIMEDIFF(t2.Dateres,t1.Dateres) AS TotalTime, 
t2.Acknowledge, t2.Resolution 
FROM t1 LEFT JOIN t2 ON t1.name = t2.name 
AND t1.IPAddress = t2.IPAddress 
AND t1.Description = t2.Description 
AND t1.Issue = t2.Issue 
AND t1.Severity = t2.Severity 
AND t1.Timestamp = t2.Timestamp 
WHERE t1.Dateres is NOT NULL AND t2.Dateres is NOT NULL 
AND t2.Acknowledge = 'user@home.net' 
AND t2.Dateres >= '2011-10-18 00:00:00' 
AND t2.Dateres <= '2011-10-23 23:59:59' 
GROUP BY ATime ORDER by BAck ASC;
__________________
Ronan Cashell
Senior Oracle/MySQL DBA
http://www.it-iss.com
Reply With Quote
  #3 (permalink)  
Old 10-20-11, 04:09
cwiggler cwiggler is offline
Registered User
 
Join Date: Oct 2011
Posts: 6
Hi I tried DISTINCT but still showing duplicate records. Any solution how to implement MIN on TotalTime ? thanks

Quote:
Originally Posted by it-iss.com View Post
Try using the DISTINCT:

Code:
SELECT DISTINCT t1.name, 
t1.Description, 
t1.Issue, 
t1.Dateres AS ATime, 
t2.Dateres AS BAck, 
TIMEDIFF(t2.Dateres,t1.Dateres) AS TotalTime, 
t2.Acknowledge, t2.Resolution 
FROM t1 LEFT JOIN t2 ON t1.name = t2.name 
AND t1.IPAddress = t2.IPAddress 
AND t1.Description = t2.Description 
AND t1.Issue = t2.Issue 
AND t1.Severity = t2.Severity 
AND t1.Timestamp = t2.Timestamp 
WHERE t1.Dateres is NOT NULL AND t2.Dateres is NOT NULL 
AND t2.Acknowledge = 'user@home.net' 
AND t2.Dateres >= '2011-10-18 00:00:00' 
AND t2.Dateres <= '2011-10-23 23:59:59' 
GROUP BY ATime ORDER by BAck ASC;
Reply With Quote
  #4 (permalink)  
Old 10-20-11, 07:28
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Code:
SELECT t1.name
     , t1.Description
     , t1.Issue
     , t1.Dateres AS ATime
     , t2.Dateres AS BAck
     , TIMEDIFF(t2.Dateres,t1.Dateres) AS TotalTime
     , t2.Acknowledge
     , t2.Resolution
  FROM t1 
LEFT 
  JOIN ( SELECT name                      
              , MIN(t2.Dateres) AS earliest
           FROM t2
          WHERE t2.Acknowledge = 'user@home.net'
            AND t2.Dateres >= '2011-10-18'
            AND t2.Dateres  < '2011-10-24'
         GROUP
             BY name ) AS m2     
    ON m2.name        = t1.name
LEFT 
  JOIN t2 
    ON t2.name = m2.name
   AND t2.Dateres = m2.earliest   
 WHERE t1.Dateres is NOT NULL 
ORDER 
    BY BAck ASC;
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 10-21-11, 03:44
cwiggler cwiggler is offline
Registered User
 
Join Date: Oct 2011
Posts: 6
Hi and thanks. I tried your suggestion but it take forever to run the query. i don't know why. I left the query running for 5 mins but still doesn't show results. any idea?

Quote:
Originally Posted by r937 View Post
Code:
SELECT t1.name
     , t1.Description
     , t1.Issue
     , t1.Dateres AS ATime
     , t2.Dateres AS BAck
     , TIMEDIFF(t2.Dateres,t1.Dateres) AS TotalTime
     , t2.Acknowledge
     , t2.Resolution
  FROM t1 
LEFT 
  JOIN ( SELECT name                      
              , MIN(t2.Dateres) AS earliest
           FROM t2
          WHERE t2.Acknowledge = 'user@home.net'
            AND t2.Dateres >= '2011-10-18'
            AND t2.Dateres  < '2011-10-24'
         GROUP
             BY name ) AS m2     
    ON m2.name        = t1.name
LEFT 
  JOIN t2 
    ON t2.name = m2.name
   AND t2.Dateres = m2.earliest   
 WHERE t1.Dateres is NOT NULL 
ORDER 
    BY BAck ASC;
Reply With Quote
  #6 (permalink)  
Old 10-21-11, 08:05
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
what indexes have you declared on those tables? please do a SHOW CREATE TABLE for each one so that we can see them
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On