1

I have a doubt in SQL Server query

I have a table dbo.[Files] with columns Id, FileName, FileSize(MB)

Id   FileName   FileSize(MB)
----------------------------
1    aa.gif        3 
2    bb.gif        5 
3    cc.gif        7 
4    dd.gif        5 
5    ee.gif        2 
6    fff.gif       4 
7    gg.gif        3 

My requirement is, I have to group with maximum file size limit and give a group Number for a group having sum of filesize less than or equal to the maximum limit

Suppose maximum file size sum is 10

Example result:

Id   FileName   FileSize(MB)   GroupNo
--------------------------------------
1    aa.gif         3             1 
2    bb.gif         5             1 
5    ee.gif         2             1 
3    cc.gif         7             2 
7    gg.gif         3             2 
4    dd.gif         5             3 
6    fff.gif        4             3 

Sum of FileSize in a Group is 10

Final result required

GroupNo  Ids
--------------
1        1,2,5 
2        4,6 
3        3,7 
halfer
  • 19,824
  • 17
  • 99
  • 186
RAGESH MK
  • 75
  • 1
  • 1
  • 5
  • 1
    Welcome to StackOverflow: if you post code, XML or data samples, **please** highlight those lines in the text editor and click on the "code samples" button ( `{ }` ) on the editor toolbar to nicely format and syntax highlight it! Then you don't need any of those messy ` ` and `
    ` tags, either!!
    – marc_s Jul 16 '15 at 12:32
  • Just a question which result do you require, the first or the second? Also have you tried any queries? – cjds Jul 16 '15 at 12:33
  • You may find this question from yesterday relevant: http://stackoverflow.com/questions/31435162/oracle-aggregation-function-to-allocate-amount – Greg Viers Jul 16 '15 at 12:33
  • 2
    What determines the grouping? There's quite a few possible combinations that would get you 10 mb, or less...? – jpw Jul 16 '15 at 12:35
  • that is my requirement I have to get 10 mb or less. If the filesize is greater than 10 mb then it can be individual records – RAGESH MK Jul 16 '15 at 12:38
  • My requirement is the last result set, I ve listed it just to covey my requirement – RAGESH MK Jul 16 '15 at 12:40
  • 1
    This question does not show any research effort. – Tab Alleman Jul 16 '15 at 13:16
  • Hi there. How did you get on with the answer from cars10? – halfer Sep 08 '15 at 13:57

2 Answers2

1

I tried quite bit, but in the end I had to settle for a procedural solution, albeit in T-SQL. The following script works on a temporary table @t1 (columns: id, fn, size, gr) into which the data has to be copied first. Alternatively you can also equip your original table with an additional group-id column gr and then work on that table.

declare @g int=1, @cnt int,@si int;
    -- group-no, row-count, size of group
select @cnt=count(*) from #t1 where gr=0;
while (@cnt>0) begin
select @si=isnull(sum(size),0) from #t1 where gr=@g;
update #t1 set gr=@g where id = (
 select top 1 id from #t1
 where gr=0 and @si+size<=10 
 order by @si+size desc);
 if (@@rowcount=0) begin
  if (@si=0) update top(1) #t1 set gr=@g 
             where gr=0; -- for files > 10mb
    set @g=@g+1;
  end;
  select @cnt=count(*) from #t1 where gr=0
end;

select * from #t1

A working example can be found at https://data.stackexchange.com/stackoverflow/query/337062/group-into-10mb-chunks-2

Community
  • 1
  • 1
Carsten Massmann
  • 26,510
  • 2
  • 22
  • 43
0

Can you please check if the below query is fetching your desired output :

        Select 
        GroupNo,
        LISTAGG((id)|| ',' ORDER BY id)
        from your_table;
Aritra Bhattacharya
  • 720
  • 1
  • 7
  • 18
  • Um, I'm pretty sure the question is about how to put the ids into groups and not only how to get the third table from the second – cjds Jul 16 '15 at 12:46
  • Well in his last comment he said that his requirement is the last result set. At first I also thought that the requirement is how to put the ids into groups. Lets see what the user has to say. @CarlSaldanha – Aritra Bhattacharya Jul 16 '15 at 12:48
  • I am not sure which database you are using. you can also refer to this link.https://www.simple-talk.com/sql/t-sql-programming/concatenating-row-values-in-transact-sql/ @RAGESHMK – Aritra Bhattacharya Jul 16 '15 at 12:53
  • the table doesn't have a `GroupNo` column. – Vamsi Prabhala Jul 16 '15 at 12:54
  • I dont have the GroupNo, I have to create the GroupNo with the help of filesize. and LISTAGG is not supported in MSSQL – RAGESH MK Jul 16 '15 at 14:26
  • In that case you have to use "STUFF". you can check the usage of the same in the link above. @RAGESHMK – Aritra Bhattacharya Jul 17 '15 at 04:51