Thanks Guys.. I may have simpified my problem to much.
I have the following query....
select a.id as rpid, b.id as ttid, c.id as tseid,
a.startdate, b.info1, b.info2 as bi2, c.info2 as ci2
from reportperiod a
join tasktimesheet b
on a.id=b.reportperiodid
join dbo.timesheetentry c
on b.id=c.tasktimesheetid
where startdate ='12/7/2009' and b.info1 is not null and b.info2 is not null
and c.info2 is not null
which produced the following output...
rpid ttid tseid startdate info1 bi2 ci2
----- ---- ----- ----------------------- ----------------------------- --- -----------
5606 3062 6897 2009-12-07 00:00:00.000 00001 - ANCHORS 6 IN AND 8 IN 5 COMPANY USE
5606 3062 6936 2009-12-07 00:00:00.000 00001 - ANCHORS 6 IN AND 8 IN 5 USED
5606 3063 6898 2009-12-07 00:00:00.000 00001 - MATERIAL AND SUPPLIES 2 USED
5606 3063 6939 2009-12-07 00:00:00.000 00001 - MATERIAL AND SUPPLIES 2 LEASE
I need to return the lowest 2 tseid, 1 for each ttid, like.
ttid tseid
3062 6936
3063 6939
I tried
with summary AS
(
select a.id as rpid, b.id as ttid, c.id as tseid,
a.startdate, b.info1, b.info2 as bi2, c.info2 as ci2
from reportperiod a
join tasktimesheet b
on a.id=b.reportperiodid
join dbo.timesheetentry c
on b.id=c.tasktimesheetid
where startdate ='12/7/2009' and b.info1 is not null and b.info2 is not null
and c.info2 is not null
)
select summary.ttid, min(tseid) as mintseid
from summary
group by summary.ttid, tseid
but that gives me all 4 records like...
ttid mintseid
----------- -----------
3062 6897
3062 6936
3063 6898
3063 6939
Thanks
Simi