Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trying to find MAX from join of two tables

Status
Not open for further replies.

UltraSmooth

Programmer
Oct 28, 2002
97
0
0
CA
I'm sure this is easy but I've been banging my head on it for a while and can't come up with the proper solution,

I have two tables maintaining transaction records for items,

whscosthdr [whh_cdate,whh_username,whh_trans_no,whh_tsource,whh_from_loc,whh_to_loc,whh_pct_incr]

whscostdtl [whd_trans_no,whd_tsource,whd_from_loc,whd_style,whd_colour,whd_width,whd_sze,whd_qty,whd_tcost]

I need to find the the cost of the last transaction by item form the tables, so basically each items MAX transaction date from the whhcosthdr table and the item/cost information from the whscostdtl. The tables are joined on the related columns (primary key),

whh_trans_no=whd_trans_no
whh_tsource=whd_tsource
whh_from_loc=whd_from_loc

There are 3542 unique items (based on sytle+colour+width+size) in the whscostdtl table so really this should be the maximum records I'm getting back, along with the additional fields needed.

Here's what I've been able to come up with but it takes forever to run and returns over 9000 rows so I'm definitely missing something - just not really sure what :)

Code:
SELECT whscostdtl.whd_style
  , whscostdtl.whd_colour
  , whscostdtl.whd_width
  , whscostdtl.whd_sze
  , whscostdtl.whd_tcost AS whd_lastcost
  , whscosthdr.whh_cdate
  , max_date.*
FROM whscosthdr
JOIN whscostdtl
  ON whscostdtl.whd_trans_no = whscosthdr.whh_trans_no
 AND whscostdtl.whd_tsource = whscosthdr.whh_tsource
 AND whscostdtl.whd_from_loc = whscosthdr.whh_from_loc

JOIN (

SELECT MAX(whscosthdr.whh_cdate) AS max_date
  , whd_from_loc
  , whd_style
  , whd_colour
  , whd_width
  , whd_sze
      FROM whscosthdr
      JOIN whscostdtl
        ON whscostdtl.whd_trans_no = whscosthdr.whh_trans_no
       AND whscostdtl.whd_tsource = whscosthdr.whh_tsource
       AND whscostdtl.whd_from_loc = whscosthdr.whh_from_loc
     WHERE whscosthdr.whh_from_loc='98'
     GROUP BY  whd_style, whd_style, whd_colour, whd_width, whd_sze
     
     ) AS max_date
     ON max_date.whd_from_loc=whscosthdr.whh_from_loc
    AND max_date.max_date=whscosthdr.whh_cdate
    AND max_date.whd_style=whscostdtl.whd_style
    AND max_date.whd_colour=whscostdtl.whd_colour
    AND max_date.whd_width=whscostdtl.whd_width
    AND max_date.whd_sze=whscostdtl.whd_sze

Any suggestions? Somehow I feel I'm making this problem far more complicated than it should be but it's just not clicking for me.

Thanks!
 
I managed to solve my problem after, just needed to take it a step further, and with an extra index added is running quick and smooth.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top