Whatever you do... Dont look at the execution plan on this one - It is perhaps the largest one I have witnessed 
create table t1 (CNAME varchar(20), PARTTYPE varchar(20), PARTID varchar(10), JDATE datetime, QTY int)insert into t1select 'LUCENT', 'TELECOM', 'TB1', '01/12/04', 1000 union allselect 'TEXAS', 'TELECOM', 'TB2', '05/02/75', 2000 union allselect 'TEXAS', 'AUTO', 'AM1', '10/25/85', 1000 union allselect 'LUCENT', 'AUTO', 'AM2', '07/18/92', 1500 union allselect 'TEXAS', 'AUTO', 'AM3', '01/17/93', 3500 union allselect 'LUCENT', 'TELECOM', 'TB3', '09/28/97', 2500select t1.*, CID , d2.PID + '.' + ( select cast(count(1)+1 as varchar(10)) from t1 t where t.parttype = t1.parttype and t.qty > t1.qty ) PID, datediff(ww,jdate,getdate()) WEEKSfrom t1join( select cname, ( select 'C' + cast(count(1)+ 1 as varchar(5)) from ( select top 100 percent cname, sum(qty) qty from t1 group by cname order by sum(qty) desc ) d where d.qty > d2.qty ) CID from ( select top 100 percent cname, sum(qty) qty from t1 group by cname order by sum(qty) desc ) d2) d on d.cname = t1.cnamejoin( select parttype, ( select 'P' + cast(count(1)+ 1 as varchar(5)) from ( select top 100 percent parttype, sum(qty) qty from t1 group by parttype order by sum(qty) desc ) d where d.qty > d3.qty ) PID from ( select top 100 percent parttype, sum(qty) qty from t1 group by parttype order by sum(qty) desc ) d3 ) d2 on t1.parttype = d2.parttype