基于值的总和生成数据组

时间:2018-04-05 05:42:21

标签: sql sql-server

如何计算总和和总文件。

这是我的表

**FileId**          **FileSize(MB)**
    1                      5
    2                      4
    3                      1
    4                      6
    5                      8
    6                      1
    7                      7
    8                      2

预期结果

BatchNo         StartId          EndId        BatchSize
   1               1               3             10
   2               4               4              6
   3               5               6              9
   4               7               8              9  

如果文件大小> = 10,则启动新批次

每批次的文件数量>> = 10然后开始新的批次

StartId和EndId基于FileId

和BatchNo是AutoIncrement

2 个答案:

答案 0 :(得分:5)

您可以像这样使用递归查询

with rdata as
(
      select row_number() over (order by fileId) rn, * from data
), rcte as
(
  select 1 no, 1 gr, fileSize fileSizeSum , * 
  from rdata where fileid = 1
  union all
  select case when fileSizeSum + d.fileSize > 10 or r.no = 10 then 1 else r.no + 1 end gr,
         case when fileSizeSum + d.fileSize > 10 or r.no = 10 then r.gr + 1 else r.gr end gr,
         case when fileSizeSum + d.fileSize > 10 or r.no = 10 then d.fileSize else d.fileSize + fileSizeSum  end fileSizeSum, 
         d.*
  from rcte r
  join rdata d on r.rn + 1 = d.rn
)
select r.gr, 
       min(fileId), 
       max(fileId), 
       max(fileSizeSum)
from rcte r
group by r.gr

dbfiddle

答案 1 :(得分:0)

这是另一种解决方案,采用不同的方法:

INSERT INTO #batchdetails (FileID, FileSizeTotal, GroupID)
SELECT FileID, (
            SELECT SUM(filesize) FROM #filedetails f2
            WHERE f1.fileid >= f2.fileid ) AS FileSizeTotal,
    1+CONVERT(INT,(
            SELECT SUM(filesize) FROM #filedetails f2
            WHERE f1.fileid >= f2.fileid
    )/(@filesizepergroup+0.1)) AS GroupID
FROM #filedetails f1

SELECT DISTINCT
    BatchDetails.GroupID AS BatchNo,
    MIN(BatchDetails.FileID) OVER (PARTITION BY BatchDetails.GroupID ORDER BY BatchDetails.GroupID) AS StartID,
    MAX(BatchDetails.FileID) OVER (PARTITION BY BatchDetails.GroupID ORDER BY BatchDetails.GroupID) AS EndID,
    BatchSizeGroup.BatchSize
FROM #batchdetails BatchDetails
INNER JOIN (
    SELECT GroupID, (GroupFileSizeTotal - LAG(GroupFileSizeTotal,1,0) OVER (ORDER BY GroupID)) AS BatchSize
    FROM (
        SELECT DISTINCT 
            GroupID, 
            MAX(FileSizeTotal) OVER (PARTITION BY GroupID ORDER BY GroupID) AS GroupFileSizeTotal
        FROM #batchdetails
        GROUP BY GroupID, FileSizeTotal
    )A
)BatchSizeGroup ON BatchDetails.GroupID = BatchSizeGroup.GroupID
GROUP BY BatchDetails.GroupID, BatchDetails.FileID, BatchSizeGroup.BatchSize

演示在这里:dbfiddle