I thought and tried using UNION aswell mate, would still mean trying to get 1 record for each projectid (MIN and MAX) .... again unless ive missed somethin
Be very interested to see what a SQL Dev would recommend
Heres my query in sql which works, not quite finished, however its not very lightweight using cursor, looping and temp tables.....
DECLARE @ProjectID int
DECLARE @Percent int
DECLARE @Total decimal(18,2)
DECLARE @Date smalldatetime
DECLARE @PREVProjectID int
DECLARE @PREVPercent int
DECLARE @PREVTotal decimal(18,2)
DECLARE @PREVDate smalldatetime
SET @PREVProjectID = 0
DECLARE @FIRSTProjectID int
DECLARE @FIRSTPercent int
DECLARE @FIRSTTotal decimal(18,2)
DECLARE @FIRSTDate smalldatetime
DECLARE @LASTProjectID int
DECLARE @LASTPercent int
DECLARE @LASTTotal decimal(18,2)
DECLARE @LASTDate smalldatetime
DECLARE @FirstRecord int
SET @FirstRecord = 0
DECLARE MyCursor CURSOR FOR
SELECT tblAccrued_Work.ProjectID,
tblAccrued_Work.Percentage_Invoiceable_To_Date,
tblAccrued_Work.Total_Costed,
tblAccrued_Work.Date_Created
FROM tblAccrued_Work
INNER JOIN tblProjects
ON tblAccrued_Work.ProjectID = tblProjects.ProjectID
WHERE (tblAccrued_Work.Date_Created BETWEEN '1-6-2006' AND '30-6-2006')
AND (tblAccrued_Work.StageID = 0)
AND tblProjects.StatusID = 2
AND tblProjects.Archived = 0
ORDER BY tblAccrued_Work.ProjectID
OPEN MyCursor
FETCH NEXT FROM MyCursor
INTO @ProjectID, @Percent, @Total, @Date
WHILE @@FETCH_STATUS = 0
BEGIN
--first record in set
IF @PREVProjectID = 0
BEGIN
--store first record in set
SET @FIRSTProjectID = @ProjectID
SET @FIRSTPercent = @Percent
SET @FIRSTTotal = @Total
SET @FIRSTDate = @Date
END
--if this projectid is equal to previous projectid
ELSE IF @ProjectID <> @PREVProjectID
BEGIN --NEW PROJECTID FOUND / START OF NEW RECORD -- Keep info
--only 1 entry found in date range (new project / short date range / very old project)
IF @FIRSTProjectID <> @LASTProjectID
BEGIN
PRINT @FIRSTProjectID
END
ELSE
BEGIN
--**STORE IN TEMP TABLE**--
--store records in temp table
PRINT 'first = ' + CAST(@FIRSTProjectID AS varchar(10)) + ', ' + CAST(@FIRSTPercent AS varchar(3))
PRINT 'last = ' + CAST(@LASTProjectID AS varchar(10)) + ', ' + CAST(@LASTPercent AS varchar(3))
END
--store first record
SET @FIRSTProjectID = @ProjectID
SET @FIRSTPercent = @Percent
SET @FIRSTTotal = @Total
SET @FIRSTDate = @Date
END
ELSE IF @ProjectID = @PREVProjectID
BEGIN
--store last record
SET @LASTProjectID = @ProjectID
SET @LASTPercent = @Percent
SET @LASTTotal = @Total
SET @LASTDate = @Date
END
--store values
SET @PREVProjectID = @ProjectID
SET @PREVPercent = @Percent
SET @PREVTotal = @Total
SET @PREVDate = @Date
FETCH NEXT FROM MyCursor
INTO @ProjectID, @Percent, @Total, @Date
END
CLOSE MyCursor
DEALLOCATE MyCursor