简体   繁体   中英

SQL query to find the sum of all rows and sum of portions of rows within a group by clause

I am trying to build a query for a SQL Server 2008 database. The query would group the SID and Date column and sum the Profit column. The query also needs to find the sum of Profit for all rows having the same units toward the end of a day. in this case, the units value of 1 switched from -1 at 12:54PM to 1 and remained the same until the last record for that day which is at 1:15PM.


SID Date               Profit    Units   RowID
-------------------------------------------------------
1   7/26/10 1:15 PM     -25          1        1
1   7/26/10 12:54 PM    -12.5        1        2
1   7/26/10 12:54 PM    0            0        3
1   7/26/10 12:54 PM    -125         -1       4
1   7/26/10 12:00 PM    100          -1       5
1   7/26/10 10:31 AM    -50          -1       6

The row with 0 units indicates that the switch happened. The Units does not always change to 0 on the third row. For example,


SID Date             Profit     Units
--------------------------------------------------------
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

The output from the first data set would look like:


SID      Date     TotalProfit    ProfitforEndofDayUnits 
-------------------------------------------------------
1        7/26/10      -112.5                  -37.5

Thanks.

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]

Result:

SID         Date       TotalProfit           ProfitforEndofDayUnits
----------- ---------- --------------------- -----------------------
1           2010-07-26 -337,50               -175.0000
2           2010-07-26 -112,50               -37.5000

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM