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

the whole table

  DISTINCT field

  SUM(field)

  COUNT(field)

--- 888983 rows OK

 SELECT
*
FROM
(
SELECT
DATE_FORMAT(date, '%Y') AS history_year,
DATE_FORMAT(date, '%m') AS history_month,
SUM(coin) AS month_category_coin,
SUM(amount) AS month_category_amount,
country AS fk_country,
category AS fk_category
FROM
study_test
WHERE
DATE_FORMAT(date, '%Y') = 2016
GROUP BY
fk_country,
fk_category,
history_year,
history_month
) AS wt_month_sum
LEFT JOIN (
SELECT
tmp.history_year,
tmp.history_month,
tmp.fk_country,
tmp.fk_category,
COUNT(fk_asin) AS month_category_diff_asins
FROM
(
SELECT
DATE_FORMAT(date, '%Y') AS history_year,
DATE_FORMAT(date, '%m') AS history_month,
country AS fk_country,
category AS fk_category,
asin AS fk_asin
FROM
study_test
WHERE
DATE_FORMAT(date, '%Y') = 2016
GROUP BY
fk_country,
fk_category,
fk_asin,
history_year,
history_month
) AS tmp
GROUP BY
fk_country,
fk_category,
history_year,
history_month
) AS wt_month_diff_asins ON wt_month_sum.fk_country = wt_month_diff_asins.fk_country
AND wt_month_sum.fk_category = wt_month_diff_asins.fk_category
AND wt_month_sum.history_year = wt_month_diff_asins.history_year
AND wt_month_sum.history_month = wt_month_diff_asins.history_month
LEFT JOIN (
SELECT
tmp.history_year,
tmp.history_month,
tmp.fk_country,
tmp.fk_category,
COUNT(fk_diff_days) AS month_category_diff_days
FROM
(
SELECT
DATE_FORMAT(date, '%Y') AS history_year,
DATE_FORMAT(date, '%m') AS history_month,
country AS fk_country,
category AS fk_category,
date AS fk_diff_days
FROM
study_test
WHERE
DATE_FORMAT(date, '%Y') = 2016
GROUP BY
fk_country,
fk_category,
fk_diff_days,
history_year,
history_month
) AS tmp
GROUP BY
fk_country,
fk_category,
history_year,
history_month
) AS wt_month_diff_days ON wt_month_sum.fk_country = wt_month_diff_days.fk_country
AND wt_month_sum.fk_category = wt_month_diff_days.fk_category
AND wt_month_sum.history_year = wt_month_diff_days.history_year
AND wt_month_sum.history_month = wt_month_diff_days.history_month
LEFT JOIN (
SELECT
DATE_FORMAT(date, '%Y') AS history_year,
DATE_FORMAT(date, '%m') AS history_month,
DATE_FORMAT(date, '%V') AS history_week,
SUM(coin) AS week_category_coin,
SUM(amount) AS week_category_amount,
country AS fk_country,
category AS fk_category
FROM
study_test
WHERE
DATE_FORMAT(date, '%Y') = 2016
GROUP BY
fk_country,
fk_category,
history_year,
history_month,
history_week
) AS wt_week_sum ON wt_month_sum.fk_country = wt_week_sum.fk_country
AND wt_month_sum.fk_category = wt_week_sum.fk_category
AND wt_month_sum.history_year = wt_week_sum.history_year
AND wt_month_sum.history_month = wt_week_sum.history_month
LEFT JOIN (
SELECT
tmp.history_year,
tmp.history_month,
tmp.history_week,
tmp.fk_country,
tmp.fk_category,
COUNT(fk_diff_asin) AS week_category_diff_asins
FROM
(
SELECT
DATE_FORMAT(date, '%Y') AS history_year,
DATE_FORMAT(date, '%m') AS history_month,
DATE_FORMAT(date, '%V') AS history_week,
country AS fk_country,
category AS fk_category,
asin AS fk_diff_asin
FROM
study_test
WHERE
DATE_FORMAT(date, '%Y') = 2016
GROUP BY
fk_country,
fk_category,
fk_diff_asin,
history_year,
history_month,
history_week
) AS tmp
GROUP BY
fk_country,
fk_category,
history_year,
history_month,
history_week
) AS wt_week_diff_asins ON wt_week_sum.fk_country = wt_week_diff_asins.fk_country
AND wt_week_sum.fk_category = wt_week_diff_asins.fk_category
AND wt_week_sum.history_year = wt_week_diff_asins.history_year
AND wt_week_sum.history_month = wt_week_diff_asins.history_month
AND wt_week_sum.history_week = wt_week_diff_asins.history_week
LEFT JOIN (
SELECT
tmp.history_year,
tmp.history_month,
tmp.history_week,
tmp.fk_country,
tmp.fk_category,
COUNT(fk_diff_days) AS week_category_diff_days
FROM
(
SELECT
DATE_FORMAT(date, '%Y') AS history_year,
DATE_FORMAT(date, '%m') AS history_month,
DATE_FORMAT(date, '%V') AS history_week,
country AS fk_country,
category AS fk_category,
date AS fk_diff_days
FROM
study_test
WHERE
DATE_FORMAT(date, '%Y') = 2016
GROUP BY
fk_country,
fk_category,
fk_diff_days,
history_year,
history_month,
history_week
) AS tmp
GROUP BY
fk_country,
fk_category,
history_year,
history_month,
history_week
) AS wt_week_diff_days ON wt_week_sum.fk_country = wt_week_diff_days.fk_country
AND wt_week_sum.fk_category = wt_week_diff_days.fk_category
AND wt_week_sum.history_year = wt_week_diff_days.history_year
AND wt_week_sum.history_month = wt_week_diff_days.history_month
AND wt_week_sum.history_week = wt_week_diff_days.history_week
 SELECT COUNT(DISTINCT date),COUNT(DISTINCT asin), SUM(coin),SUM(amount),SUM(coin)/SUM(amount) FROM asinsaleranks WHERE category=9 AND  LEFT(date, 6) = ''
SELECT COUNT(DISTINCT date),COUNT(DISTINCT asin), SUM(coin),SUM(amount),SUM(coin)/SUM(amount) FROM asinsaleranks WHERE category=9 AND LEFT(date, 6) = '' SELECT COUNT(DISTINCT date),COUNT(DISTINCT asin), SUM(coin),SUM(amount),SUM(coin)/SUM(amount) FROM asinsaleranks WHERE category=6 AND date= ''
SELECT COUNT(DISTINCT date),COUNT(DISTINCT asin), SUM(coin),SUM(amount),SUM(coin)/SUM(amount) FROM asinsaleranks WHERE category=6 AND date> '' AND date< ''
SELECT COUNT(DISTINCT date),COUNT(DISTINCT asin), SUM(coin),SUM(amount),SUM(coin)/SUM(amount) FROM asinsaleranks WHERE category=6 AND date> '' AND date< ''
 SELECT
*
FROM
(
SELECT
DATE_FORMAT(date, '%Y') AS history_year,
DATE_FORMAT(date, '%m') AS history_month,
SUM(coin) AS month_category_coin,
SUM(amount) AS month_category_amount,
country AS fk_country,
category AS fk_category
FROM
study_test
WHERE
DATE_FORMAT(date, '%Y') = 2016
GROUP BY
fk_country,
fk_category,
history_year,
history_month
) AS wt_month_sum
LEFT JOIN (
SELECT
tmp.history_year,
tmp.history_month,
tmp.fk_country,
tmp.fk_category,
COUNT(fk_asin) AS month_category_diff_asins
FROM
(
SELECT
DATE_FORMAT(date, '%Y') AS history_year,
DATE_FORMAT(date, '%m') AS history_month,
country AS fk_country,
category AS fk_category,
asin AS fk_asin
FROM
study_test
WHERE
DATE_FORMAT(date, '%Y') = 2016
GROUP BY
fk_country,
fk_category,
fk_asin,
history_year,
history_month
) AS tmp
GROUP BY
fk_country,
fk_category,
history_year,
history_month
) AS wt_month_diff_asins ON wt_month_sum.fk_country = wt_month_diff_asins.fk_country
AND wt_month_sum.fk_category = wt_month_diff_asins.fk_category
AND wt_month_sum.history_year = wt_month_diff_asins.history_year
AND wt_month_sum.history_month = wt_month_diff_asins.history_month
LEFT JOIN (
SELECT
tmp.history_year,
tmp.history_month,
tmp.fk_country,
tmp.fk_category,
COUNT(fk_diff_days) AS month_category_diff_days
FROM
(
SELECT
DATE_FORMAT(date, '%Y') AS history_year,
DATE_FORMAT(date, '%m') AS history_month,
country AS fk_country,
category AS fk_category,
date AS fk_diff_days
FROM
study_test
WHERE
DATE_FORMAT(date, '%Y') = 2016
GROUP BY
fk_country,
fk_category,
fk_diff_days,
history_year,
history_month
) AS tmp
GROUP BY
fk_country,
fk_category,
history_year,
history_month
) AS wt_month_diff_days ON wt_month_sum.fk_country = wt_month_diff_days.fk_country
AND wt_month_sum.fk_category = wt_month_diff_days.fk_category
AND wt_month_sum.history_year = wt_month_diff_days.history_year
AND wt_month_sum.history_month = wt_month_diff_days.history_month
LEFT JOIN (
SELECT
DATE_FORMAT(date, '%Y') AS history_year,
DATE_FORMAT(date, '%m') AS history_month,
DATE_FORMAT(date, '%V') AS history_week,
SUM(coin) AS week_category_coin,
SUM(amount) AS week_category_amount,
country AS fk_country,
category AS fk_category
FROM
study_test
WHERE
DATE_FORMAT(date, '%Y') = 2016
GROUP BY
fk_country,
fk_category,
history_year,
history_month,
history_week
) AS wt_week_sum ON wt_month_sum.fk_country = wt_week_sum.fk_country
AND wt_month_sum.fk_category = wt_week_sum.fk_category
AND wt_month_sum.history_year = wt_week_sum.history_year
AND wt_month_sum.history_month = wt_week_sum.history_month
LEFT JOIN (
SELECT
tmp.history_year,
tmp.history_month,
tmp.history_week,
tmp.fk_country,
tmp.fk_category,
COUNT(fk_diff_asin) AS week_category_diff_asins
FROM
(
SELECT
DATE_FORMAT(date, '%Y') AS history_year,
DATE_FORMAT(date, '%m') AS history_month,
DATE_FORMAT(date, '%V') AS history_week,
country AS fk_country,
category AS fk_category,
asin AS fk_diff_asin
FROM
study_test
WHERE
DATE_FORMAT(date, '%Y') = 2016
GROUP BY
fk_country,
fk_category,
fk_diff_asin,
history_year,
history_month,
history_week
) AS tmp
GROUP BY
fk_country,
fk_category,
history_year,
history_month,
history_week
) AS wt_week_diff_asins ON wt_week_sum.fk_country = wt_week_diff_asins.fk_country
AND wt_week_sum.fk_category = wt_week_diff_asins.fk_category
AND wt_week_sum.history_year = wt_week_diff_asins.history_year
AND wt_week_sum.history_month = wt_week_diff_asins.history_month
AND wt_week_sum.history_week = wt_week_diff_asins.history_week
LEFT JOIN (
SELECT
tmp.history_year,
tmp.history_month,
tmp.history_week,
tmp.fk_country,
tmp.fk_category,
COUNT(fk_diff_days) AS week_category_diff_days
FROM
(
SELECT
DATE_FORMAT(date, '%Y') AS history_year,
DATE_FORMAT(date, '%m') AS history_month,
DATE_FORMAT(date, '%V') AS history_week,
country AS fk_country,
category AS fk_category,
date AS fk_diff_days
FROM
study_test
WHERE
DATE_FORMAT(date, '%Y') = 2016
GROUP BY
fk_country,
fk_category,
fk_diff_days,
history_year,
history_month,
history_week
) AS tmp
GROUP BY
fk_country,
fk_category,
history_year,
history_month,
history_week
) AS wt_week_diff_days ON wt_week_sum.fk_country = wt_week_diff_days.fk_country
AND wt_week_sum.fk_category = wt_week_diff_days.fk_category
AND wt_week_sum.history_year = wt_week_diff_days.history_year
AND wt_week_sum.history_month = wt_week_diff_days.history_month
AND wt_week_sum.history_week = wt_week_diff_days.history_week
 DROP TABLE IF EXISTS `study_test`;
CREATE TABLE `study_test` (
`country` char(2) COLLATE utf8_bin NOT NULL,
`date` char(8) COLLATE utf8_bin NOT NULL DEFAULT '',
`asin` char(10) COLLATE utf8_bin NOT NULL,
`category` int(10) unsigned NOT NULL DEFAULT '',
`coin` decimal(16,4) unsigned NOT NULL DEFAULT '0.0000',
`amount` decimal(16,4) unsigned NOT NULL DEFAULT '0.0000',
PRIMARY KEY (`country`,`date`,`asin`),
KEY `k_asin` (`asin`) USING BTREE,
KEY `k_coin` (`coin`) USING BTREE,
KEY `k_amount` (`amount`) USING BTREE
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

wdate-year-month-week-gategory-amount-coin的更多相关文章

  1. T-SQL - 习题02_将数据表year|month|amount查询成year|m1|m2|m3|m4的样式

    时间:2017-09-11 整理:byzqy 题目:有个年度统计表,结构如下: 怎么样把这个表,查询成这样一个结果: 这是在面试过程中遇到的一个关于数据库的题,没有一点思路,不知它考查到的知识点是什么 ...

  2. [Swift]LeetCode518. 零钱兑换 II | Coin Change 2

    You are given coins of different denominations and a total amount of money. Write a function to comp ...

  3. Give $20/month and provide 480 hours of free education

    Hi , Hope all is well. Summer is right around the corner, and the Khan Academy team is excited to sp ...

  4. Kotlin 介绍

    Kotlin (0:00) 大家好,我是 Michael Pardo,今天我要给大家展示一下 Kotlin 这门语言,同时看看他如何让你在 Android 开发的时候更开心,更有效率. Kotlin ...

  5. oracle 触发器学习

    触发器使用教程和命名规范 目  录触发器使用教程和命名规范 11,触发器简介 12,触发器示例 23,触发器语法和功能 34,例一:行级触发器之一 45,例二:行级触发器之二 46,例三:INSTEA ...

  6. SSH三大框架笔面试总结

    Java工程师(程序员)面题 Struts,Spring,Hibernate三大框架 1.Hibernate工作原理及为什么要用? 原理: 1.读取并解析配置文件 2.读取并解析映射信息,创建Sess ...

  7. Oracle触发器实例(网搜)

    触发器使用教程和命名规范 目  录触发器使用教程和命名规范 11,触发器简介 12,触发器示例 23,触发器语法和功能 34,例一:行级触发器之一 45,例二:行级触发器之二 46,例三:INSTEA ...

  8. 关于完整解答Leo C.W博客中名为“我们公司的ASP.NET 笔试题,你觉得难度如何”的所有题目

    关于完整解答Leo C.W博客中名为“我们公司的ASP.NET 笔试题,你觉得难度如何”的所有题目,请大家鉴定,不足之处,敬请指教! 第1到3题解答如下: public enum QuestionTy ...

  9. mysql 经典题目

    题目1:实现如下效果 CREATE TABLE IF NOT EXISTS tb_amount( `Id` INT NOT NULL AUTO_INCREMENT, `), `), `Amount` ...

  10. How to do Mathematics

    著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处.作者:匿名用户链接:http://www.zhihu.com/question/30087053/answer/47815698来源 ...

随机推荐

  1. Java Applet与Java Application的区别

    转自:http://www.educity.cn/java/500609.html 在Java语言中,能够独立运行的程序称为Java应用程序(Application).Java语言还有另外一种程序-- ...

  2. C++ Primer与c++编程思想的比较(转)

    C++primer是最经典的c++教材之一,它的经典程度要超过thinking in c++.连thinking in c++作者本人都说他写这本书在某种程度上是让读者更好的理解C++primer.但 ...

  3. Android权限设置android.permission

    android.permission.ACCESS_NETWORK_STATE: 允许程序访问有关GSM网络信息(Allows applications to access information a ...

  4. SQL SERVER 与ACCESS、EXCEL的数据转换

    --Excel导入到SQL的一个新思路: /*比如Excel有两列,A列和B列需要导入到SQL表中,反正我已经有几年不用DTS之类的工具了. 在Excel中的新的一列中,直接写公式 =CONCATEN ...

  5. Insert select 带选择复制一张表到另一张表

    使用SELECT INTO 和 INSERT INTO SELECT 表复制语句了. 1.INSERT INTO SELECT语句 语句形式为:Insert into Table2(field1,fi ...

  6. mac OS X操作--快捷键

  7. 人见人爱A^B

    求A^B的最后三位数表示的整数.说明:A^B的含义是“A的B次方”   Input 输入数据包含多个测试实例,每个实例占一行,由两个正整数A和B组成(1<=A,B<=10000),如果A= ...

  8. LightOJ1201 A Perfect Murder(树形DP)

    一道经典的树型DP入门题.dp[u][0/1]表示u点不选或选时以u为根的子树最多能选择的点数. 题目给的有向有环图可以看作森林,注意不是树,因为题目没有说图是连通的! #include<cst ...

  9. POJ1780 Code(欧拉路径)

    n位密码,要用尽可能短的序列将n位密码的10n种状态的子串都包括,那么要尽量地重合. 题目已经说最短的是10n + n - 1,即每一个状态的后n-1位都和序列中后一个状态的前n-1位重合. 这题是经 ...

  10. ZOJ1232 Adventure of Super Mario(DP+SPFA)

    dp[u][t]表示从起点出发,到达i点且用了t次magic boot时的最短时间, 方程如下: dp[v][t]=min(dp[v][t],dp[u][t]+dis[u][v]); dp[v][t] ...