I agree with Celko that you really should post a create table statement and
inserts of the sample data so we can help you more efficiently.
Given the data listed, there isn't likely to be an efficient way to do what
you need. Here is a quickie I slapped together that may or may not be right
(getting late), but should get you pointed in the right direction. It
includes a teensy (and not robust) time table. NOTE that I changed your
data to reduce the number of rows necessary in the time table.
create table #tmp (id smallint, OppID char(1), ModDate smalldatetime, Amount
money)
insert #tmp values (1 ,'A', '2007-05-11 15:30', 10 )
insert #tmp values (2 ,'A', '2007-05-11 12:30', 20 )
insert #tmp values (3 ,'A', '2007-05-11 10:30', 15 )
insert #tmp values (4 ,'A', '2007-05-10 18:06', 11 )
insert #tmp values (5 ,'A', '2007-05-10 10:00', 16 )
insert #tmp values (6 ,'A', '2007-05-09 23:00', 25 )
insert #tmp values (7 ,'B', '2007-05-11 15:00', 20 )
insert #tmp values (8 ,'B', '2007-05-11 10:00', 36 )
insert #tmp values (9 ,'B', '2007-05-02 12:00', 94 )
insert #tmp values (10 ,'B', '2007-05-01 10:00', 510 )
insert #tmp values (11 ,'B', '2007-05-01 09:00', 14 )
insert #tmp values (12 ,'C', '2007-05-01 23:59', 19 )
insert #tmp values (13 ,'C', '2007-05-01 00:00', 87 )
insert #tmp values (14 ,'C', '2007-05-07 12:00', 46 )
insert #tmp values (15 ,'C', '2007-05-07 00:00', 30 )
insert #tmp values (16 ,'C', '2007-05-03 00:00', 25 )
insert #tmp values (17 ,'D', '2007-05-10 00:00', 2 )
insert #tmp values (18 ,'D', '2007-05-07 13:00', 81 )
insert #tmp values (19 ,'D', '2007-05-07 00:00', 84 )
insert #tmp values (20 ,'D', '2007-05-02 00:00', 20 )
CREATE TABLE #Time (
Date smalldatetime NOT NULL ,
Year smallint NOT NULL ,
Month char (3) NOT NULL ,
DayOfWeek tinyint NOT NULL
)
insert #time values ('2007-05-01', 2007, 'May', 3)
insert #time values ('2007-05-02', 2007, 'May', 4)
insert #time values ('2007-05-03', 2007, 'May', 5)
insert #time values ('2007-05-04', 2007, 'May', 6)
insert #time values ('2007-05-05', 2007, 'May', 7)
insert #time values ('2007-05-06', 2007, 'May', 1)
insert #time values ('2007-05-07', 2007, 'May', 2)
insert #time values ('2007-05-08', 2007, 'May', 3)
insert #time values ('2007-05-09', 2007, 'May', 4)
insert #time values ('2007-05-10', 2007, 'May', 5)
insert #time values ('2007-05-11', 2007, 'May', 6)
select tm.date, sum(amount)
--select *
from #tmp t1 inner join #time tm on t1.moddate <= tm.date
where tm.dayofweek = 3
and t1.moddate = (select max(t2.moddate) from #tmp t2 where t2.oppid =
t1.oppid and t2.moddate <= tm.date)
group by tm.date
Here is the select * output
id OppID ModDate Amount Date
Year Month DayOfWeek
------ ----- ----------------------- --------------------- -----------------------
------ ----- ---------
13 C 2007-05-01 00:00:00 87.00 2007-05-01
00:00:00 2007 May 3
9 B 2007-05-02 12:00:00 94.00 2007-05-08
00:00:00 2007 May 3
14 C 2007-05-07 12:00:00 46.00 2007-05-08
00:00:00 2007 May 3
18 D 2007-05-07 13:00:00 81.00 2007-05-08
00:00:00 2007 May 3
and here is the aggregate output
date
----------------------- ---------------------
2007-05-01 00:00:00 87.00
2007-05-08 00:00:00 221.00
--
TheSQLGuru
President
Indicium Resources, Inc.
[quoted text, click to view] "nisheeth29" <nisheeth29@discussions.microsoft.com> wrote in message
news:2D7DFE1F-480F-46B3-832F-E1CD1E25F068@microsoft.com...
> So here's my question:
>
> I have a table like this:
>
> Id OpportunityID TimeStamp Amount
> 1 A 2007-05-11 15:30 $10
> 2 A 2007-05-11 12:30 $20
> 3 A 2007-05-11 10:30 $15
> 4 A 2007-05-10 18:06 $11
> 5 A 2007-05-10 10:00 $16
> 6 A 2007-05-09 23:00 $25
> 7 B 2007-04-25 15:00 $20
> 8 B 2007-04-25 10:00 $36
> 9 B 2007-04-01 12:00 $94
> 10 B 2007-04-01 10:00 $510
> 11 B 2007-03-31 09:00 $14
> 12 C 2007-05-01 23:59 $19
> 13 C 2007-05-01 00:00 $87
> 14 C 2007-04-07 12:00 $46
> 15 C 2007-04-07 00:00 $30
> 16 C 2007-04-03 00:00 $25
> 17 D 2006-05-10 00:00 $2
> 18 D 2006-05-07 13:00 $81
> 19 D 2006-05-07 00:00 $84
> 20 D 2006-05-02 00:00 $20
>
>
> This represents a table in our sales data where it is tracking the dollar
> value of a sales opportunity over time.
>
> The Id is the primary key, the OpportunityID defines an opportunity and is
> a
> foreign key in this table. The TimeStamp refers to the actual time where
> there was a change in the Amount field of the Opportunity (in another
> table)
> as a result of which it wrote a record to the above table with the
> TimeStamp
> and the new Amount.
>
> Here's my business issue that I'm trying solve - the sum of amounts on all
> opportunities at any given point of time - that's we call our pipeline. I
> want to be able to write a query on this dataset that can give me
> snapshots
> at different points of time. The trick is to do this in a way that it
> reflects the latest timestamp record for each opportunity that is less
> than
> or equal to the snapshot time that I require. The big issue is that I want
> a
> single query (or a combination of a few) to return to me this result not
> for
> a single snapshot at a time but for a series of snapshots - for example,
> every Monday for the past 5 years.
>
> So for a snapshot of Jan 1, 2007 - this query should pick up only record
> 17.
> Snapshot of Apr 2, 2007 should return records 17 and 9. And a snapshot for
> Apr 29, 2007 should return records 7, 14, and 17.
>
> I'm open to all kinds of suggestions.
>