---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. node.js简单的页面输出

    在node.js基本上没有兼容问题(如果你不是从早期的node.js玩起来),而且原生对象又加了这么多扩展,再加上node.js自带的库,每个模块都提供了花样繁多的API,如果还嫌不够,github上 ...

  2. SURF算法与源码分析、下

    上一篇文章 SURF算法与源码分析.上 中主要分析的是SURF特征点定位的算法原理与相关OpenCV中的源码分析,这篇文章接着上篇文章对已经定位到的SURF特征点进行特征描述.这一步至关重要,这是SU ...

  3. android 定时器的实现

    在Android上常用的定时器有两种,一种是Java.util.Timer,一种就是系统的AlarmService了. 实验1:使用Java.util.Timer. 在onStart()创创建Time ...

  4. hadoop1.2.1的namenode格式化失败的问题

    最近要开始找工作,就在原来搭建好的hadoop1.2.1的伪分布式跑跑mapreduce 很久没用,就想着格式化一下namode,结果: Format aborted in /uar/local/ha ...

  5. throws 和 throw

    package unit5; public class Person { private int age; private String name; public String getName() { ...

  6. 使用egypt+graphviz生成函数调用关系图示例

    总结: make  (-fdump-rtl-expand)  去除编译优化,比如-O3 egypt test.c.128r.expand >test.dot  可以手动打开dot文件去除一些孤立 ...

  7. Spring的qualifier标签

    @Autowired是根据类型进行自动装配的.如果当Spring上下文中存在不止一个UserDao类型的bean时,就会抛出BeanCreationException异常;如果Spring上下文中不存 ...

  8. ZOJ1516 Uncle Tom's Inherited Land(二分图最大匹配)

    一个经典的构图:对格子进行黑白染色,黑白的点分别作XY部的点. 这一题的边就是可以出售的单位面积2的土地,边的端点就是这个土地占用的X部和Y部的两个点. 这样就建好二分图,要求最多土地的答案显然是这个 ...

  9. WPF之资源字典zz

    最近在看wpf相关东西,虽然有过两年的wpf方面的开发经验,但是当时开发的时候,许多东西一知半解,至今都是模模糊糊,框架基本是别人搭建,自己也就照着模板写写,现在许多东西慢慢的理解了,回顾以前的若干记 ...

  10. 找模式串[XDU1032]

    Problem 1032 - 找模式串 Time Limit: 1000MS   Memory Limit: 65536KB   Difficulty: Total Submit: 644  Acce ...