Skip to content

Commit 28ead88

Browse files
Update 12 Double Or Add 1 While Loop.sql
1 parent f376ccd commit 28ead88

1 file changed

Lines changed: 27 additions & 54 deletions

File tree

Permutations, Combinations, Sequences and Random Numbers/SQL Scripts/12 Double Or Add 1 While Loop.sql

Lines changed: 27 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@ Last Updated: 07/05/2022
77
This script is written in SQL Server's T-SQL
88
99
10-
• I can make a safe assumption that less than the desired score (100) numbers are needed
11-
• To double the number, a WHILE loop is needed
12-
• To Add 1, we can use windowing
13-
• The #Numbers table is the only table needed to solve this puzzle
10+
* I can make a safe assumption that less than the desired score (100) numbers are needed
11+
* Recursion can be used to solve this problem
12+
* The #Numbers table is the only table needed to solve this puzzle
1413
1514
**********************************************************************/
1615

@@ -37,63 +36,37 @@ AS (
3736
FROM cte_Number
3837
WHERE Number < @vTotalNumbers
3938
)
40-
SELECT Number,
39+
SELECT CAST(Number AS bigint) AS Number,
4140
(CASE Number WHEN 1 THEN 1 ELSE NULL END) AS Calculation
4241
INTO #Numbers
4342
FROM cte_Number
44-
OPTION (MAXRECURSION 0)--A value of 0 means no limit to the recursion level
43+
OPTION (MAXRECURSION 101)--A value of 0 means no limit to the recursion level
4544

4645
-------------------------------
4746
-------------------------------
48-
--Update the #Numbers table with double calcualtion
49-
WHILE (SELECT MAX(Calculation) * 2 FROM #Numbers) < 100
5047

51-
BEGIN
52-
WITH cte_Min AS
53-
(
54-
(SELECT MIN(Number) AS MinNumber FROM #Numbers WHERE Calculation IS NULL)
55-
),
56-
cte_Lag AS
57-
(
58-
SELECT Number, LAG(Calculation,1) OVER (ORDER BY Number) * 2 AS LagValue
59-
FROM #Numbers
60-
),
61-
cte_Numbers AS
62-
(
63-
SELECT Number, LagValue
64-
FROM cte_Lag
65-
WHERE Number = (SELECT MinNumber FROM cte_Min)
66-
)
67-
UPDATE #Numbers
68-
SET Calculation = b.LagValue
69-
FROM #Numbers a INNER JOIN
70-
cte_Numbers b ON a.Number = b.Number
71-
72-
IF @@ROWCOUNT = 0
73-
BEGIN
74-
PRINT '@@ROWCOUNT Returned 0. Increase The Size Of The Numbers Table';
75-
RETURN
76-
END
77-
78-
END;
79-
80-
-------------------------------
81-
-------------------------------
82-
--Update the #Numbers table with add 1 calculation
83-
WITH cte_RowNumber AS
48+
;WITH cte_Numbers AS
8449
(
85-
SELECT ROW_NUMBER() OVER (ORDER BY Number) AS RowNumber2,
86-
(SELECT MAX(Calculation) FROM #Numbers) AS MaxNumber,
87-
*
50+
--Add a ranking function here if needed
51+
--Test data has StepNumber to rank/sort the records.
52+
SELECT number
8853
FROM #Numbers
89-
WHERE Calculation IS NULL
54+
),
55+
cte_Recursion AS
56+
(
57+
SELECT Number,
58+
CASE WHEN Number = 1 THEN 1 ELSE Number * 2 END AS RunningSum
59+
FROM #Numbers
60+
WHERE Number = 1
61+
UNION ALL
62+
SELECT
63+
t.Number,
64+
CASE WHEN (RunningSum * 2) < 101 THEN (RunningSum * 2) ELSE RunningSum + 1 END AS RunningSum
65+
FROM cte_Recursion cte
66+
INNER JOIN
67+
cte_Numbers t ON t.Number = (cte.Number + 1)
9068
)
91-
UPDATE #Numbers
92-
SET Calculation = MaxNumber + RowNumber2
93-
FROM #Numbers a INNER JOIN
94-
cte_RowNumber b ON a.Number = b.Number;
95-
96-
-------------------------------
97-
-------------------------------
98-
--Display the results
99-
SELECT * FROM #Numbers WHERE Calculation <= 100;
69+
SELECT *
70+
FROM cte_Recursion
71+
WHERE RunningSum = 100
72+
ORDER BY Number;

0 commit comments

Comments
 (0)