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 > DB2 > Need Help DISTINCT

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-01-10, 10:34
pjf135 pjf135 is offline
Registered User
 
Join Date: Sep 2005
Posts: 6
Need Help DISTINCT

I need help aggregating unique data.

Let's say my table consists of the following data:
ID COMPANYNAME COMPANYNUMBER UpdatedGMT

AF5AD27D CompanyA 987654321XX 1/12/2010 6:40:26 PM
A4FD4F65 CompanyB 32748923 11/18/2010 6:45:30 PM
AF5AD27D CompanyA 987654321ZZ 11/18/2010 6:47:29 PM

I need to pull both the CompanyName and CompanyNumber based on the last Update to each, this is stored in UpdatedGMT.

So my expected result should be

CompanyA 987654321ZZ
CompanyB 32748923

Because I only want to grab the lasest record for the updated Company A

Please help ASAP!

Thank you sooo much.
Reply With Quote
  #2 (permalink)  
Old 12-01-10, 10:45
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Code:
SELECT t.companyname
     , t.companynumber
  FROM ( SELECT companyname
              , MAX(updatedgmt) AS max_time
           FROM daTable
         GROUP
             BY companyname ) AS m
INNER
  JOIN daTable AS t
    ON t.companyname = m.companyname
   AND t.updatedgmt = m.max_time
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 12-01-10, 10:52
nvk@vhv nvk@vhv is offline
Registered User
 
Join Date: Jan 2010
Posts: 294
select COMPANYNAME, COMPANYNUMBER
from <table> t1
where not exists
(select t2.UpdatedGMT from <table> t2
where t2.UpdatedGMT > t1.UpdatedGMT and
t1.COMPANYNAME = t2.COMPANYNAME)

This should work, but there's maybe a faster way.
Reply With Quote
  #4 (permalink)  
Old 12-01-10, 12:11
pjf135 pjf135 is offline
Registered User
 
Join Date: Sep 2005
Posts: 6
Thanks for the replies.

A couple things that probably would have been helpful.

First, the company can vary to, so i can't join on that. CompanyA can also be updated to Company_A, thus we need to key off of the ID.

Second the table is not a physical table but a result set I am aliasing (sp?) as a table...

ie- SELECT ID, CompanyName, CompanyNumber, UpdatedGMT FROM (Select id, companyname, companynumber, effectivedate, updatedgmt from table join table on etc, etc, etc) AS X

Hope this helps.

I couldnt get either suggestion to work- probably due to these ommissions, my apologies.

Thanks
Reply With Quote
  #5 (permalink)  
Old 12-01-10, 14:20
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Quote:
Originally Posted by pjf135 View Post
... thus we need to key off of the ID.
Code:
SELECT t.id
     , t.companyname
     , t.companynumber
  FROM ( SELECT id
              , MAX(updatedgmt) AS max_time
           FROM daTable
         GROUP
             BY id) AS m
INNER
  JOIN daTable AS t
    ON t.id= m.id
   AND t.updatedgmt = m.max_time
__________________
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