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

回答編集履歴

1

調整

2019/04/02 05:05

投稿

yambejp
yambejp

スコア117878

answer CHANGED
@@ -14,8 +14,11 @@
14
14
  (5,3,'2019/03','2019-03-10 01:00:00'),
15
15
  (6,3,'2019/03','2019-03-01 02:00:00'),
16
16
  (7,3,'2019/03','2019-03-20 03:00:00'),
17
- (8,3,'2019/02','2019-02-01 04:00:00');
17
+ (8,3,'2019/02','2019-02-01 04:00:00'),
18
+ (9,4,'2019/03','2019-04-01 05:00:00'),
19
+ (10,5,'2019/03','2019-04-02 06:00:00');
18
20
  ```
21
+ ※insertデータを調整
19
22
 
20
23
  - 抽出
21
24
  ```SQL
@@ -26,4 +29,22 @@
26
29
  and wk_month=t1.wk_month
27
30
  and created_at<t1.created_at)
28
31
  and wk_month='2019/03'
32
+ ```
33
+
34
+ # 2行以上あるデータのみ
35
+ ```SQL
36
+ select * from tbl as t1
37
+ where not exists
38
+ (select 1 from tbl
39
+ where employee_id=t1.employee_id
40
+ and wk_month=t1.wk_month
41
+ and created_at<t1.created_at
42
+ )
43
+ and exists
44
+ (select 1 from tbl
45
+ where employee_id=t1.employee_id
46
+ and wk_month=t1.wk_month
47
+ having count(*)>1
48
+ )
49
+ and wk_month='2019/03'
29
50
  ```