回答編集履歴

5

SQL整形

2018/05/25 05:29

投稿

sazi
sazi

スコア25173

test CHANGED
@@ -46,9 +46,9 @@
46
46
 
47
47
  ,array_to_string(array(
48
48
 
49
- select name from テーブルA
49
+ select name from テーブルA
50
50
 
51
- where ((2^(id-1))::bigint & b.bit)>0
51
+ where ((2^(id-1))::bigint & b.bit)>0
52
52
 
53
53
  ),',')
54
54
 

4

biit 属性をbigintに変更およびpostgres 8.1用に変更

2018/05/25 05:29

投稿

sazi
sazi

スコア25173

test CHANGED
@@ -33,3 +33,35 @@
33
33
  where b.id=3
34
34
 
35
35
  ```
36
+
37
+ 追記
38
+
39
+ ---
40
+
41
+ array_aggをpostgres 8.1で展開
42
+
43
+ ```SQL
44
+
45
+ select *
46
+
47
+ ,array_to_string(array(
48
+
49
+ select name from テーブルA
50
+
51
+ where ((2^(id-1))::bigint & b.bit)>0
52
+
53
+ ),',')
54
+
55
+ from テーブルB b
56
+
57
+ ```
58
+
59
+ 尚、bigintだと2の62乗までしか展開できません。
60
+
61
+ また、ビット文字列演算子(&)は整数に対するものなのでbigintが上限です。
62
+
63
+ 要は、テーブルAには63・64は登録できないということになります。
64
+
65
+
66
+
67
+ ”桁あふれ”についてはそもそもbitに2の63乗以上は登録できないのですから、考慮は無意味だと思いますが。

3

名称修正

2018/05/25 05:27

投稿

sazi
sazi

スコア25173

test CHANGED
@@ -26,9 +26,9 @@
26
26
 
27
27
  select *
28
28
 
29
- ,(select array_to_string(array_agg(item),',') from table_a where ((2^(id-1))::int & b.bit)>0)
29
+ ,(select array_to_string(array_agg(name),',') from テーブルA where ((2^(id-1))::int & b.bit)>0)
30
30
 
31
- from table_b b
31
+ from テーブルB b
32
32
 
33
33
  where b.id=3
34
34
 

2

追記

2018/05/24 02:52

投稿

sazi
sazi

スコア25173

test CHANGED
@@ -13,3 +13,23 @@
13
13
  where b.id=3
14
14
 
15
15
  ```
16
+
17
+ 追記
18
+
19
+ --
20
+
21
+ 質問の意味を取り違えていたようです。
22
+
23
+ テーブルAに64bitのフラグごとのitemが登録されていて、それをテーブルBのIDの各フラグごとに対応させるということですね。
24
+
25
+ ```SQL
26
+
27
+ select *
28
+
29
+ ,(select array_to_string(array_agg(item),',') from table_a where ((2^(id-1))::int & b.bit)>0)
30
+
31
+ from table_b b
32
+
33
+ where b.id=3
34
+
35
+ ```

1

where条件追加

2018/05/24 02:50

投稿

sazi
sazi

スコア25173

test CHANGED
@@ -10,4 +10,6 @@
10
10
 
11
11
  on b.bit=2^(a.id-1)
12
12
 
13
+ where b.id=3
14
+
13
15
  ```