---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. C# 读取本地图片 转存到其他盘符

    UpFileContent upfile = new UpFileContent(); upfile.StationImageName = "123.png"; FileStrea ...

  2. hdu 2795 线段树(纵向)

    注意h的范围和n的范围,纵向建立线段树 题意:h*w的木板,放进一些1*L的物品,求每次放空间能容纳且最上边的位子思路:每次找到最大值的位子,然后减去L线段树功能:query:区间求最大值的位子(直接 ...

  3. 建模算法(十一)——目标规划

    求解多目标规划的思路 1.加权系数法 为每一个目标加一个权系数,把多目标模型转化成单一目标模型.但是困难时确定合理的权系数,以反映不同目标之间的重要程度. 2.优先等级法 将各目标按其重要程度分为不同 ...

  4. 【xml 报错】xml编译错误

    ---恢复内容开始--- 有时候在xml文件中,特别是于Spring相关的配置文件中,会出现一些不影响程序正常运行的编译错误,如: Referenced file contains errors (h ...

  5. mysql ODBC 在64位下提示找不到odbc驱动问题

    在64位机器上,如果你想要连接32位mysql ,一般会安装mysql connector/ODBC 64位,并在配置ODBC数据源测试中连接正常,但在程序连接,如ASP.asp.net.VB.Del ...

  6. C# params参数的应用

    为了将方法声明为可以接受可变数量参数的方法,我们可以使用params关键字来声明数组,如下所示: public static Int32Add(params Int32[] values) { Int ...

  7. null VS undefined

    null VS undefined “null与undefined的区别?” 以前去淘宝面试的时候被问过这个问题,当时只是粗略的按照犀牛书上的答案讲了下,但具体的并没有深入去了解. 后来有机会去问问身 ...

  8. 【BZOJ】2002: [Hnoi2010]Bounce 弹飞绵羊(lct)

    http://www.lydsy.com/JudgeOnline/problem.php?id=2002 (BZOJ挂了,还没在BZOJ测,先是在wikioi测过了,,) 囧.在军训时立志要学lct! ...

  9. HttpClient_javax.net.ssl.SSLHandshakeException: sun.security.validator 问题解决,与环境有关

    用httpclient访问https 资源时,会出现异常,与环境也有关系,有些机器请求正常. javax.net.ssl.SSLHandshakeException: sun.security.val ...

  10. Quartz与Spring整合进行热部署的实现(一)

    先来几张实现图 任务管理页 新建任务管理.目前实现叫简单的需求...若各位同学要实现复杂的设计...quartz都有提供强大的支持.小弟目前的需求做到这已经够用了. 接下来.我们如何实现quartz的 ...