declare @T table(
[SID] int,
[Date] datetime,
Profit money,
Units int)
insert into @T values
(1, '7/26/10 2:15 PM', -37.5, -1),
(1, '7/26/10 2:06 PM', -125, -1),
(1, '7/26/10 2:00 PM', -12.5, -1),
(1, '7/26/10 2:00 PM', 0, 0),
(1, '7/26/10 2:00 PM', -75, 1),
(1, '7/26/10 12:45 PM', -12.5, 1),
(1, '7/26/10 12:45 PM', 0, 0),
(1, '7/26/10 12:45 PM', -125, -1),
(1, '7/26/10 12:00 PM', 100, -1),
(1, '7/26/10 9:55 AM', -50, -1),
(2, '7/26/10 1:15 PM', -25, 1),
(2, '7/26/10 12:54 PM', -12.5, 1),
(2, '7/26/10 12:54 PM', 0, 0),
(2, '7/26/10 12:54 PM', -125, -1),
(2, '7/26/10 12:00 PM', 100, -1),
(2, '7/26/10 10:31 AM', -50, -1)
;with cte as
(
select
[SID],
cast([Date] as Date) as [Date],
Profit,
Units,
row_number() over(partition by SID, cast([Date] as Date)
order by [Date] desc) as rn
from @T
)
select
C1.[SID],
C1.[Date],
sum(C1.Profit) as TotalProfit,
sum(case when C2.Units = C1.Units and
C1.rn < S.rn
then C1.Profit
else 0.0
end) ProfitforEndofDayUnits
from cte as C1
inner join cte as C2
on C1.[SID] = C2.[SID] and
C1.[Date] = C2.[Date] and
C2.rn = 1
cross apply
(select min(C3.rn) as rn
from cte as C3
where C1.[SID] = C3.[SID] and
C1.[Date] = C3.[Date] and
C3.Units <> C2.Units) as S
group by C1.[SID], C1.[Date]
Результат:
SID Date TotalProfit ProfitforEndofDayUnits
----------- ---------- --------------------- -----------------------
1 2010-07-26 -337,50 -175.0000
2 2010-07-26 -112,50 -37.5000