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

回答編集履歴

2

回答を追記

2016/04/06 15:30

投稿

KiyoshiMotoki
KiyoshiMotoki

スコア4791

answer CHANGED
@@ -77,4 +77,113 @@
77
77
  WHERE sub.data IS NULL
78
78
 
79
79
  ) AS l ON h.shop_id = l.shop_id AND h.head = l.head;
80
- ```
80
+ ```
81
+
82
+ ---
83
+
84
+ いろいろ想像で補った部分がありますが、《結果01》を経由して《結果02》を得るためのSQLは、以下のようになります。
85
+ ```sql
86
+ # ④
87
+ SELECT
88
+ h.jan,
89
+ h.head AS price_type,
90
+ h.product_name_id AS max_product_name_id,
91
+
92
+ # ⑤
93
+ CASE l.product_name_id
94
+ WHEN h.product_name_id THEN 0
95
+ ELSE l.product_name_id
96
+ END AS min_product_name_id,
97
+
98
+ h.data AS price_max,
99
+
100
+ # ⑤
101
+ CASE l.data
102
+ WHEN h.data THEN 0
103
+ ELSE l.data
104
+ END AS price_min,
105
+
106
+ (h.data - l.data) AS price_diff
107
+ FROM (
108
+
109
+ # ②
110
+ SELECT main.*
111
+ FROM (
112
+
113
+ # ①
114
+ SELECT t1.*
115
+ FROM product_jans AS t1
116
+ LEFT JOIN product_jans AS t2 ON
117
+ t1.shop_id = t2.shop_id
118
+ AND t1.jan = t2.jan
119
+ AND t1.head = t2.head
120
+ AND t1.modified < t2.modified
121
+ WHERE t2.modified IS NULL
122
+
123
+ ) AS main
124
+ LEFT OUTER JOIN (
125
+
126
+ # ①
127
+ SELECT t1.*
128
+ FROM product_jans AS t1
129
+ LEFT JOIN product_jans AS t2 ON
130
+ t1.shop_id = t2.shop_id
131
+ AND t1.jan = t2.jan
132
+ AND t1.head = t2.head
133
+ AND t1.modified < t2.modified
134
+ WHERE t2.modified IS NULL
135
+
136
+ ) AS sub ON
137
+ main.jan = sub.jan
138
+ AND main.head = sub.head
139
+ AND main.data < sub.data
140
+ WHERE sub.data IS NULL
141
+
142
+ ) AS h
143
+ LEFT OUTER JOIN (
144
+
145
+ # ③
146
+ SELECT main.*
147
+ FROM (
148
+
149
+ # ①
150
+ SELECT t1.*
151
+ FROM product_jans AS t1
152
+ LEFT JOIN product_jans AS t2 ON
153
+ t1.shop_id = t2.shop_id
154
+ AND t1.jan = t2.jan
155
+ AND t1.head = t2.head
156
+ AND t1.modified < t2.modified
157
+ WHERE t2.modified IS NULL
158
+
159
+ ) AS main
160
+ LEFT OUTER JOIN (
161
+
162
+ # ①
163
+ SELECT t1.*
164
+ FROM product_jans AS t1
165
+ LEFT JOIN product_jans AS t2 ON
166
+ t1.shop_id = t2.shop_id
167
+ AND t1.jan = t2.jan
168
+ AND t1.head = t2.head
169
+ AND t1.modified < t2.modified
170
+ WHERE t2.modified IS NULL
171
+
172
+ ) AS sub ON
173
+ main.jan = sub.jan
174
+ AND main.head = sub.head
175
+ AND main.data > sub.data
176
+ WHERE sub.data IS NULL
177
+
178
+ ) AS l ON h.jan = l.jan AND h.head = l.head;
179
+ ```
180
+
181
+ 以下の条件で、データを抽出・整形しています。
182
+ 斜体字の部分が、私が想像で補った箇所です。
183
+ ① __jan、shop_id__、"新品・中古・コレクター"価格の組み合わせごとに`modified`が最新の行を抽出
184
+ ② ①の中から、__jan__、"新品・中古・コレクター"価格の組み合わせごとに最高値(`data`が最大)の行を抽出
185
+ ③ ①の中から、__jan__、"新品・中古・コレクター"価格の組み合わせごとに最安値(`data`が最小)の行を抽出
186
+ ④ ②、③を、__jan__、"新品・中古・コレクター"価格の組み合わせごとに1行に結合し、最高値と最安値、さらにそのそれぞれの商品名コード、価格差を取得
187
+ ⑤ その際、最高値と最安値が一致している行は最安値の商品名コードと価格を"0"と表示
188
+
189
+ なお、どこから出てきたか不明なため、《結果02》の`id`カラムは無視しています。

1

未完成の回答を追記

2016/04/06 15:30

投稿

KiyoshiMotoki
KiyoshiMotoki

スコア4791

answer CHANGED
@@ -17,4 +17,64 @@
17
17
  例えば`product_name_id` = 20493 は、新品価格・中古価格・コレクター価格の`modified`が
18
18
  いずれも"2016-04-05 07:38:02"になっていますが、
19
19
  これが1秒でもズレることはないという認識で問題ないか、という意味です。
20
- この前提が誤っていると、そもそも《結果01》を取得するSQLから見直す必要があるかもしれません。
20
+ この前提が誤っていると、そもそも《結果01》を取得するSQLから見直す必要があるかもしれません。
21
+
22
+ ---
23
+ ※まだ未完成です。うまく動きません。
24
+ ```sql
25
+ SELECT
26
+ h.jan,
27
+ h.product_name_id AS max_product_name_id,
28
+ l.product_name_id AS min_product_name_id,
29
+ h.data AS price_max,
30
+ l.data AS price_min,
31
+ (h.data - l.data) AS price_diff
32
+ FROM (
33
+
34
+ # ②
35
+ SELECT main.*
36
+ FROM product_jans AS main
37
+ LEFT OUTER JOIN (
38
+
39
+ # ①
40
+ SELECT t1.*
41
+ FROM product_jans AS t1
42
+ LEFT JOIN product_jans AS t2 ON
43
+ t1.shop_id = t2.shop_id
44
+ AND t1.product_name_id = t2.product_name_id
45
+ AND t1.head = t2.head
46
+ AND t1.modified < t2.modified
47
+ WHERE t2.modified IS NULL
48
+
49
+ ) AS sub ON
50
+ main.shop_id = sub.shop_id
51
+ AND main.head = sub.head
52
+ AND main.data < sub.data
53
+ WHERE sub.data IS NULL
54
+
55
+ ) AS h
56
+ LEFT OUTER JOIN (
57
+
58
+ # ③
59
+ SELECT main.*
60
+ FROM product_jans AS main
61
+ LEFT OUTER JOIN (
62
+
63
+ # ①
64
+ SELECT t1.product_name_id, t1.jan, t1.head, t1.data, t1.shop_id
65
+ FROM product_jans AS t1
66
+ LEFT JOIN product_jans AS t2 ON
67
+ t1.shop_id = t2.shop_id
68
+ AND t1.product_name_id = t2.product_name_id
69
+ AND t1.head = t2.head
70
+ AND t1.modified < t2.modified
71
+ WHERE t2.modified IS NULL
72
+
73
+ ) AS sub ON
74
+ main.shop_id = sub.shop_id
75
+ AND main.head = sub.head
76
+ AND main.data > sub.data
77
+ WHERE sub.data IS NULL
78
+
79
+ ) AS l ON h.shop_id = l.shop_id AND h.head = l.head;
80
+ ```