Advertisement
balexandre

Stackoverflow question

Jun 9th, 2011
689
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 5.66 KB | None | 0 0
  1.  
  2. CREATE PROCEDURE getGroups
  3. (
  4.     @groupByQty INT, -- grouping number
  5.     @numberRuns INT  -- how many loops
  6.        
  7.     -- usage: getGroups 10, 10
  8. )
  9. AS
  10.  
  11. SET NOCOUNT ON;
  12.  
  13. -- declare all variables
  14. DECLARE  @rowId      INT,
  15.          @rowQty     INT,
  16.          @rowTotal   INT,
  17.          @groupId    INT,
  18.          @totalRuns  INT,
  19.          @continue   bit
  20.  
  21. -- set up our final temporary table
  22. CREATE TABLE #TBL_COUNT
  23. (
  24.   ids NVARCHAR(4000),
  25.   qty INT,
  26.   [GROUP] INT
  27. )
  28.  
  29. -- initializate variable
  30. SET @groupId = 1;
  31. SET @continue = 1;
  32. SET @totalRuns = 0;
  33. SELECT Id, Qty INTO #TBL_ALL FROM tblExample ORDER BY Qty DESC;
  34.  
  35. WHILE @totalRuns <= @numberRuns
  36. BEGIN
  37.     -- declare the cursor
  38.     DECLARE Product CURSOR FOR SELECT Id, Qty FROM #TBL_ALL ORDER BY Qty DESC;
  39.  
  40.     OPEN Product;
  41.     FETCH Product INTO @rowId, @rowQty;
  42.  
  43.     PRINT ' ';
  44.     PRINT '### Run: ' + CAST(@totalRuns AS nvarchar(10)) + ' #################################################################';
  45.     PRINT 'Grouping Table by ' + CAST(@groupByQty AS nvarchar(10)) + ' | group id = ' + CAST(@groupId AS nvarchar(10));
  46.  
  47.     -- Retrieve and process the first row
  48.  
  49.     SELECT Top 1 @rowId = Id, @rowQty = Qty FROM #TBL_ALL ORDER BY Qty DESC;
  50.     PRINT 'First Row: id = ' + CAST(@rowId AS nvarchar(10)) + ' | qty = ' + CAST(@rowQty AS nvarchar(10));
  51.    
  52.     -- sum it up and see if we have @groupByQty
  53.     SELECT @rowTotal = ISNULL(SUM(qty),0) FROM #TBL_COUNT WHERE [GROUP] = @groupId;
  54.     PRINT 'Current sum in #TBL_COUNT: @groupId = '+ CAST(@groupId AS nvarchar(10)) +' | @rowTotal = ' + CAST(@rowTotal AS nvarchar(10)) + ' | (@rowTotal + @rowQty) = ' + CAST((@rowTotal + @rowQty) AS nvarchar(10));
  55.          
  56.     IF @rowQty > @groupByQty
  57.     BEGIN
  58.         PRINT '  x First row has an unused number';
  59.     END
  60.     ELSE
  61.     BEGIN
  62.       -- handle result
  63.       IF (@rowTotal + @rowQty) = @groupByQty
  64.       BEGIN
  65.        
  66.         PRINT '+++ Current sum is ' + CAST(@groupByQty AS nvarchar(10)) + ' +++';
  67.        
  68.         -- save number
  69.         INSERT INTO #TBL_COUNT SELECT @rowId, @rowQty, @groupId;
  70.         PRINT '### Inserted final # into #TBL_COUNT : id = ' + CAST(@rowId AS nvarchar(10)) + ' | qty = ' + CAST(@rowQty AS nvarchar(10)) + ' | group = ' + CAST(@groupId AS nvarchar(10));
  71.        
  72.         -- remove from table as we use it already
  73.         DELETE FROM #TBL_ALL WHERE Id = @rowId;
  74.            
  75.         -- we got 10, let's change our Groupping
  76.         SET @groupId = (@groupId + 1);
  77.        
  78.         PRINT 'New group id: ' + CAST(@groupId AS nvarchar(10));
  79.        
  80.       END
  81.       ELSE
  82.       BEGIN  
  83.         IF (@rowTotal + @rowQty) < @groupByQty
  84.         BEGIN
  85.             PRINT '### Inserted into #TBL_COUNT : id = ' + CAST(@rowId AS nvarchar(10)) + ' | qty = ' + CAST(@rowQty AS nvarchar(10)) + ' | group = ' + CAST(@groupId AS nvarchar(10));
  86.            
  87.             -- save number
  88.             INSERT INTO #TBL_COUNT SELECT @rowId, @rowQty, @groupId;
  89.            
  90.             -- remove from table as we use it already
  91.             DELETE FROM #TBL_ALL WHERE Id = @rowId;
  92.         END
  93.         ELSE
  94.         BEGIN
  95.             PRINT '  x Unmatch number, will handle this latter';
  96.         END
  97.       END
  98.     END
  99.    
  100.     -- start the main processing loop
  101.     WHILE @@Fetch_Status = 0
  102.        BEGIN
  103.        
  104.           FETCH Product INTO @rowId, @rowQty;
  105.           PRINT '@@Fetch_Status = ' + CAST(@@Fetch_Status AS nvarchar(100));
  106.          
  107.           IF @@Fetch_Status < 0
  108.           BEGIN
  109.             BREAK
  110.           END
  111.              
  112.           -- we have the values of our row, let's use them
  113.           PRINT 'Fetched Row: id = ' + CAST(@rowId AS nvarchar(10)) + ' | qty = ' + CAST(@rowQty AS nvarchar(10));
  114.              
  115.           -- sum it up and see if we have @groupByQty
  116.           SELECT @rowTotal = ISNULL(SUM(qty),0) FROM #TBL_COUNT WHERE [GROUP] = @groupId;
  117.           PRINT 'Current sum in #TBL_COUNT: @groupId = '+ CAST(@groupId AS nvarchar(10)) +' | @rowTotal = ' + CAST(@rowTotal AS nvarchar(10)) + ' | (@rowTotal + @rowQty) = ' + CAST((@rowTotal + @rowQty) AS nvarchar(10));
  118.          
  119.           -- handle result
  120.           IF (@rowTotal + @rowQty) = @groupByQty
  121.           BEGIN
  122.            
  123.             PRINT '+++ Current sum is ' + CAST(@groupByQty AS nvarchar(10)) + ' +++';
  124.            
  125.             -- save number
  126.             INSERT INTO #TBL_COUNT SELECT @rowId, @rowQty, @groupId;
  127.             PRINT '### Inserted final # into #TBL_COUNT : id = ' + CAST(@rowId AS nvarchar(10)) + ' | qty = ' + CAST(@rowQty AS nvarchar(10)) + ' | group = ' + CAST(@groupId AS nvarchar(10));
  128.            
  129.             -- remove from table as we use it already
  130.             DELETE FROM #TBL_ALL WHERE Id = @rowId;
  131.                
  132.             -- we got 10, let's change our Groupping
  133.             SET @groupId = (@groupId + 1);
  134.            
  135.             PRINT 'New group id: ' + CAST(@groupId AS nvarchar(10));
  136.            
  137.             -- start again
  138.             BREAK;
  139.            
  140.           END
  141.           ELSE
  142.           BEGIN  
  143.             IF (@rowTotal + @rowQty) < @groupByQty
  144.             BEGIN
  145.                 PRINT '### Inserted into #TBL_COUNT : id = ' + CAST(@rowId AS nvarchar(10)) + ' | qty = ' + CAST(@rowQty AS nvarchar(10)) + ' | group = ' + CAST(@groupId AS nvarchar(10));
  146.                
  147.                 -- save number
  148.                 INSERT INTO #TBL_COUNT SELECT @rowId, @rowQty, @groupId;
  149.                
  150.                 -- remove from table as we use it already
  151.                 DELETE FROM #TBL_ALL WHERE Id = @rowId;
  152.             END
  153.             ELSE
  154.             BEGIN
  155.                 PRINT '  x Unmatch number, will handle this latter';
  156.             END
  157.           END
  158.  
  159.        END -- END WHILE @@Fetch_Status = 0
  160.        
  161.        SET @totalRuns = @totalRuns + 1;
  162.        
  163.        -- Close and dealocate
  164.        CLOSE Product;
  165.        DEALLOCATE Product;
  166.  
  167.    END -- END WHILE totalRuns <= @numberRuns
  168.    
  169.    -- let's sum our last group and remove it if it's not @groupByQty
  170.    SELECT @rowTotal = ISNULL(SUM(qty),0) FROM #TBL_COUNT WHERE [GROUP] = @groupId;
  171.    IF @rowTotal <> @groupByQty
  172.    BEGIN
  173.     SET IDENTITY_INSERT #TBL_ALL ON
  174.     INSERT INTO #TBL_ALL (Id, Qty) SELECT Ids, Qty FROM #TBL_COUNT WHERE [GROUP] = @groupId;
  175.     DELETE FROM #TBL_COUNT WHERE [GROUP] = @groupId;
  176.    END
  177.    
  178. SET NOCOUNT OFF;
  179.  
  180. -- Show and Delete temp tables
  181. SELECT * FROM #TBL_COUNT;
  182. SELECT * FROM #TBL_ALL;
  183. DROP TABLE #TBL_COUNT;
  184. DROP TABLE #TBL_ALL;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement