wdate-year-month-week-gategory-amount-coin

 

 

 

---2016-12-02 19:46:39

the whole table

  DISTINCT field

  SUM(field)

  COUNT(field)

 

 

--- 888983 rows OK

 

 

  1 SELECT
  2 *
  3 FROM
  4     (
  5         SELECT
  6             DATE_FORMAT(date, '%Y') AS history_year,
  7             DATE_FORMAT(date, '%m') AS history_month,
  8             SUM(coin) AS month_category_coin,
  9             SUM(amount) AS month_category_amount,
 10             country AS fk_country,
 11             category AS fk_category
 12         FROM
 13             study_test
 14         WHERE
 15             DATE_FORMAT(date, '%Y') = 2016
 16         GROUP BY
 17             fk_country,
 18             fk_category,
 19             history_year,
 20             history_month
 21     ) AS wt_month_sum
 22 LEFT JOIN (
 23     SELECT
 24         tmp.history_year,
 25         tmp.history_month,
 26         tmp.fk_country,
 27         tmp.fk_category,
 28         COUNT(fk_asin) AS month_category_diff_asins
 29     FROM
 30         (
 31             SELECT
 32                 DATE_FORMAT(date, '%Y') AS history_year,
 33                 DATE_FORMAT(date, '%m') AS history_month,
 34                 country AS fk_country,
 35                 category AS fk_category,
 36                 asin AS fk_asin
 37             FROM
 38                 study_test
 39             WHERE
 40                 DATE_FORMAT(date, '%Y') = 2016
 41             GROUP BY
 42                 fk_country,
 43                 fk_category,
 44                 fk_asin,
 45                 history_year,
 46                 history_month
 47         ) AS tmp
 48     GROUP BY
 49         fk_country,
 50         fk_category,
 51         history_year,
 52         history_month
 53 ) AS wt_month_diff_asins ON wt_month_sum.fk_country = wt_month_diff_asins.fk_country
 54 AND wt_month_sum.fk_category = wt_month_diff_asins.fk_category
 55 AND wt_month_sum.history_year = wt_month_diff_asins.history_year
 56 AND wt_month_sum.history_month = wt_month_diff_asins.history_month
 57 LEFT JOIN (
 58     SELECT
 59         tmp.history_year,
 60         tmp.history_month,
 61         tmp.fk_country,
 62         tmp.fk_category,
 63         COUNT(fk_diff_days) AS month_category_diff_days
 64     FROM
 65         (
 66             SELECT
 67                 DATE_FORMAT(date, '%Y') AS history_year,
 68                 DATE_FORMAT(date, '%m') AS history_month,
 69                 country AS fk_country,
 70                 category AS fk_category,
 71                 date AS fk_diff_days
 72             FROM
 73                 study_test
 74             WHERE
 75                 DATE_FORMAT(date, '%Y') = 2016
 76             GROUP BY
 77                 fk_country,
 78                 fk_category,
 79                 fk_diff_days,
 80                 history_year,
 81                 history_month
 82         ) AS tmp
 83     GROUP BY
 84         fk_country,
 85         fk_category,
 86         history_year,
 87         history_month
 88 ) AS wt_month_diff_days ON wt_month_sum.fk_country = wt_month_diff_days.fk_country
 89 AND wt_month_sum.fk_category = wt_month_diff_days.fk_category
 90 AND wt_month_sum.history_year = wt_month_diff_days.history_year
 91 AND wt_month_sum.history_month = wt_month_diff_days.history_month
 92 LEFT JOIN (
 93     SELECT
 94         DATE_FORMAT(date, '%Y') AS history_year,
 95         DATE_FORMAT(date, '%m') AS history_month,
 96         DATE_FORMAT(date, '%V') AS history_week,
 97         SUM(coin) AS week_category_coin,
 98         SUM(amount) AS week_category_amount,
 99         country AS fk_country,
100         category AS fk_category
101     FROM
102         study_test
103     WHERE
104         DATE_FORMAT(date, '%Y') = 2016
105     GROUP BY
106         fk_country,
107         fk_category,
108         history_year,
109         history_month,
110         history_week
111 ) AS wt_week_sum ON wt_month_sum.fk_country = wt_week_sum.fk_country
112 AND wt_month_sum.fk_category = wt_week_sum.fk_category
113 AND wt_month_sum.history_year = wt_week_sum.history_year
114 AND wt_month_sum.history_month = wt_week_sum.history_month
115 LEFT JOIN (
116     SELECT
117         tmp.history_year,
118         tmp.history_month,
119         tmp.history_week,
120         tmp.fk_country,
121         tmp.fk_category,
122         COUNT(fk_diff_asin) AS week_category_diff_asins
123     FROM
124         (
125             SELECT
126                 DATE_FORMAT(date, '%Y') AS history_year,
127                 DATE_FORMAT(date, '%m') AS history_month,
128                 DATE_FORMAT(date, '%V') AS history_week,
129                 country AS fk_country,
130                 category AS fk_category,
131                 asin AS fk_diff_asin
132             FROM
133                 study_test
134             WHERE
135                 DATE_FORMAT(date, '%Y') = 2016
136             GROUP BY
137                 fk_country,
138                 fk_category,
139                 fk_diff_asin,
140                 history_year,
141                 history_month,
142                 history_week
143         ) AS tmp
144     GROUP BY
145         fk_country,
146         fk_category,
147         history_year,
148         history_month,
149         history_week
150 ) AS wt_week_diff_asins ON wt_week_sum.fk_country = wt_week_diff_asins.fk_country
151 AND wt_week_sum.fk_category = wt_week_diff_asins.fk_category
152 AND wt_week_sum.history_year = wt_week_diff_asins.history_year
153 AND wt_week_sum.history_month = wt_week_diff_asins.history_month
154 AND wt_week_sum.history_week = wt_week_diff_asins.history_week
155 LEFT JOIN (
156     SELECT
157         tmp.history_year,
158         tmp.history_month,
159         tmp.history_week,
160         tmp.fk_country,
161         tmp.fk_category,
162         COUNT(fk_diff_days) AS week_category_diff_days
163     FROM
164         (
165             SELECT
166                 DATE_FORMAT(date, '%Y') AS history_year,
167                 DATE_FORMAT(date, '%m') AS history_month,
168                 DATE_FORMAT(date, '%V') AS history_week,
169                 country AS fk_country,
170                 category AS fk_category,
171                 date AS fk_diff_days
172             FROM
173                 study_test
174             WHERE
175                 DATE_FORMAT(date, '%Y') = 2016
176             GROUP BY
177                 fk_country,
178                 fk_category,
179                 fk_diff_days,
180                 history_year,
181                 history_month,
182                 history_week
183         ) AS tmp
184     GROUP BY
185         fk_country,
186         fk_category,
187         history_year,
188         history_month,
189         history_week
190 ) AS wt_week_diff_days ON wt_week_sum.fk_country = wt_week_diff_days.fk_country
191 AND wt_week_sum.fk_category = wt_week_diff_days.fk_category
192 AND wt_week_sum.history_year = wt_week_diff_days.history_year
193 AND wt_week_sum.history_month = wt_week_diff_days.history_month
194 AND wt_week_sum.history_week = wt_week_diff_days.history_week

 

 

 

1 SELECT COUNT(DISTINCT date),COUNT(DISTINCT asin), SUM(coin),SUM(amount),SUM(coin)/SUM(amount) FROM asinsaleranks WHERE category=9 AND  LEFT(date, 6) = '201608'
2 SELECT COUNT(DISTINCT date),COUNT(DISTINCT asin), SUM(coin),SUM(amount),SUM(coin)/SUM(amount) FROM asinsaleranks WHERE category=9 AND  LEFT(date, 6) = '201609'
3 
4 
5 SELECT COUNT(DISTINCT date),COUNT(DISTINCT asin), SUM(coin),SUM(amount),SUM(coin)/SUM(amount) FROM asinsaleranks WHERE category=6 AND date= '20160731'
6 SELECT COUNT(DISTINCT date),COUNT(DISTINCT asin), SUM(coin),SUM(amount),SUM(coin)/SUM(amount) FROM asinsaleranks WHERE category=6 AND date> '20160731' AND date< '20160807'
7 SELECT COUNT(DISTINCT date),COUNT(DISTINCT asin), SUM(coin),SUM(amount),SUM(coin)/SUM(amount) FROM asinsaleranks WHERE category=6 AND date> '20160806' AND date< '20160814'

 

 

  1 SELECT
  2 *
  3 FROM
  4     (
  5         SELECT
  6             DATE_FORMAT(date, '%Y') AS history_year,
  7             DATE_FORMAT(date, '%m') AS history_month,
  8             SUM(coin) AS month_category_coin,
  9             SUM(amount) AS month_category_amount,
 10             country AS fk_country,
 11             category AS fk_category
 12         FROM
 13             study_test
 14         WHERE
 15             DATE_FORMAT(date, '%Y') = 2016
 16         GROUP BY
 17             fk_country,
 18             fk_category,
 19             history_year,
 20             history_month
 21     ) AS wt_month_sum
 22 LEFT JOIN (
 23     SELECT
 24         tmp.history_year,
 25         tmp.history_month,
 26         tmp.fk_country,
 27         tmp.fk_category,
 28         COUNT(fk_asin) AS month_category_diff_asins
 29     FROM
 30         (
 31             SELECT
 32                 DATE_FORMAT(date, '%Y') AS history_year,
 33                 DATE_FORMAT(date, '%m') AS history_month,
 34                 country AS fk_country,
 35                 category AS fk_category,
 36                 asin AS fk_asin
 37             FROM
 38                 study_test
 39             WHERE
 40                 DATE_FORMAT(date, '%Y') = 2016
 41             GROUP BY
 42                 fk_country,
 43                 fk_category,
 44                 fk_asin,
 45                 history_year,
 46                 history_month
 47         ) AS tmp
 48     GROUP BY
 49         fk_country,
 50         fk_category,
 51         history_year,
 52         history_month
 53 ) AS wt_month_diff_asins ON wt_month_sum.fk_country = wt_month_diff_asins.fk_country
 54 AND wt_month_sum.fk_category = wt_month_diff_asins.fk_category
 55 AND wt_month_sum.history_year = wt_month_diff_asins.history_year
 56 AND wt_month_sum.history_month = wt_month_diff_asins.history_month
 57 LEFT JOIN (
 58     SELECT
 59         tmp.history_year,
 60         tmp.history_month,
 61         tmp.fk_country,
 62         tmp.fk_category,
 63         COUNT(fk_diff_days) AS month_category_diff_days
 64     FROM
 65         (
 66             SELECT
 67                 DATE_FORMAT(date, '%Y') AS history_year,
 68                 DATE_FORMAT(date, '%m') AS history_month,
 69                 country AS fk_country,
 70                 category AS fk_category,
 71                 date AS fk_diff_days
 72             FROM
 73                 study_test
 74             WHERE
 75                 DATE_FORMAT(date, '%Y') = 2016
 76             GROUP BY
 77                 fk_country,
 78                 fk_category,
 79                 fk_diff_days,
 80                 history_year,
 81                 history_month
 82         ) AS tmp
 83     GROUP BY
 84         fk_country,
 85         fk_category,
 86         history_year,
 87         history_month
 88 ) AS wt_month_diff_days ON wt_month_sum.fk_country = wt_month_diff_days.fk_country
 89 AND wt_month_sum.fk_category = wt_month_diff_days.fk_category
 90 AND wt_month_sum.history_year = wt_month_diff_days.history_year
 91 AND wt_month_sum.history_month = wt_month_diff_days.history_month
 92 LEFT JOIN (
 93     SELECT
 94         DATE_FORMAT(date, '%Y') AS history_year,
 95         DATE_FORMAT(date, '%m') AS history_month,
 96         DATE_FORMAT(date, '%V') AS history_week,
 97         SUM(coin) AS week_category_coin,
 98         SUM(amount) AS week_category_amount,
 99         country AS fk_country,
100         category AS fk_category
101     FROM
102         study_test
103     WHERE
104         DATE_FORMAT(date, '%Y') = 2016
105     GROUP BY
106         fk_country,
107         fk_category,
108         history_year,
109         history_month,
110         history_week
111 ) AS wt_week_sum ON wt_month_sum.fk_country = wt_week_sum.fk_country
112 AND wt_month_sum.fk_category = wt_week_sum.fk_category
113 AND wt_month_sum.history_year = wt_week_sum.history_year
114 AND wt_month_sum.history_month = wt_week_sum.history_month
115 LEFT JOIN (
116     SELECT
117         tmp.history_year,
118         tmp.history_month,
119         tmp.history_week,
120         tmp.fk_country,
121         tmp.fk_category,
122         COUNT(fk_diff_asin) AS week_category_diff_asins
123     FROM
124         (
125             SELECT
126                 DATE_FORMAT(date, '%Y') AS history_year,
127                 DATE_FORMAT(date, '%m') AS history_month,
128                 DATE_FORMAT(date, '%V') AS history_week,
129                 country AS fk_country,
130                 category AS fk_category,
131                 asin AS fk_diff_asin
132             FROM
133                 study_test
134             WHERE
135                 DATE_FORMAT(date, '%Y') = 2016
136             GROUP BY
137                 fk_country,
138                 fk_category,
139                 fk_diff_asin,
140                 history_year,
141                 history_month,
142                 history_week
143         ) AS tmp
144     GROUP BY
145         fk_country,
146         fk_category,
147         history_year,
148         history_month,
149         history_week
150 ) AS wt_week_diff_asins ON wt_week_sum.fk_country = wt_week_diff_asins.fk_country
151 AND wt_week_sum.fk_category = wt_week_diff_asins.fk_category
152 AND wt_week_sum.history_year = wt_week_diff_asins.history_year
153 AND wt_week_sum.history_month = wt_week_diff_asins.history_month
154 AND wt_week_sum.history_week = wt_week_diff_asins.history_week
155 LEFT JOIN (
156     SELECT
157         tmp.history_year,
158         tmp.history_month,
159         tmp.history_week,
160         tmp.fk_country,
161         tmp.fk_category,
162         COUNT(fk_diff_days) AS week_category_diff_days
163     FROM
164         (
165             SELECT
166                 DATE_FORMAT(date, '%Y') AS history_year,
167                 DATE_FORMAT(date, '%m') AS history_month,
168                 DATE_FORMAT(date, '%V') AS history_week,
169                 country AS fk_country,
170                 category AS fk_category,
171                 date AS fk_diff_days
172             FROM
173                 study_test
174             WHERE
175                 DATE_FORMAT(date, '%Y') = 2016
176             GROUP BY
177                 fk_country,
178                 fk_category,
179                 fk_diff_days,
180                 history_year,
181                 history_month,
182                 history_week
183         ) AS tmp
184     GROUP BY
185         fk_country,
186         fk_category,
187         history_year,
188         history_month,
189         history_week
190 ) AS wt_week_diff_days ON wt_week_sum.fk_country = wt_week_diff_days.fk_country
191 AND wt_week_sum.fk_category = wt_week_diff_days.fk_category
192 AND wt_week_sum.history_year = wt_week_diff_days.history_year
193 AND wt_week_sum.history_month = wt_week_diff_days.history_month
194 AND wt_week_sum.history_week = wt_week_diff_days.history_week

 

 

 

 1 DROP TABLE IF EXISTS `study_test`;
 2 CREATE TABLE `study_test` (
 3   `country` char(2) COLLATE utf8_bin NOT NULL,
 4   `date` char(8) COLLATE utf8_bin NOT NULL DEFAULT '20161130',
 5   `asin` char(10) COLLATE utf8_bin NOT NULL,
 6   `category` int(10) unsigned NOT NULL DEFAULT '0',
 7   `coin` decimal(16,4) unsigned NOT NULL DEFAULT '0.0000',
 8   `amount` decimal(16,4) unsigned NOT NULL DEFAULT '0.0000',
 9   PRIMARY KEY (`country`,`date`,`asin`),
10   KEY `k_asin` (`asin`) USING BTREE,
11   KEY `k_coin` (`coin`) USING BTREE,
12   KEY `k_amount` (`amount`) USING BTREE
13 ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

 

posted @ 2016-11-30 19:48  papering  阅读(173)  评论(0编辑  收藏  举报