Skip to content

Commit dc073c7

Browse files
fresh initialize
0 parents  commit dc073c7

File tree

90 files changed

+15254
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

90 files changed

+15254
-0
lines changed

Advanced SQL Puzzles/Advanced SQL Puzzles DDL.sql

+1,525
Large diffs are not rendered by default.

Advanced SQL Puzzles/Advanced SQL Puzzles Solutions.sql

+3,336
Large diffs are not rendered by default.
938 KB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*********************************************************************
2+
Scott Peters
3+
Factorials
4+
https://advancedsqlpuzzles.com
5+
Last Updated: 01/13/2023
6+
Microsoft SQL Server T-SQL
7+
8+
The script creates a temporary table called #Numbers that contains the
9+
factorials of a range of numbers specified by the variable @vTotalNumbers.
10+
The script uses a common table expression (CTE) with recursion to calculate the factorials,
11+
starting with the number one and incrementing by one until the value of @vTotalNumbers is reached.
12+
The results of the CTE are then inserted into the #Numbers table and displayed at the end.
13+
The OPTION (MAXRECURSION 0) setting ensures that there is no limit to the recursion level.
14+
15+
**********************************************************************/
16+
17+
---------------------
18+
---------------------
19+
--Tables used in script
20+
DROP TABLE IF EXISTS #Numbers;
21+
GO
22+
23+
---------------------
24+
---------------------
25+
--Declare and set and variables
26+
DECLARE @vTotalNumbers INTEGER = 10;
27+
28+
---------------------
29+
---------------------
30+
--Create #Numbers table using recursion
31+
WITH cte_Factorial (Number, Factorial) AS
32+
(
33+
SELECT 1,
34+
1
35+
UNION ALL
36+
SELECT Number + 1 AS Number,
37+
(Number + 1) * Factorial AS Factorial
38+
FROM cte_Factorial
39+
WHERE Number < @vTotalNumbers
40+
)
41+
SELECT Number,
42+
Factorial
43+
INTO #Numbers
44+
FROM cte_Factorial
45+
OPTION (MAXRECURSION 0);--A value of 0 means no limit to the recursion level;
46+
GO
47+
48+
---------------------
49+
---------------------
50+
--Display the results
51+
SELECT *
52+
FROM #Numbers;
53+
GO
54+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*********************************************************************
2+
Scott Peters
3+
All Permutations
4+
https://advancedsqlpuzzles.com
5+
Last Updated: 02/07/2023
6+
Microsoft SQL Server T-SQL
7+
8+
The script generates all the permutations of numbers from 1 to @vTotalNumbers (inclusive).
9+
It first creates a table called #Numbers, which contains a column of numbers from 1 to @vTotalNumbers.
10+
Then, it uses a recursive CTE to generate all the permutations by iterating through the #Numbers
11+
table and concatenating the numbers to form unique permutations. The output is saved in the temporary
12+
table #Permutations, and the results can be viewed by running a SELECT statement on it. The script
13+
uses bitwise operators to speed up the calculation, but it can take a significant amount of time
14+
to generate all permutations, especially with larger values of @vTotalNumbers.
15+
16+
**********************************************************************/
17+
18+
---------------------
19+
---------------------
20+
--Tables used
21+
DROP TABLE IF EXISTS #Numbers;
22+
DROP TABLE IF EXISTS #Permutations;
23+
GO
24+
25+
---------------------
26+
---------------------
27+
--Declare and set variables
28+
DECLARE @vTotalNumbers BIGINT = 3;
29+
30+
---------------------
31+
---------------------
32+
--Create a #Numbers table using recursion
33+
WITH cte_Numbers (Number)
34+
AS (
35+
SELECT 1 AS Number
36+
UNION ALL
37+
SELECT Number + 1
38+
FROM cte_Numbers
39+
WHERE Number < @vTotalNumbers
40+
)
41+
SELECT
42+
Number
43+
INTO #Numbers
44+
FROM cte_Numbers
45+
OPTION (MAXRECURSION 0);--A value of 0 means no limit to the recursion level
46+
47+
---------------------
48+
---------------------
49+
--Generate the Permutations using recursion
50+
WITH cte_Numbers AS
51+
(
52+
SELECT CAST(Number AS VARCHAR(MAX)) AS Number
53+
FROM #Numbers
54+
),
55+
cte_Bitmasks AS
56+
(
57+
SELECT
58+
Number,
59+
CAST(POWER(2, ROW_Number() OVER (ORDER BY Number) - 1) AS INT) AS Bitmask
60+
FROM cte_Numbers
61+
),
62+
cte_Permutations AS
63+
(
64+
SELECT Number AS Permutation,
65+
Bitmask
66+
FROM cte_Bitmasks
67+
68+
UNION ALL
69+
70+
SELECT p.Permutation + ',' + b.Number,
71+
p.Bitmask ^ b.Bitmask
72+
FROM cte_Permutations p INNER JOIN
73+
cte_Bitmasks b ON p.Bitmask ^ b.Bitmask > p.Bitmask
74+
)
75+
SELECT ROW_NUMBER() OVER (ORDER BY GETDATE()) AS Id,
76+
Permutation
77+
INTO #Permutations
78+
FROM cte_Permutations
79+
WHERE Bitmask = POWER(2, (SELECT COUNT(*) FROM cte_Numbers)) - 1;
80+
81+
---------------------
82+
---------------------
83+
--Display the results
84+
SELECT @vTotalNumbers AS MaxNumber,
85+
Permutation
86+
FROM #Permutations
87+
ORDER BY 2;
88+
GO
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*********************************************************************
2+
Scott Peters
3+
All Permutations
4+
https://advancedsqlpuzzles.com
5+
Last Updated: 01/13/2023
6+
Microsoft SQL Server T-SQL
7+
8+
This script creates all the possible permutations of a set of numbers (from one to a user-specified value)
9+
using a recursive CTE. It creates a #Numbers table using recursion and then uses that table to populate
10+
the #Permutations table with all possible permutations. It then uses a WHERE clause to limit the output
11+
to only the permutations of the same length as the longest permutation. The results are then ordered by the permutation.
12+
13+
**********************************************************************/
14+
15+
---------------------
16+
---------------------
17+
--Tables used
18+
DROP TABLE IF EXISTS #Numbers;
19+
DROP TABLE IF EXISTS #Permutations;
20+
GO
21+
22+
---------------------
23+
---------------------
24+
--Declare and set variables
25+
DECLARE @vTotalNumbers INTEGER = 3;
26+
27+
---------------------
28+
---------------------
29+
--Create a #Numbers table using recursion
30+
WITH cte_Numbers (Number)
31+
AS (
32+
SELECT 1 AS Number
33+
UNION ALL
34+
SELECT Number + 1
35+
FROM cte_Numbers
36+
WHERE Number < @vTotalNumbers
37+
)
38+
SELECT
39+
Number
40+
INTO #Numbers
41+
FROM cte_Numbers
42+
OPTION (MAXRECURSION 0);--A value of 0 means no limit to the recursion level
43+
44+
---------------------
45+
---------------------
46+
--Populate the #Permutations table with all possible permutations
47+
WITH cte_Permutations (Permutation, Ids, Depth)
48+
AS
49+
(
50+
SELECT CAST(Number AS VARCHAR(MAX)),
51+
CAST(CONCAT(Number,';') AS VARCHAR(MAX)),
52+
1 AS Depth
53+
FROM #Numbers
54+
UNION ALL
55+
SELECT CONCAT(a.Permutation,',',b.Number),
56+
CONCAT(a.Ids,b.Number,';'),
57+
a.Depth + 1
58+
FROM cte_Permutations a,
59+
#Numbers b
60+
WHERE a.Depth < @vTotalNumbers AND
61+
a.Ids NOT LIKE CONCAT('%',b.Number,';%')
62+
)
63+
SELECT Permutation
64+
INTO #Permutations
65+
FROM cte_Permutations;
66+
GO
67+
---------------------
68+
---------------------
69+
--Display the results
70+
SELECT *
71+
FROM #Permutations
72+
WHERE LEN(Permutation) = (SELECT MAX(LEN(Permutation)) FROM #Permutations)
73+
ORDER BY 1;
74+
GO
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*********************************************************************
2+
Scott Peters
3+
All Permutations
4+
https://advancedsqlpuzzles.com
5+
Last Updated: 02/07/2023
6+
Microsoft SQL Server T-SQL
7+
8+
This script generates all permutations of a given set of numbers using a while loop.
9+
It starts by creating a #Numbers table with a specified number of integers using a recursive CTE.
10+
Then, it creates an empty #Permutations table and seeds it with the initial values from the #Numbers table.
11+
Then, it enters a while loop, and in each iteration of the loop, it first performs a DELETE statement to
12+
keep the number of records in the #Permutations table minimal. Then, it uses an INSERT statement
13+
to add new permutations to the #Permutations table by combining the current permutations with the
14+
remaining numbers from the #Numbers table. The while loop continues until the number of rows
15+
affected by the DELETE statement is 0. Finally, it selects and displays the final
16+
permutations from the #Permutations table.
17+
18+
**********************************************************************/
19+
20+
---------------------
21+
---------------------
22+
--Tables used
23+
DROP TABLE IF EXISTS #Numbers;
24+
DROP TABLE IF EXISTS #Permutations;
25+
GO
26+
27+
---------------------
28+
---------------------
29+
--Set the number of permutations to create
30+
DECLARE @vTotalNumbers BIGINT = 3;
31+
32+
---------------------
33+
---------------------
34+
--Create a #Numbers table using recursion
35+
WITH cte_Numbers (Number)
36+
AS (
37+
SELECT 1 AS Number
38+
UNION ALL
39+
SELECT Number + 1
40+
FROM cte_Numbers
41+
WHERE Number < @vTotalNumbers
42+
)
43+
SELECT
44+
Number
45+
INTO #Numbers
46+
FROM cte_Numbers
47+
OPTION (MAXRECURSION 0);--A value of 0 means no limit to the recursion level
48+
49+
---------------------
50+
---------------------
51+
--Create the #Permutations table and provide initial seed
52+
SELECT CAST(Number AS VARCHAR(100)) AS Permutation,
53+
GETDATE() AS InsertDate
54+
INTO #Permutations
55+
FROM #Numbers;
56+
57+
---------------------
58+
---------------------
59+
--Populate the #Permutations table
60+
WHILE @@ROWCOUNT > 0
61+
BEGIN
62+
63+
--Used to keep the table record count to a minimal
64+
DELETE #Permutations WHERE InsertDate < (SELECT MAX(InsertDate) FROM #Permutations);
65+
66+
INSERT INTO #Permutations (Permutation, InsertDate)
67+
SELECT CONCAT(a.Permutation, ',', b.Number),
68+
GETDATE()
69+
FROM #Permutations a CROSS JOIN
70+
#Numbers b
71+
WHERE CAST(REPLACE(RIGHT(a.Permutation,2),',','') AS INTEGER) <> b.Number
72+
AND
73+
CHARINDEX(CONCAT(b.Number,','),CONCAT(a.Permutation,',')) = 0;
74+
END;
75+
76+
---------------------
77+
---------------------
78+
--Display the results
79+
SELECT @vTotalNumbers AS MaxNumber,
80+
Permutation
81+
FROM #Permutations
82+
ORDER BY Permutation;
83+
GO
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*********************************************************************
2+
Scott Peters
3+
Growing Numbers
4+
https://advancedsqlpuzzles.com
5+
Last Updated: 01/13/2023
6+
Microsoft SQL Server T-SQL
7+
8+
This script finds all possible growing numbers from a set of integers within a specified range.
9+
For example, given the input 1, 2, 3, 4, and 5. It will produce the following output:
10+
1
11+
12
12+
123
13+
1234
14+
12345
15+
16+
The script creates a temporary table called #GrowingNumbers and uses a recursive CTE to populate it.
17+
The script initializes a variable, @vTotalNumbers, with the count of integers in the set.
18+
The CTE, cte_GrowingNumbers, generates all possible growing numbers from the set of integers.
19+
The CTE starts by selecting the individual integers from the #Numbers table and concatenating them to
20+
form a single growing number. Then, it recursively joins itself with the #Numbers table, adding one
21+
integer at a time while ensuring that the next integer added is a successive number.
22+
23+
The script then inserts the results of the CTE into the #GrowingNumbers table and uses a SELECT
24+
statement to display the contents of the table filtered by the condition that the first digit of
25+
the number must be equal to the minimum number in the set.
26+
27+
The script will run properly if the following conditions are met:
28+
The first number is a single-digit
29+
All digits are successive, and there are no gaps in the numbers
30+
Two of the numbers cannot be two digits (i.e., 10 and 11)
31+
32+
**********************************************************************/
33+
34+
---------------------
35+
---------------------
36+
--Tables used
37+
DROP TABLE IF EXISTS #Numbers;
38+
DROP TABLE IF EXISTS #GrowingNumbers;
39+
GO
40+
41+
---------------------
42+
---------------------
43+
--For this puzzle, I manually create the #Numbers table to provide special testing cases (rather than using recursion)
44+
CREATE TABLE #Numbers
45+
(
46+
Number INTEGER UNIQUE NOT NULL
47+
);
48+
GO
49+
50+
INSERT INTO #Numbers (Number) VALUES
51+
(1),(2),(3),(4),(5); --Passes
52+
--(6),(7),(8),(9),(10); --Passes
53+
--(5),(7),(8),(9),(10); --Does Not Pass (Gap between 5 and 7)
54+
--(7),(8),(9),(10),(11); --Does Not Pass (Two numbers greater than 10)
55+
--(10),(11),(12); --Does Not Pass (The lowest number is not a single digit, and the set contains two numbers greater than 10)
56+
GO
57+
58+
---------------------
59+
---------------------
60+
--Declare and set variables
61+
DECLARE @vTotalNumbers INTEGER = (SELECT COUNT(*) FROM #Numbers);
62+
63+
---------------------
64+
---------------------
65+
--Create the #GrowingNumbers table using recursion
66+
WITH cte_GrowingNumbers (Number, Ids, Depth)
67+
AS
68+
(
69+
SELECT CAST(Number AS VARCHAR(MAX)) AS Number,
70+
CAST(CONCAT(Number,'') AS VARCHAR(MAX)) AS Ids,
71+
1 AS Depth
72+
FROM #Numbers
73+
UNION ALL
74+
SELECT CONCAT(a.Number,b.Number),
75+
CONCAT(a.Ids,b.Number),
76+
a.Depth + 1
77+
FROM cte_GrowingNumbers a,
78+
#Numbers b
79+
WHERE a.Depth < @vTotalNumbers AND
80+
CAST(RIGHT(a.Ids,1) AS INTEGER) + 1 = b.Number
81+
)
82+
SELECT Number
83+
INTO #GrowingNumbers
84+
FROM cte_GrowingNumbers;
85+
GO
86+
87+
---------------------
88+
---------------------
89+
--Display the results
90+
SELECT *
91+
FROM #GrowingNumbers
92+
WHERE LEFT(Number,1) = (SELECT MIN(Number) FROM #Numbers);
93+
GO

0 commit comments

Comments
 (0)