回答編集履歴
5
追記
test
CHANGED
@@ -49,3 +49,39 @@
|
|
49
49
|
where tgt.会員ID = src.会員ID
|
50
50
|
|
51
51
|
```
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
追記2
|
56
|
+
|
57
|
+
--
|
58
|
+
|
59
|
+
```SQL
|
60
|
+
|
61
|
+
select *
|
62
|
+
|
63
|
+
, case when 集計_総額 >= 33000 then 1
|
64
|
+
|
65
|
+
when 集計_総額 < 33000 and 集計_総額 >= 25000 then 2
|
66
|
+
|
67
|
+
else 3
|
68
|
+
|
69
|
+
end as Mランク
|
70
|
+
|
71
|
+
from (
|
72
|
+
|
73
|
+
select *
|
74
|
+
|
75
|
+
, max(購入日) over(partition by 会員ID) as 集計_直近購入日
|
76
|
+
|
77
|
+
, count(*) over(partition by 会員ID) as 集計_回数
|
78
|
+
|
79
|
+
, sum(金額) over(partition by 会員ID) as 集計_総額
|
80
|
+
|
81
|
+
from uriage_total
|
82
|
+
|
83
|
+
) step1
|
84
|
+
|
85
|
+
```
|
86
|
+
|
87
|
+
※rfm_analytics_raw_totalをデータとして確定したいのなら、上記を元にupsertすれば良いかと。
|
4
訂正
test
CHANGED
@@ -24,7 +24,7 @@
|
|
24
24
|
|
25
25
|
|
26
26
|
|
27
|
-
追記
|
27
|
+
追記(訂正)
|
28
28
|
|
29
29
|
--
|
30
30
|
|
@@ -32,7 +32,7 @@
|
|
32
32
|
|
33
33
|
update rfm_analytics_raw_total tgt
|
34
34
|
|
35
|
-
set
|
35
|
+
set 集計_総額= src.amt_total
|
36
36
|
|
37
37
|
from (
|
38
38
|
|
3
推敲
test
CHANGED
@@ -30,7 +30,7 @@
|
|
30
30
|
|
31
31
|
```SQL
|
32
32
|
|
33
|
-
update rfm_analytics_raw_total
|
33
|
+
update rfm_analytics_raw_total tgt
|
34
34
|
|
35
35
|
set tgt.集計_総額= src.amt_total
|
36
36
|
|
@@ -44,7 +44,7 @@
|
|
44
44
|
|
45
45
|
group by tt2.会員ID
|
46
46
|
|
47
|
-
)
|
47
|
+
) src
|
48
48
|
|
49
49
|
where tgt.会員ID = src.会員ID
|
50
50
|
|
2
追記
test
CHANGED
@@ -21,3 +21,31 @@
|
|
21
21
|
sum ( interval ) → interval
|
22
22
|
|
23
23
|
sum ( money ) → money
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
追記
|
28
|
+
|
29
|
+
--
|
30
|
+
|
31
|
+
```SQL
|
32
|
+
|
33
|
+
update rfm_analytics_raw_total as tgt
|
34
|
+
|
35
|
+
set tgt.集計_総額= src.amt_total
|
36
|
+
|
37
|
+
from (
|
38
|
+
|
39
|
+
select tt2.会員ID, sum(tt1.金額) ::integer as amt_total
|
40
|
+
|
41
|
+
from uriage_total tt1
|
42
|
+
|
43
|
+
join rfm_analytics_raw_total tt2 using(会員ID)
|
44
|
+
|
45
|
+
group by tt2.会員ID
|
46
|
+
|
47
|
+
) as src
|
48
|
+
|
49
|
+
where tgt.会員ID = src.会員ID
|
50
|
+
|
51
|
+
```
|
1
追記
test
CHANGED
@@ -1,5 +1,23 @@
|
|
1
|
-
sum()の結果はbigintですので、integerに格納
|
1
|
+
sum(integer)の結果はbigintですので、integerに格納しようとしてのエラーだと思われます。
|
2
2
|
|
3
3
|
|
4
4
|
|
5
5
|
sum()の結果をintegerにcastして下さい。
|
6
|
+
|
7
|
+
[9.21. 集約関数](https://www.postgresql.jp/document/13/html/functions-aggregate.html)
|
8
|
+
|
9
|
+
> sum ( smallint ) → bigint
|
10
|
+
|
11
|
+
sum ( integer ) → bigint
|
12
|
+
|
13
|
+
sum ( bigint ) → numeric
|
14
|
+
|
15
|
+
sum ( numeric ) → numeric
|
16
|
+
|
17
|
+
sum ( real ) → real
|
18
|
+
|
19
|
+
sum ( double precision ) → double precision
|
20
|
+
|
21
|
+
sum ( interval ) → interval
|
22
|
+
|
23
|
+
sum ( money ) → money
|