回答編集履歴

17

推敲

2018/06/28 04:16

投稿

sazi
sazi

スコア25173

test CHANGED
@@ -44,7 +44,7 @@
44
44
 
45
45
 
46
46
 
47
- 店舗マスタと商品分類マスタを結合(or条件がミソ)し、店舗別の共通分類を含めた一覧としてそれを集計する
47
+ 店舗マスタと商品分類マスタを結合(or条件がポイント)し、店舗別の共通分類を含めた一覧としてそれを集計する
48
48
 
49
49
  ```SQL
50
50
 

16

修正(left join →inner join)

2018/06/28 04:16

投稿

sazi
sazi

スコア25173

test CHANGED
@@ -50,7 +50,7 @@
50
50
 
51
51
  select str.store_id
52
52
 
53
- from store_mst str left join tag_mst tag
53
+ from store_mst str inner join tag_mst tag
54
54
 
55
55
  on str.groupstore_id=tag.groupstore_id or str.store_id=tag.store_id
56
56
 
@@ -74,7 +74,7 @@
74
74
 
75
75
  select count(*)
76
76
 
77
- from store_mst str left join tag_mst tag
77
+ from store_mst str inner join tag_mst tag
78
78
 
79
79
  on str.groupstore_id=tag.groupstore_id or str.store_id=tag.store_id
80
80
 

15

修正

2018/06/28 02:29

投稿

sazi
sazi

スコア25173

test CHANGED
@@ -68,7 +68,7 @@
68
68
 
69
69
  ```
70
70
 
71
- 指定した店舗のみの状況でいいなら、件数のみで判断できるので、以下のようにすると簡潔になります。
71
+ 質問のSQLが要件を満たしている場合、指定した店舗のみの状況件数のみで判断できるので、以下のようにすると簡潔になります。
72
72
 
73
73
  ```SQL
74
74
 

14

修正

2018/06/28 02:26

投稿

sazi
sazi

スコア25173

test CHANGED
@@ -68,11 +68,11 @@
68
68
 
69
69
  ```
70
70
 
71
- 指定した店舗のみの状況でいいなら、
71
+ 指定した店舗のみの状況でいいなら、件数のみで判断できるので、以下のようにすると簡潔になります。
72
72
 
73
73
  ```SQL
74
74
 
75
- select str.store_id
75
+ select count(*)
76
76
 
77
77
  from store_mst str left join tag_mst tag
78
78
 
@@ -82,8 +82,4 @@
82
82
 
83
83
  and str.store_id=/*storeId*/101
84
84
 
85
- group by str.store_id
86
-
87
- having count(*)>5
88
-
89
85
  ```

13

追記

2018/06/28 02:23

投稿

sazi
sazi

スコア25173

test CHANGED
@@ -67,3 +67,23 @@
67
67
  having count(*)>5
68
68
 
69
69
  ```
70
+
71
+ 指定した店舗のみの状況でいいなら、
72
+
73
+ ```SQL
74
+
75
+ select str.store_id
76
+
77
+ from store_mst str left join tag_mst tag
78
+
79
+ on str.groupstore_id=tag.groupstore_id or str.store_id=tag.store_id
80
+
81
+ where tag.display_flg = 1
82
+
83
+ and str.store_id=/*storeId*/101
84
+
85
+ group by str.store_id
86
+
87
+ having count(*)>5
88
+
89
+ ```

12

修正

2018/06/28 02:15

投稿

sazi
sazi

スコア25173

test CHANGED
@@ -44,7 +44,7 @@
44
44
 
45
45
 
46
46
 
47
- 店舗マスタと商品分類マスタを結合(or条件がミソ)し、店舗別の共通分類を含めた一覧としてそれを集計する
47
+ 店舗マスタと商品分類マスタを結合(or条件がミソ)し、店舗別の共通分類を含めた一覧としてそれを集計する
48
48
 
49
49
  ```SQL
50
50
 

11

全部修正

2018/06/28 02:04

投稿

sazi
sazi

スコア25173

test CHANGED
@@ -1,107 +1,69 @@
1
+ 質問のSQLを分析します
2
+
1
3
  ```SQL
2
4
 
3
- select store_id from tag_mst
5
+ SELECT T5.store_id
4
6
 
5
- where display_flg = 1
7
+ FROM (
6
8
 
7
- and store_id in (
9
+ SELECT T1.store_id
8
10
 
9
- select store_id from store_mst
11
+ , (
10
12
 
11
- where groupstore_id in (
13
+ SELECT COUNT(*) FROM tag_mst T2
12
14
 
13
- select groupstore_id from store_mst
15
+ WHERE T2.display_flg = 1 AND T2.store_id=T1.store_id GROUP BY T2.store_id
14
16
 
15
- where store_id=/*storeId*/101
17
+ ) + (
16
18
 
19
+ SELECT COUNT(*) FROM tag_mst T3
20
+
21
+ WHERE T3.display_flg = 1 AND T3.groupstore_id=(
22
+
23
+ SELECT T4.groupstore_id FROM store_mst T4 WHERE T4.store_id = T1.store_id
24
+
17
- )
25
+ )
26
+
27
+ )
28
+
29
+ AS cnt
30
+
31
+ FROM tag_mst T1
32
+
33
+ WHERE store_id = /*storeId*/101
34
+
35
+ GROUP BY T1.store_id
36
+
37
+ ) T5
38
+
39
+ WHERE cnt > 5
40
+
41
+ ```
42
+
43
+ 指定した店舗の件数が>5の結果しか返却しないので、これでは要件満たせていませんね。
44
+
45
+
46
+
47
+ 店舗マスタと商品分類マスタを結合(or条件がミソ)して、店舗別の共通分類を含めた一覧としてそれを集計する
48
+
49
+ ```SQL
50
+
51
+ select str.store_id
52
+
53
+ from store_mst str left join tag_mst tag
54
+
55
+ on str.groupstore_id=tag.groupstore_id or str.store_id=tag.store_id
56
+
57
+ where tag.display_flg = 1
58
+
59
+ and str.groupstore_id in (
60
+
61
+ select groupstore_id from store_mst where store_id=/*storeId*/101
18
62
 
19
63
  )
20
64
 
21
- group by store_id
65
+ group by str.store_id
22
66
 
23
67
  having count(*)>5
24
68
 
25
69
  ```
26
-
27
-
28
-
29
- 以下NG(データ仕様に合っていない)
30
-
31
- --
32
-
33
- #パターン1
34
-
35
- ```SQL
36
-
37
- select store_id from tag_mst
38
-
39
- where display_flg = 1
40
-
41
- and groupstore_id = (
42
-
43
- --ログインユーザーが所属する店舗のgroupstore_id
44
-
45
- select groupstore_id from store_mst
46
-
47
- where store_id=/*storeId*/5100
48
-
49
- )
50
-
51
- -- 店舗毎で商品件数が5件を超える
52
-
53
- group by store_id
54
-
55
- having count(*)>5
56
-
57
- ```
58
-
59
- #パターン2
60
-
61
- ```SQL
62
-
63
- select store_id from tag_mst t1
64
-
65
- where display_flg = 1
66
-
67
- and exists (
68
-
69
- select 1 from store_mst
70
-
71
- where store_id=/*storeId*/5100
72
-
73
- and groupstore_id=t1.groupstore_id
74
-
75
- )
76
-
77
- -- 店舗毎で商品件数が5件を超える
78
-
79
- group by store_id
80
-
81
- having count(*)>5
82
-
83
- ```
84
-
85
- #パターン3
86
-
87
- ```SQL
88
-
89
- select store_id from tag_mst
90
-
91
- where display_flg = 1
92
-
93
- and groupstore_id in (
94
-
95
- select groupstore_id from tag_mst
96
-
97
- where store_id=/*storeId*/5100
98
-
99
- )
100
-
101
- group by store_id
102
-
103
- having count(*)>5
104
-
105
- ```
106
-
107
- ※件数的にはtag_mst > store_mstだと思うので、パターン3以外の方が高速だと思われる。

10

修正

2018/06/28 02:02

投稿

sazi
sazi

スコア25173

test CHANGED
@@ -1,12 +1,14 @@
1
1
  ```SQL
2
2
 
3
- select count(*) from tag_mst
3
+ select store_id from tag_mst
4
4
 
5
5
  where display_flg = 1
6
6
 
7
- and (store_id=/*storeId*/101
7
+ and store_id in (
8
8
 
9
+ select store_id from store_mst
10
+
9
- or groupstore_id in (
11
+ where groupstore_id in (
10
12
 
11
13
  select groupstore_id from store_mst
12
14
 
@@ -15,6 +17,10 @@
15
17
  )
16
18
 
17
19
  )
20
+
21
+ group by store_id
22
+
23
+ having count(*)>5
18
24
 
19
25
  ```
20
26
 

9

修正

2018/06/27 17:35

投稿

sazi
sazi

スコア25173

test CHANGED
@@ -6,7 +6,7 @@
6
6
 
7
7
  and (store_id=/*storeId*/101
8
8
 
9
- or groupstore_id =(
9
+ or groupstore_id in (
10
10
 
11
11
  select groupstore_id from store_mst
12
12
 

8

修正

2018/06/27 17:07

投稿

sazi
sazi

スコア25173

test CHANGED
@@ -1,26 +1,20 @@
1
1
  ```SQL
2
2
 
3
- select store_id from tag_mst
3
+ select count(*) from tag_mst
4
4
 
5
5
  where display_flg = 1
6
6
 
7
- and store_id in (
7
+ and (store_id=/*storeId*/101
8
8
 
9
- select store_id from store_mst
10
-
11
- where groupstore_id = (
9
+ or groupstore_id =(
12
10
 
13
11
  select groupstore_id from store_mst
14
12
 
15
- where store_id=/*storeId*/5100
13
+ where store_id=/*storeId*/101
16
14
 
17
- )
15
+ )
18
16
 
19
17
  )
20
-
21
- group by store_id
22
-
23
- having count(*)>5
24
18
 
25
19
  ```
26
20
 

7

追記

2018/06/27 16:55

投稿

sazi
sazi

スコア25173

test CHANGED
@@ -1,3 +1,35 @@
1
+ ```SQL
2
+
3
+ select store_id from tag_mst
4
+
5
+ where display_flg = 1
6
+
7
+ and store_id in (
8
+
9
+ select store_id from store_mst
10
+
11
+ where groupstore_id = (
12
+
13
+ select groupstore_id from store_mst
14
+
15
+ where store_id=/*storeId*/5100
16
+
17
+ )
18
+
19
+ )
20
+
21
+ group by store_id
22
+
23
+ having count(*)>5
24
+
25
+ ```
26
+
27
+
28
+
29
+ 以下NG(データ仕様に合っていない)
30
+
31
+ --
32
+
1
33
  #パターン1
2
34
 
3
35
  ```SQL

6

修正

2018/06/27 16:42

投稿

sazi
sazi

スコア25173

test CHANGED
@@ -68,7 +68,7 @@
68
68
 
69
69
  group by store_id
70
70
 
71
- having count(*)>=5
71
+ having count(*)>5
72
72
 
73
73
  ```
74
74
 

5

パターン追記

2018/06/27 16:11

投稿

sazi
sazi

スコア25173

test CHANGED
@@ -1,3 +1,5 @@
1
+ #パターン1
2
+
1
3
  ```SQL
2
4
 
3
5
  select store_id from tag_mst
@@ -22,7 +24,33 @@
22
24
 
23
25
  ```
24
26
 
27
+ #パターン2
28
+
29
+ ```SQL
30
+
31
+ select store_id from tag_mst t1
32
+
33
+ where display_flg = 1
34
+
35
+ and exists (
36
+
37
+ select 1 from store_mst
38
+
39
+ where store_id=/*storeId*/5100
40
+
41
+ and groupstore_id=t1.groupstore_id
42
+
43
+ )
44
+
45
+ -- 店舗毎で商品件数が5件を超える
46
+
47
+ group by store_id
48
+
49
+ having count(*)>5
50
+
25
- または、
51
+ ```
52
+
53
+ #パターン3
26
54
 
27
55
  ```SQL
28
56
 
@@ -44,4 +72,4 @@
44
72
 
45
73
  ```
46
74
 
47
- ※件数的にはtag_mst > store_mstだと思うので、前者の方が高速だと思われる。
75
+ ※件数的にはtag_mst > store_mstだと思うので、パターン3以外の方が高速だと思われる。

4

修正

2018/06/27 15:53

投稿

sazi
sazi

スコア25173

test CHANGED
@@ -30,7 +30,7 @@
30
30
 
31
31
  where display_flg = 1
32
32
 
33
- and groupstore_id = (
33
+ and groupstore_id in (
34
34
 
35
35
  select groupstore_id from tag_mst
36
36
 

3

追記

2018/06/27 15:49

投稿

sazi
sazi

スコア25173

test CHANGED
@@ -10,7 +10,7 @@
10
10
 
11
11
  select groupstore_id from store_mst
12
12
 
13
- where stm.store_id=/*storeId*/5100
13
+ where store_id=/*storeId*/5100
14
14
 
15
15
  )
16
16
 
@@ -21,3 +21,27 @@
21
21
  having count(*)>5
22
22
 
23
23
  ```
24
+
25
+ または、
26
+
27
+ ```SQL
28
+
29
+ select store_id from tag_mst
30
+
31
+ where display_flg = 1
32
+
33
+ and groupstore_id = (
34
+
35
+ select groupstore_id from tag_mst
36
+
37
+ where store_id=/*storeId*/5100
38
+
39
+ )
40
+
41
+ group by store_id
42
+
43
+ having count(*)>=5
44
+
45
+ ```
46
+
47
+ ※件数的にはtag_mst > store_mstだと思うので、前者の方が高速だと思われる。

2

修正

2018/06/27 15:47

投稿

sazi
sazi

スコア25173

test CHANGED
@@ -4,21 +4,13 @@
4
4
 
5
5
  where display_flg = 1
6
6
 
7
- and store_id in (
7
+ and groupstore_id = (
8
8
 
9
- --ログインユーザーが所属する店舗のチェーン店一覧
9
+ --ログインユーザーが所属する店舗のgroupstore_id
10
10
 
11
- select store_id from store_mst
11
+ select groupstore_id from store_mst
12
12
 
13
- where groupstore_id = (
14
-
15
- --ログインユーザーが所属する店舗のgroupstore_id
16
-
17
- select groupstore_id from store_mst
18
-
19
- where stm.store_id=/*storeId*/5100
13
+ where stm.store_id=/*storeId*/5100
20
-
21
- )
22
14
 
23
15
  )
24
16
 

1

コメント追記

2018/06/27 15:43

投稿

sazi
sazi

スコア25173

test CHANGED
@@ -22,8 +22,10 @@
22
22
 
23
23
  )
24
24
 
25
+ -- 店舗毎で商品件数が5件を超える
26
+
25
27
  group by store_id
26
28
 
27
- having count(*)>=5
29
+ having count(*)>5
28
30
 
29
31
  ```