回答編集履歴
1
typesテーブルのidカラムが連番ではない場合の対応方法を記載
test
CHANGED
@@ -37,3 +37,53 @@
|
|
37
37
|
WHERE id = (SELECT MAX(id) FROM Q)
|
38
38
|
|
39
39
|
```
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
---
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
**2021-08-18 10:11追記**
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
typesテーブルのidカラムが連番ではないのであれば、DENSE_RANKを使って連番を生成するやり方があるかと思います。
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
```SQL
|
56
|
+
|
57
|
+
WITH RECURSIVE
|
58
|
+
|
59
|
+
P (id, value) AS (
|
60
|
+
|
61
|
+
SELECT DENSE_RANK() OVER(ORDER BY T.id), T.type || ':' || V.value
|
62
|
+
|
63
|
+
FROM types T
|
64
|
+
|
65
|
+
JOIN values V ON T.id = V.type_id
|
66
|
+
|
67
|
+
),
|
68
|
+
|
69
|
+
Q (id, pattern) AS (
|
70
|
+
|
71
|
+
SELECT id, value FROM P WHERE id = 1
|
72
|
+
|
73
|
+
UNION ALL
|
74
|
+
|
75
|
+
SELECT P.id, pattern || ',' || P.value
|
76
|
+
|
77
|
+
FROM P
|
78
|
+
|
79
|
+
JOIN Q ON P.id = Q.id + 1
|
80
|
+
|
81
|
+
)
|
82
|
+
|
83
|
+
SELECT ROW_NUMBER() OVER() AS no, pattern
|
84
|
+
|
85
|
+
FROM Q
|
86
|
+
|
87
|
+
WHERE id = (SELECT MAX(id) FROM Q)
|
88
|
+
|
89
|
+
```
|