回答編集履歴

1

そのまま試せるようにおまけつき

2020/08/14 10:55

投稿

退会済みユーザー
test CHANGED
@@ -79,3 +79,89 @@
79
79
  sumchange;
80
80
 
81
81
  ```
82
+
83
+ ```text
84
+
85
+ userid | start | end | a | b | c
86
+
87
+ --------+---------------------+---------------------+---+---+---
88
+
89
+ user1 | 2020-01-01 00:00:00 | 2020-01-01 00:01:00 | 0 | 0 | 0
90
+
91
+ user1 | 2020-01-01 00:02:00 | 2020-01-01 00:02:00 | 0 | 1 | 0
92
+
93
+ user1 | 2020-01-01 00:03:00 | 2020-01-01 00:04:00 | 0 | 0 | 0
94
+
95
+ user1 | 2020-01-01 00:05:00 | 2020-01-01 00:06:00 | 0 | 1 | 0
96
+
97
+ user2 | 2020-01-01 00:00:00 | 2020-01-01 00:02:00 | 0 | 0 | 0
98
+
99
+ user2 | 2020-01-01 00:03:00 | 2020-01-01 00:04:00 | 1 | 0 | 0
100
+
101
+ user2 | 2020-01-01 00:05:00 | 2020-01-01 00:06:00 | 0 | 0 | 0
102
+
103
+ (7 rows)
104
+
105
+ ```
106
+
107
+ ### おまけ
108
+
109
+ #### DDL
110
+
111
+ ```sql
112
+
113
+ create table sample(
114
+
115
+ userid varchar(10),
116
+
117
+ record_date timestamp,
118
+
119
+ a numeric(1),
120
+
121
+ b numeric(1),
122
+
123
+ c numeric(1),
124
+
125
+ primary key(userid, record_date)
126
+
127
+ );
128
+
129
+
130
+
131
+ ```
132
+
133
+ #### データ投入
134
+
135
+ ```sql
136
+
137
+ insert into sample values ('user1','2020/1/1 00:00:00',0,0,0);
138
+
139
+ insert into sample values ('user1','2020/1/1 00:01:00',0,0,0);
140
+
141
+ insert into sample values ('user1','2020/1/1 00:02:00',0,1,0);
142
+
143
+ insert into sample values ('user1','2020/1/1 00:03:00',0,0,0);
144
+
145
+ insert into sample values ('user1','2020/1/1 00:04:00',0,0,0);
146
+
147
+ insert into sample values ('user1','2020/1/1 00:05:00',0,1,0);
148
+
149
+ insert into sample values ('user1','2020/1/1 00:06:00',0,1,0);
150
+
151
+ insert into sample values ('user2','2020/1/1 00:00:00',0,0,0);
152
+
153
+ insert into sample values ('user2','2020/1/1 00:01:00',0,0,0);
154
+
155
+ insert into sample values ('user2','2020/1/1 00:02:00',0,0,0);
156
+
157
+ insert into sample values ('user2','2020/1/1 00:03:00',1,0,0);
158
+
159
+ insert into sample values ('user2','2020/1/1 00:04:00',1,0,0);
160
+
161
+ insert into sample values ('user2','2020/1/1 00:05:00',0,0,0);
162
+
163
+ insert into sample values ('user2','2020/1/1 00:06:00',0,0,0);
164
+
165
+ commit;
166
+
167
+ ```