回答編集履歴
4
追記
test
CHANGED
@@ -22,7 +22,7 @@
|
|
22
22
|
|
23
23
|
```
|
24
24
|
|
25
|
-
③の条件がIDに関するものというのに気付
|
25
|
+
③の条件がIDに関するものというのに気付けなかった。
|
26
26
|
|
27
27
|
解決したようですけど、取り敢えず修正版です。
|
28
28
|
|
3
追記
test
CHANGED
@@ -28,26 +28,28 @@
|
|
28
28
|
|
29
29
|
```SQL
|
30
30
|
|
31
|
-
select
|
31
|
+
select join1.*
|
32
32
|
|
33
|
-
|
33
|
+
from t_transactions join1
|
34
34
|
|
35
|
-
|
35
|
+
inner join (
|
36
36
|
|
37
|
-
|
37
|
+
select max(id) as id from t_transactions
|
38
38
|
|
39
|
-
|
39
|
+
where amount >= 0
|
40
40
|
|
41
|
-
|
41
|
+
group by tran_grp, payment_date
|
42
42
|
|
43
|
+
) join2
|
44
|
+
|
45
|
+
on join1.id=join2.id
|
46
|
+
|
43
|
-
|
47
|
+
where join1.id not in (
|
44
48
|
|
45
49
|
select settle_grp from t_transactions where settle_grp IS NOT NULL
|
46
50
|
|
47
51
|
)
|
48
52
|
|
49
|
-
and amount>=0
|
50
|
-
|
51
53
|
order by id
|
52
54
|
|
53
55
|
```
|
2
追記
test
CHANGED
@@ -21,3 +21,33 @@
|
|
21
21
|
on join1.id=join2.id
|
22
22
|
|
23
23
|
```
|
24
|
+
|
25
|
+
③の条件がIDに関するものというのに気付いてなかった。
|
26
|
+
|
27
|
+
解決したようですけど、取り敢えず修正版です。
|
28
|
+
|
29
|
+
```SQL
|
30
|
+
|
31
|
+
select * from t_transactions
|
32
|
+
|
33
|
+
where id in (
|
34
|
+
|
35
|
+
select MAX(id) AS id from t_transactions group by tran_grp
|
36
|
+
|
37
|
+
union
|
38
|
+
|
39
|
+
select id from t_transactions where settle_grp IS not null
|
40
|
+
|
41
|
+
)
|
42
|
+
|
43
|
+
and id not in (
|
44
|
+
|
45
|
+
select settle_grp from t_transactions where settle_grp IS NOT NULL
|
46
|
+
|
47
|
+
)
|
48
|
+
|
49
|
+
and amount>=0
|
50
|
+
|
51
|
+
order by id
|
52
|
+
|
53
|
+
```
|
1
追記
test
CHANGED
@@ -1,4 +1,6 @@
|
|
1
1
|
`#最終的にほしいレコード`と質問の条件について合致しない部分があるので、`#最終的にほしいレコード`に合わせる形でSQLを組んでみました。
|
2
|
+
|
3
|
+
※`②select id from t_transactions where settle_grp IS not null;`を含めると欲しい形にはなりません。
|
2
4
|
|
3
5
|
```SQL
|
4
6
|
|