Sign In | Register Now About Bytes | Help | Site Map
Connecting Tech Pros Worldwide

SubQuery Question

Question posted by: HeavenCore (Newbie) on July 4th, 2008 02:09 PM
Hello Everyone, i have a rather chunky Query:

Expand|Select|Wrap|Line Numbers
  1. SELECT top 100 percent EMDET.DET_NUMBERA, EMDET.DET_G1_NAME1A, EMDET.DET_SURNAMEA, EMDET.DET_BIR_DATED, EMDET.DET_SFT_DATED, EMPOS.POS_PDT_GRDA, EMPOS.POS_ENDD, EMPOS.POS_AV_HR_WKN, EMPOS.POS_L3_CDA, Max(EMSAL.SMN_DATEC) AS SMN_DATEC, Max(EMSAL.SMN_HOURLYN) as SMN_HOURLYN, EMPOS.POS_COST_GRPA, EMDET.DET_TER_DATED
  2. FROM EMDET, EMPOS, EMSAL
  3. WHERE EMDET.DET_NUMBERA = EMPOS.DET_NUMBERA AND EMDET.DET_NUMBERA = EMSAL.DET_NUMBERA AND EMPOS.DET_NUMBERA = EMSAL.DET_NUMBERA
  4. GROUP BY EMDET.DET_NUMBERA, EMDET.DET_G1_NAME1A, EMDET.DET_SURNAMEA, EMDET.DET_BIR_DATED, EMDET.DET_SFT_DATED, EMPOS.POS_PDT_GRDA, EMPOS.POS_ENDD, EMPOS.POS_AV_HR_WKN, EMPOS.POS_L3_CDA, EMPOS.POS_COST_GRPA, EMDET.DET_TER_DATED
  5. HAVING (EMPOS.POS_ENDD Is Null) AND (EMPOS.POS_L3_CDA<>'002' And EMPOS.POS_L3_CDA<>'004' And EMPOS.POS_L3_CDA<>'023' And EMPOS.POS_L3_CDA<>'024' And EMPOS.POS_L3_CDA<>'601' And EMPOS.POS_L3_CDA<>'701') AND (EMPOS.POS_COST_GRPA<>'MA' And EMPOS.POS_COST_GRPA<>'RM' And EMPOS.POS_COST_GRPA<>'PM' And EMPOS.POS_COST_GRPA<>'HO') AND (EMDET.DET_TER_DATED Is Null)
  6. ORDER BY EMPOS.POS_L3_CDA, EMDET.DET_SURNAMEA, Max(EMSAL.SMN_DATEC) DESC


This returns the following alot of data, but the 2 columns i am interested in are SMN_DATEC and SMN_HOURLYN, and the foreign key which is DET_NUMBERA

Example data in these columns is:
SMN_DATEC: 2008-05-01 00:00:00
SMN_HOURLYN: 7.9028
DET_NUMBERA: 0002379

This is selecting the max HOURLY and DATEC, this logic however is not correct, The HOURLYN needs to be selected where its on the same row as the MAX(SMN_DATEC) in the EMSAL table. For example, for DET_NUMBERA '0002379' we can get the data by running the following:

Expand|Select|Wrap|Line Numbers
  1. SELECT SMN_DATEC, SMN_HOURLYN FROM db_Chris21_Live."COHENSCHEMIST\chriscs".EMSAL WHERE DET_NUMBERA = '0002379' Order by SMN_DATEC DESC


Which produces the following results, the row i am after is the latest date. (top 1)

Expand|Select|Wrap|Line Numbers
  1. 2008-05-01 00:00:00.000    6.98
  2. 2008-04-01 00:00:00.000    7.9028
  3. 2007-09-01 00:00:00.000    6.81
  4. 2007-06-01 00:00:00.000    6.68
  5. 2007-04-01 00:00:00.000    7.71
  6. 2006-08-01 00:00:00.000    6.5
  7. 2006-07-03 00:00:00.000    7.5


Note the max date is 1st May2 2008.... which has an hour rate of 6.98, you can see in the original results i got 7.9 (because we where only using MAX) so how do we edit the original query to select the Hourly rate based on MAX date?

PS: sorry for the long over complicated query, i was just trying to offer as much info as possible.
SQL SERVER: 2000 (8.0.760) by the way, and there is no primary key in the EMSAL table, just the DET_NUMBERA foreign key (its an off the-shelf product so i cant just go adding my own primary key etc)
ck9663's Avatar
ck9663
Expert
1,326 Posts
July 8th, 2008
02:15 PM
#2

Re: SubQuery Question
Sorry, I easily gets confused with queries. I can be of more help seeing the source table(s) and your desired result.

-- CK

Reply
balabaster's Avatar
balabaster
Expert
507 Posts
July 8th, 2008
02:49 PM
#3

Re: SubQuery Question
You know you could simplify the query a little if you used the NOT IN expression:

...
AND EMPOS.POS_L3_CDA Not In ('002','004','023','024','601','701')
AND EMPOS.POS_COST_GRPA Not In ('MA','RM','PM','HO')
...

Performance shouldn't be impacted noticeably (although, it should only improve) but that's gotta be easier to read than all those brackets and <>

Reply
Reply
Not the answer you were looking for? Post your question . . .
189,072 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Latest Articles: Read & Comment
Top Microsoft SQL Server Contributors