---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 Hour 12 Generic

    有句名言,叫做10000小时成为某一个领域的专家.姑且不辩论这句话是否正确,让我们到达10000小时的时候再回头来看吧. 本文作者Java 现经验约为12 Hour,请各位不吝赐教. 泛型程序设计 为 ...

  2. 理解HTTP session原理及应用

    转自:http://www.2cto.com/kf/201206/135471.html 一.术语session在我的经验里,session这个词被滥用的程度大概仅次于transaction,更加有趣 ...

  3. JavaEE路径陷阱之getRealPath

    转自:http://blog.csdn.net/shendl/article/details/1427637   JavaEE路径陷阱之getRealPath   本文是<Java路径问题最终解 ...

  4. PHP扩展开发

    安装好php,进入安装目录. zbseoag@ubuntu:/usr/local/php-5.6.28/ext$ ./ext_skel --extname=mytest zbseoag@ubuntu: ...

  5. SQL Server--用户自定义函数

    除了使用系统提供的函数外,用户还可以根据需要自定义函数.用户自定义函数是 SQL Server 2000 新增的数据库对象,是 SQL Server 的一大改进.与编程语言中的函数类似,Microso ...

  6. C#环境datagidview添加删除操作

    添加 行 dataGridView1.Rows.Add();//添加空行 dataGridView1.Rows.Add("a","b"……);//添加指定列数的 ...

  7. js执行顺序

    我们知道有个全局的 window对象,js的一切皆window上的属性和方法.window上有个window.document属性,记录了整个html的dom树,document是顶层. body 和 ...

  8. 添加网页ico

    <link rel="Shortcut Icon" href="/favicon.ico" />

  9. achartengine 绘图表神器

    http://code.google.com/p/achartengine/

  10. POJ2763 Housewife Wind(DFS序)

    题目:单边修改,树链查询. 这题是边权,不是点权,不过也可以看作是点权. 然后其实就和BZOJ2819一样. #include<cstdio> #include<cstring> ...