回答編集履歴

6

訂正

2020/08/29 03:34

投稿

sazi
sazi

スコア25199

test CHANGED
@@ -28,34 +28,34 @@
28
28
 
29
29
  with rows as (
30
30
 
31
- select id2, count(*) cnt, count(*) over() max_cnt
31
+ select id2, count(*) cnt from test2
32
-
33
- from test2
34
32
 
35
33
  group by id2
34
+
35
+ ), temp as (
36
+
37
+ select id2 from rows
38
+
39
+ where cnt = (select max(cnt) from rows)
36
40
 
37
41
  )
38
42
 
39
43
  select *
40
44
 
41
- from test
45
+ from test
42
46
 
43
47
  where (
44
48
 
45
- id in ( -- レコード数が最大のもの
49
+ id in (select ID2 from temp) -- レコード数が最大のものがある場合
46
50
 
47
- select ID2 from rows where cnt=max_cnt
51
+ or not exists( -- 同じ値がない場合
48
52
 
49
- )
53
+ select 1 from test2 where id2=test.id
50
54
 
51
- or not exists( -- 同じ値がない場合
55
+ )
52
56
 
53
- select 1 from test2 where id2=test.id
57
+ )
54
58
 
55
- )
56
-
57
- )
58
-
59
- and (foo = 値1 or id = 値2)
59
+ and (foo = 値1 or id = 値2)
60
60
 
61
61
  ```

5

改良

2020/08/29 03:34

投稿

sazi
sazi

スコア25199

test CHANGED
@@ -28,15 +28,11 @@
28
28
 
29
29
  with rows as (
30
30
 
31
- select id2, count(*) cnt from test2
31
+ select id2, count(*) cnt, count(*) over() max_cnt
32
32
 
33
- group by id2
33
+ from test2
34
34
 
35
- ), temp as (
35
+ group by id2
36
-
37
- select id2 from rows
38
-
39
- where cnt = (select max(cnt) from rows)
40
36
 
41
37
  )
42
38
 
@@ -46,7 +42,11 @@
46
42
 
47
43
  where (
48
44
 
45
+ id in ( -- レコード数が最大のもの
46
+
49
- id in (select ID2 from temp) -- レコード数が最大のものがある場合
47
+ select ID2 from rows where cnt=max_cnt
48
+
49
+ )
50
50
 
51
51
  or not exists( -- 同じ値がない場合
52
52
 

4

推敲

2020/08/29 03:30

投稿

sazi
sazi

スコア25199

test CHANGED
@@ -1,4 +1,4 @@
1
- 結果が却って来ないのは、直積の片側が0件なら、結果は0件だからです。
1
+ 結果が却って来ないのは、直積の片側が0件なら、結果(0との積算)は0件だからです。
2
2
 
3
3
  full join すれば結果は却ります。
4
4
 

3

訂正

2020/08/29 03:18

投稿

sazi
sazi

スコア25199

test CHANGED
@@ -46,7 +46,7 @@
46
46
 
47
47
  where (
48
48
 
49
- id = (select ID2 from temp) -- レコード数が最大のものがある場合
49
+ id in (select ID2 from temp) -- レコード数が最大のものがある場合
50
50
 
51
51
  or not exists( -- 同じ値がない場合
52
52
 

2

訂正

2020/08/29 01:35

投稿

sazi
sazi

スコア25199

test CHANGED
@@ -46,11 +46,11 @@
46
46
 
47
47
  where (
48
48
 
49
- id = (selct ID2 from temp) -- レコード数が最大のものがある場合
49
+ id = (select ID2 from temp) -- レコード数が最大のものがある場合
50
50
 
51
51
  or not exists( -- 同じ値がない場合
52
52
 
53
- select 1 from test2 where id2=.test.id
53
+ select 1 from test2 where id2=test.id
54
54
 
55
55
  )
56
56
 

1

追記

2020/08/29 01:34

投稿

sazi
sazi

スコア25199

test CHANGED
@@ -19,3 +19,43 @@
19
19
  on temp.id=temp2.id
20
20
 
21
21
  ```
22
+
23
+
24
+
25
+ やりたいことがいまいち理解できませんが、組み替えてみました。
26
+
27
+ ```SQL
28
+
29
+ with rows as (
30
+
31
+ select id2, count(*) cnt from test2
32
+
33
+ group by id2
34
+
35
+ ), temp as (
36
+
37
+ select id2 from rows
38
+
39
+ where cnt = (select max(cnt) from rows)
40
+
41
+ )
42
+
43
+ select *
44
+
45
+ from test
46
+
47
+ where (
48
+
49
+ id = (selct ID2 from temp) -- レコード数が最大のものがある場合
50
+
51
+ or not exists( -- 同じ値がない場合
52
+
53
+ select 1 from test2 where id2=.test.id
54
+
55
+ )
56
+
57
+ )
58
+
59
+ and (foo = 値1 or id = 値2)
60
+
61
+ ```