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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

LEFT INNER JOIN WITH GROUP BY 1

Status
Not open for further replies.

cristi22

MIS
Aug 17, 2005
155
US
Hello all –

Thanks for your help!

I need to create a result table where members have earnings in 2002 & nothing in 2003 or 2004.

Here is my input data:

Code:
SSN       WK         NAME                                     EARN       
--------- ---------- ---------------------------------------- -----------
999999999 04/01/2002 HUGO MORINIGO                                2520.00
999999999 06/01/2002 HUGO MORINIGO                                2520.00
999999999 07/01/2002 HUGO MORINIGO                                2268.00
999999999 11/01/2002 HUGO MORINIGO                                2142.00
999999999 12/01/2002 HUGO MORINIGO                                1890.00
999999999 01/01/2003 HUGO MORINIGO                                2520.00
999999999 08/01/2002 HUGO MORINIGO                                2520.00
999999999 09/01/2002 HUGO MORINIGO                                2394.00
999999999 10/01/2002 HUGO MORINIGO                                2520.00
999999999 02/01/2003 HUGO MORINIGO                                2394.00
999999999 03/01/2003 HUGO MORINIGO                                1890.00

Somehow I need to combine the following 2 statements

Code:
select ssn, year(wk) , name , sum(earn) from SSA_363EP where 
YEAR(WK) =  YEAR (DATE('2002-01-01')) 
group by ssn, year(wk), name

Code:
select ssn, year(wk), name, sum(earn)  from SSA_363EP where 
YEAR(WK) =   YEAR (DATE('2003-01-01')) or YEAR(WK) = YEAR (DATE('2004-01-01')) 
group by ssn, year(wk),name

I think I am no the right track, although the following select returns 0 records (should be 36)

Code:
select L.ssn, year(L.wk) , l.name , sum(L.earn) from SSA_363EP L LEFT OUTER JOIN  SSA_363EP R 
on l.ssn=r.ssn 
where 
YEAR(L.WK) =  YEAR (DATE('2002-01-01'))  
and YEAR(r.WK) =  YEAR (DATE('2003-01-01'))   and r.ssn is null
group by L.ssn, year(L.wk), L.name

really appreciate your help,
Cristi [3eyes]
 
Cristi,

Try a subquery:

Code:
select ssn, year(wk) , name , sum(earn) from SSA_363EP where 
YEAR(WK) =  2002 AND ssn not in (select ssn from SSA_363EP where year(WK) in (2003,2004))
group by ssn, year(wk), name

Can be too slow for very big table, though




Ties Blom
Information analyst
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top