teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

9

条件の追加

2018/03/19 09:43

投稿

iki
iki

スコア12

title CHANGED
File without changes
body CHANGED
@@ -138,13 +138,14 @@
138
138
  from (
139
139
  select cmpgn.shisaku_id, cmpgn.shisaku_name, cmpgn.kihon_id
140
140
  , case when p_date between param.campaign_Start and param.campaign_End
141
- and (fuyo_pt_tj is not null or kan_pt_tj is not null or kan_pt_kg is not null)
141
+ and (fuyo_pt_tj is not null or kan_pt_tj is not null )
142
142
  then '消費' else '消費なし'
143
143
  end as shohi_flg
144
144
 
145
145
 
146
146
 
147
147
         , case when p_date between param.campaign_Start and param.campaign_End
148
+           and (fuyo_pt_tj is not null or kan_pt_tj is not null )
148
149
  and (shisaku_id is not null)
149
150
  then '施策利用なし' else '施策利用あり'
150
151
  end as shisaku_umu_flg

8

順序

2018/03/19 09:43

投稿

iki
iki

スコア12

title CHANGED
File without changes
body CHANGED
@@ -161,8 +161,8 @@
161
161
  left join point_rireki2 pnt
162
162
  on cmpgn.kihon_id=pnt.kihon_id
163
163
  ) step1
164
- group by shisaku_id, shisaku_name,shohi_flg,demogra_flg
164
+ group by shisaku_id, shisaku_name,shohi_flg,shisaku_umu_flg, demogra_flg
165
- order by shisaku_id,shohi_flg,demogra_flg
165
+ order by shisaku_id,shohi_flg,shisaku_umu_flg,demogra_flg
166
166
 
167
167
 
168
168
 

7

修正

2018/03/19 09:23

投稿

iki
iki

スコア12

title CHANGED
File without changes
body CHANGED
@@ -134,7 +134,7 @@
134
134
  with param as (
135
135
  select '2018-02-07':: date apply_date_limit ,'2018-01-26':: date campaign_Start,'2018-02-09':: date campaign_End
136
136
  )
137
- select shisaku_id, shisaku_name, shohi_flg,demogra_flg, count(distinct kihon_id) as patarn_count
137
+ select shisaku_id, shisaku_name, shohi_flg, shisaku_umu_flg , demogra_flg, count(distinct kihon_id) as patarn_count
138
138
  from (
139
139
  select cmpgn.shisaku_id, cmpgn.shisaku_name, cmpgn.kihon_id
140
140
  , case when p_date between param.campaign_Start and param.campaign_End

6

→追加

2018/03/19 09:22

投稿

iki
iki

スコア12

title CHANGED
File without changes
body CHANGED
@@ -122,12 +122,12 @@
122
122
          〈定義〉
123
123
           キャンペーン利用をせずにポイント利用がある人:"施策利用なし"と定義したい 
124
124
              →ポイント履歴のテーブルに、利用履歴がある(fuyo_pt_tj, あるいはkan_pt_tjが0でない)
125
-              キャンペーンテーブルにある、施策の履歴がない(=キャンペーンテーブルとは紐づかない形でのポイント利用がある)
125
+             →キャンペーンテーブルにある、施策の履歴がない(=キャンペーンテーブルとは紐づかない形でのポイント利用がある)
126
126
 
127
127
 
128
128
      キャンペーン利用にてポイント利用がある人:"施策利用あり"と定義したい 
129
129
              →ポイント履歴のテーブルに、利用履歴がある(fuyo_pt_tj, あるいはkan_pt_tjが0でない)
130
-              キャンペーンテーブルにある、施策利用に紐づいたポイント利用となっている
130
+             →キャンペーンテーブルにある、施策利用に紐づいたポイント利用となっている
131
131
 
132
132
  ```lang-sql
133
133
 

5

条件の追加

2018/03/19 08:10

投稿

iki
iki

スコア12

title CHANGED
File without changes
body CHANGED
@@ -113,4 +113,58 @@
113
113
 
114
114
  【現在の環境】
115
115
  Windows
116
- Postgre(pgAdmin4内のクエリツールよりクエリを書いてます) を利用しています
116
+ Postgre(pgAdmin4内のクエリツールよりクエリを書いてます) を利用しています
117
+
118
+
119
+
120
+ 【追記】
121
+ やりたい内容: キャンペーン利用の有無によるポイント消費者を区分けするフラグを作りたい
122
+         〈定義〉
123
+          キャンペーン利用をせずにポイント利用がある人:"施策利用なし"と定義したい 
124
+             →ポイント履歴のテーブルに、利用履歴がある(fuyo_pt_tj, あるいはkan_pt_tjが0でない)
125
+              キャンペーンテーブルにある、施策の履歴がない(=キャンペーンテーブルとは紐づかない形でのポイント利用がある)
126
+
127
+
128
+     キャンペーン利用にてポイント利用がある人:"施策利用あり"と定義したい 
129
+             →ポイント履歴のテーブルに、利用履歴がある(fuyo_pt_tj, あるいはkan_pt_tjが0でない)
130
+              キャンペーンテーブルにある、施策利用に紐づいたポイント利用となっている
131
+
132
+ ```lang-sql
133
+
134
+ with param as (
135
+ select '2018-02-07':: date apply_date_limit ,'2018-01-26':: date campaign_Start,'2018-02-09':: date campaign_End
136
+ )
137
+ select shisaku_id, shisaku_name, shohi_flg,demogra_flg, count(distinct kihon_id) as patarn_count
138
+ from (
139
+ select cmpgn.shisaku_id, cmpgn.shisaku_name, cmpgn.kihon_id
140
+ , case when p_date between param.campaign_Start and param.campaign_End
141
+ and (fuyo_pt_tj is not null or kan_pt_tj is not null or kan_pt_kg is not null)
142
+ then '消費' else '消費なし'
143
+ end as shohi_flg
144
+
145
+
146
+
147
+        , case when p_date between param.campaign_Start and param.campaign_End
148
+ and (shisaku_id is not null)
149
+ then '施策利用なし' else '施策利用あり'
150
+ end as shisaku_umu_flg
151
+
152
+
153
+
154
+ , case gender when 1 then 'M' when 2 then 'F' end ||
155
+ case when old between 20 and 34 then '1'
156
+ when old between 35 and 49 then '2'
157
+ when old >= 50 then '3'
158
+ end as demogra_flg
159
+ from param left join campaign cmpgn
160
+ on cmpgn.apply_date < param.apply_date_limit
161
+ left join point_rireki2 pnt
162
+ on cmpgn.kihon_id=pnt.kihon_id
163
+ ) step1
164
+ group by shisaku_id, shisaku_name,shohi_flg,demogra_flg
165
+ order by shisaku_id,shohi_flg,demogra_flg
166
+
167
+
168
+
169
+
170
+ ```

4

表記内容を修正しました

2018/03/19 08:04

投稿

iki
iki

スコア12

title CHANGED
File without changes
body CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
   〈仕様〉
6
6
    ・テーブルの項目について:
7
-    [テーブル項目](http://sqlfiddle.com/#!17/7fcf5/1)
7
+    [テーブル項目](http://sqlfiddle.com/#!17/5602f/1)
8
8
     -fuyo_pt_tj, kan_pt_tj:ポイントのカラムになります。
9
9
  -kihon_id:ユーザーidになります
10
10
  -sex:1-男性、2-女性、3-不明となります
@@ -25,11 +25,11 @@
25
25
  〈テーブルの内容〉
26
26
  ```lang-sql
27
27
  CREATE TABLE point_rireki2
28
- ("date" date, "kihon_id" int, "fuyo_pt_tj" int, "kan_pt_tj" int, "old" int, "sex" int, "area" varchar(3))
28
+ ("p_date" date, "kihon_id" int, "fuyo_pt_tj" int, "kan_pt_tj" int, "old" int, "gender" int, "area" varchar(3))
29
29
  ;
30
30
 
31
31
  INSERT INTO point_rireki2
32
- ("date", "kihon_id", "fuyo_pt_tj", "kan_pt_tj", "old", "sex", "area")
32
+ ("p_date", "kihon_id", "fuyo_pt_tj", "kan_pt_tj", "old", "gender", "area")
33
33
  VALUES
34
34
  ('2018-01-01', 1001, 10, 200, 10, 1, '東京'),
35
35
  ('2018-01-17', 1005, 100, 50, 22, 3, '千葉'),
@@ -39,7 +39,6 @@
39
39
  ('2018-01-28', 1010, 120, 90, 30, 2, '神奈川'),
40
40
  ('2018-02-01', 1010, 170, 80, 30, 2, '神奈川'),
41
41
  ('2018-02-03', 1015, 40, 280, -3, 1, '東京')
42
- ;
43
42
 
44
43
 
45
44
  CREATE TABLE campaign
@@ -85,24 +84,24 @@
85
84
  , case when exists(
86
85
  select 1 from point_rireki2
87
86
  where kihon_id = cmpgn.kihon_id
88
- and "date" between param.campaign_Start and param.campaign_End
87
+ and "p_date" between param.campaign_Start and param.campaign_End
89
88
  and (fuyo_pt_tj is not null or kan_pt_tj is not null )
90
89
  ) then '消費' else '消費なし'
91
90
  end as shohi_flg
92
91
  from campaign cmpgn cross join param
93
92
  where cmpgn.apply_date < param.apply_date_limit
94
93
 
95
- , case when sex = 1 and old >= 20 and old <= 34
94
+ , case when gender = 1 and old >= 20 and old <= 34
96
95
  then'M1'
97
- when sex = 1 and old >= 35 and old <= 49
96
+ when gender = 1 and old >= 35 and old <= 49
98
97
  then'M2'
99
- when sex = 1 and old >= 50
98
+ when gender = 1 and old >= 50
100
99
  then'M3'
101
- when sex = 2 and old >= 20 and old <= 34
100
+ when gender = 2 and old >= 20 and old <= 34
102
101
  then'F1'
103
- when sex = 2 and old >= 35 and old <= 49
102
+ when gender = 2 and old >= 35 and old <= 49
104
103
  then'F2'
105
- when sex = 2 and old >= 50
104
+ when gender = 2 and old >= 50
106
105
  then'F3'
107
106
  else null
108
107
  end as demogra_flg

3

データの件数を追加

2018/03/18 08:02

投稿

iki
iki

スコア12

title CHANGED
File without changes
body CHANGED
@@ -58,7 +58,13 @@
58
58
  ;
59
59
  ```
60
60
 
61
+  〈テーブルの件数〉
62
+   ・point_rireki2:400万行  
63
+   ・campaign   :1万行
61
64
 
65
+
66
+
67
+
62
68
  【わからないこと】
63
69
  現状に記載している、サブクエリ内のcase文の書き方が分からず困っています。
64
70
  2つのcase文にて、それぞれの条件の絞り込みに必要なテーブルが異なる場合、どう記述すればよいのでしょうか。

2

指摘頂いた内容を修正させて頂きました!

2018/03/17 10:41

投稿

iki
iki

スコア12

title CHANGED
File without changes
body CHANGED
@@ -69,7 +69,7 @@
69
69
  【現状】
70
70
  ```lang-sql
71
71
  with param as (
72
- select '2018-02-07':: date oubo_date_limit ,'2018-01-26':: date campaign_Start,'2018-02-09':: date campaign_End
72
+ select '2018-02-07':: date apply_date_limit ,'2018-01-26':: date campaign_Start,'2018-02-09':: date campaign_End
73
73
  )
74
74
 
75
75
 
@@ -80,11 +80,11 @@
80
80
  select 1 from point_rireki2
81
81
  where kihon_id = cmpgn.kihon_id
82
82
  and "date" between param.campaign_Start and param.campaign_End
83
- and (fuyo_pt_tj is not null or kan_pt_tj is not null or kan_pt_kg is not null)
83
+ and (fuyo_pt_tj is not null or kan_pt_tj is not null )
84
84
  ) then '消費' else '消費なし'
85
85
  end as shohi_flg
86
86
  from campaign cmpgn cross join param
87
- where cmpgn.oubo_date < param.oubo_date_limit
87
+ where cmpgn.apply_date < param.apply_date_limit
88
88
 
89
89
  , case when sex = 1 and old >= 20 and old <= 34
90
90
  then'M1'

1

ご指摘頂いた内容を修正しました

2018/03/17 10:39

投稿

iki
iki

スコア12

title CHANGED
File without changes
body CHANGED
@@ -4,8 +4,8 @@
4
4
 
5
5
   〈仕様〉
6
6
    ・テーブルの項目について:
7
-    http://sqlfiddle.com/#!9/d2c7c6/1
7
+    [テーブル項目](http://sqlfiddle.com/#!17/7fcf5/1)
8
-    -fuyo_pt_tj`, `kan_pt_tj`:ポイントのカラムになります。
8
+    -fuyo_pt_tj, kan_pt_tj:ポイントのカラムになります。
9
9
  -kihon_id:ユーザーidになります
10
10
  -sex:1-男性、2-女性、3-不明となります
11
11
    ・キャンペーン期間:1/26~2/9です
@@ -16,10 +16,49 @@
16
16
  F3:女性、50歳以上
17
17
           M1:男性、20~34歳
18
18
  M2:男性、34~49歳
19
- FM3:男性、50歳以上
19
+ M3:男性、50歳以上
20
20
      
21
21
    ・キャンペーンテーブルにおいて、2/6までの応募日でデータを絞りたいと思っています。
22
22
 
23
+
24
+
25
+ 〈テーブルの内容〉
26
+ ```lang-sql
27
+ CREATE TABLE point_rireki2
28
+ ("date" date, "kihon_id" int, "fuyo_pt_tj" int, "kan_pt_tj" int, "old" int, "sex" int, "area" varchar(3))
29
+ ;
30
+
31
+ INSERT INTO point_rireki2
32
+ ("date", "kihon_id", "fuyo_pt_tj", "kan_pt_tj", "old", "sex", "area")
33
+ VALUES
34
+ ('2018-01-01', 1001, 10, 200, 10, 1, '東京'),
35
+ ('2018-01-17', 1005, 100, 50, 22, 3, '千葉'),
36
+ ('2018-01-22', 1010, 99, 10, 30, 2, '神奈川'),
37
+ ('2018-01-25', 1001, 10, 200, NULL, 1, '東京'),
38
+ ('2018-01-26', 1015, 190, 20, 40, 3, '東京'),
39
+ ('2018-01-28', 1010, 120, 90, 30, 2, '神奈川'),
40
+ ('2018-02-01', 1010, 170, 80, 30, 2, '神奈川'),
41
+ ('2018-02-03', 1015, 40, 280, -3, 1, '東京')
42
+ ;
43
+
44
+
45
+ CREATE TABLE campaign
46
+ ("apply_id" int, "shisaku_id" varchar(6), "shisaku_name" varchar(3), "kihon_id" int, "apply_date" date)
47
+ ;
48
+
49
+ INSERT INTO campaign
50
+ ("apply_id", "shisaku_id", "shisaku_name", "kihon_id", "apply_date")
51
+ VALUES
52
+ (10001, 'AYC100', 'C施策', 1001, '2018-01-25'),
53
+ (10002, 'AYC100', 'C施策', 1005, '2018-01-17'),
54
+ (10003, 'AYC100', 'C施策', 1010, '2018-01-28'),
55
+ (10004, 'AYB100', 'B施策', 1015, '2018-01-26'),
56
+ (10005, 'AYB100', 'B施策', 1010, '2018-02-01'),
57
+ (10006, 'AYC100', 'C施策', 1001, '2018-01-01')
58
+ ;
59
+ ```
60
+
61
+
23
62
  【わからないこと】
24
63
  現状に記載している、サブクエリ内のcase文の書き方が分からず困っています。
25
64
  2つのcase文にて、それぞれの条件の絞り込みに必要なテーブルが異なる場合、どう記述すればよいのでしょうか。
@@ -28,6 +67,7 @@
28
67
 
29
68
 
30
69
  【現状】
70
+ ```lang-sql
31
71
  with param as (
32
72
  select '2018-02-07':: date oubo_date_limit ,'2018-01-26':: date campaign_Start,'2018-02-09':: date campaign_End
33
73
  )
@@ -64,8 +104,8 @@
64
104
  ) step1
65
105
  group by shisaku_id, shisaku_name,shohi_flg,demogra_flg
66
106
  order by shisaku_id, new_or_existing,shohi_flg
107
+ ```
67
108
 
68
-
69
109
  【現在の環境】
70
110
  Windows
71
111
  Postgre(pgAdmin4内のクエリツールよりクエリを書いてます) を利用しています