-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2308-ArrangeTableByGender.sql
More file actions
130 lines (123 loc) · 3.36 KB
/
2308-ArrangeTableByGender.sql
File metadata and controls
130 lines (123 loc) · 3.36 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
118
119
120
121
122
123
124
125
126
127
128
129
130
-- 2308. Arrange Table by Gender
-- Table: Genders
-- +-------------+---------+
-- | Column Name | Type |
-- +-------------+---------+
-- | user_id | int |
-- | gender | varchar |
-- +-------------+---------+
-- user_id is the primary key (column with unique values) for this table.
-- gender is ENUM (category) of type 'female', 'male', or 'other'.
-- Each row in this table contains the ID of a user and their gender.
-- The table has an equal number of 'female', 'male', and 'other'.
-- Write a solution to rearrange the Genders table such that the rows alternate between 'female', 'other', and 'male' in order.
-- The table should be rearranged such that the IDs of each gender are sorted in ascending order.
-- Return the result table in the mentioned order.
-- The result format is shown in the following example.
-- Example 1:
-- Input:
-- Genders table:
-- +---------+--------+
-- | user_id | gender |
-- +---------+--------+
-- | 4 | male |
-- | 7 | female |
-- | 2 | other |
-- | 5 | male |
-- | 3 | female |
-- | 8 | male |
-- | 6 | other |
-- | 1 | other |
-- | 9 | female |
-- +---------+--------+
-- Output:
-- +---------+--------+
-- | user_id | gender |
-- +---------+--------+
-- | 3 | female |
-- | 1 | other |
-- | 4 | male |
-- | 7 | female |
-- | 2 | other |
-- | 5 | male |
-- | 9 | female |
-- | 6 | other |
-- | 8 | male |
-- +---------+--------+
-- Explanation:
-- Female gender: IDs 3, 7, and 9.
-- Other gender: IDs 1, 2, and 6.
-- Male gender: IDs 4, 5, and 8.
-- We arrange the table alternating between 'female', 'other', and 'male'.
-- Note that the IDs of each gender are sorted in ascending order.
-- Create table If Not Exists Genders (user_id int, gender ENUM('female', 'other', 'male'))
-- Truncate table Genders
-- insert into Genders (user_id, gender) values ('4', 'male')
-- insert into Genders (user_id, gender) values ('7', 'female')
-- insert into Genders (user_id, gender) values ('2', 'other')
-- insert into Genders (user_id, gender) values ('5', 'male')
-- insert into Genders (user_id, gender) values ('3', 'female')
-- insert into Genders (user_id, gender) values ('8', 'male')
-- insert into Genders (user_id, gender) values ('6', 'other')
-- insert into Genders (user_id, gender) values ('1', 'other')
-- insert into Genders (user_id, gender) values ('9', 'female')
WITH f AS
( -- 女性数据
SELECT
user_id,
gender,
RANK() OVER (ORDER BY user_id) AS rk
FROM
Genders
WHERE
gender = 'female'
),
o AS
( -- 其它数据
SELECT
user_id,
gender,
RANK() OVER (ORDER BY user_id) AS rk
FROM
Genders
WHERE
gender = 'other'
),
m AS
( -- 男性数据
SELECT
user_id,
gender,
RANK() OVER (ORDER BY user_id) AS rk
FROM
Genders
WHERE
gender = 'male'
)
SELECT
user_id,
gender
FROM
(
(
SELECT user_id,gender, rk * 3 AS rk FROM f
)
UNION ALL
(
SELECT user_id,gender, rk * 3 + 1 AS rk FROM o
)
UNION ALL
(
SELECT user_id,gender, rk * 3 + 2 AS rk FROM m
)
) AS r
ORDER BY
rk
-- best solution
SELECT
*
FROM
genders
ORDER BY
ROW_NUMBER() OVER(PARTITION BY gender ORDER BY user_id),
length(gender) DESC