-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeloitte_Questions_Custom_Sorting.sql
More file actions
117 lines (100 loc) · 4.31 KB
/
Deloitte_Questions_Custom_Sorting.sql
File metadata and controls
117 lines (100 loc) · 4.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
===============================================================================
PURPOSE:
--------
This query rearranges product names within each category based on a
custom business-defined order of ProductID.
Instead of sorting normally (ASC/DESC), we:
1. Assign a custom rank (rnk) using CASE logic.
2. Reverse that ranking using another ROW_NUMBER().
3. Join the two rankings to "re-map" product names.
-------------------------------------------------------------------------------
BUSINESS RULES:
---------------
- Each category (Electronics, Accessories) has its own custom order.
- The order is NOT based on ProductID sorting, but predefined business logic.
- Final output keeps ProductID from one order and Product name from reversed order.
-------------------------------------------------------------------------------
HOW IT WORKS (STEP-BY-STEP):
---------------------------
1. Create base table and insert sample data.
2. CTE1:
- Assigns a custom rank (rnk) per category using CASE.
3. CTE2:
- Reverses that rank using ROW_NUMBER() over rnk DESC.
4. Final Join:
- Match original rank (rnk) with reversed rank (rnk2)
- This swaps product names within each category.
===============================================================================
*/
-- ============================================================================
-- STEP 1: CREATE TABLE
-- ============================================================================
CREATE TABLE Products (
ProductID INT PRIMARY KEY, -- Unique identifier for each product
Product VARCHAR(50), -- Product name
Category VARCHAR(50) -- Category grouping (Electronics / Accessories)
);
-- ============================================================================
-- STEP 2: INSERT SAMPLE DATA
-- ============================================================================
INSERT INTO Products (ProductID, Product, Category) VALUES
-- Electronics category products
(101, 'Gaming Laptop', 'Electronics'),
(102, 'iPhone', 'Electronics'),
(108, 'iPad', 'Electronics'),
(104, 'Scanner', 'Electronics'),
-- Accessories category products
(105, 'Bluetooth Earbuds', 'Accessories'),
(106, 'Fitness Band', 'Accessories'),
(107, 'Mechanical Keyboard', 'Accessories'),
(103, 'Wireless Mouse', 'Accessories'),
(109, 'LED Monitor', 'Accessories');
-- View base data
SELECT * FROM Products;
-- ============================================================================
-- STEP 3: APPLY CUSTOM ORDER + REVERSAL LOGIC
-- ============================================================================
WITH cte1 AS (
SELECT
*,
ROW_NUMBER() OVER (
PARTITION BY category
ORDER BY
CASE
-- Custom order for Electronics
WHEN productid = 104 AND category='Electronics' THEN 1
WHEN productid = 108 AND category='Electronics' THEN 2
WHEN productid = 102 AND category='Electronics' THEN 3
WHEN productid = 101 AND category='Electronics' THEN 4
-- Custom order for Accessories
WHEN productid = 109 AND category='Accessories' THEN 1
WHEN productid = 103 AND category='Accessories' THEN 2
WHEN productid = 107 AND category='Accessories' THEN 3
WHEN productid = 106 AND category='Accessories' THEN 4
WHEN productid = 105 AND category='Accessories' THEN 5
END
) AS rnk -- Assign custom rank per category
FROM products
),
cte2 AS (
SELECT
*,
ROW_NUMBER() OVER (
PARTITION BY category
ORDER BY rnk DESC -- Reverse the ranking
) AS rnk2
FROM cte1
)
-- ============================================================================
-- STEP 4: FINAL OUTPUT
-- ============================================================================
SELECT
t1.productid, -- Keep original ProductID
t2.product, -- Swap product name using reversed rank
t1.category
FROM cte1 t1
INNER JOIN cte2 t2
ON t1.category = t2.category
AND t1.rnk = t2.rnk2 -- Match forward rank with reverse rank
ORDER BY t1.category, t1.rnk;