首页 > 数据库 >MySQL分组聚合

MySQL分组聚合

时间:2023-10-14 13:44:54浏览次数:38  
标签:聚合 business MAX px MIC1 timestamp 分组 MySQL data

-- 创建hq_kline表
delete from hq_kline
CREATE TABLE hq_kline (
    id INT AUTO_INCREMENT PRIMARY KEY,
    finance_mic VARCHAR(16) NOT NULL,
    prod_code VARCHAR(64) NOT NULL,
    trade_date INT NOT NULL,
    data_timestamp INT NOT NULL,
    preclose_px DECIMAL(9,3) NOT NULL,
    open_px DECIMAL(9,3) NOT NULL,
    high_px DECIMAL(9,3) NOT NULL,
    low_px DECIMAL(9,3) NOT NULL,
    close_px DECIMAL(9,3) NOT NULL,
    business_count INT NOT NULL,
    business_amount BIGINT NOT NULL,
    business_balance DECIMAL(18,3) NOT NULL
);
-- 插入数据
INSERT INTO hq_kline (finance_mic, prod_code, trade_date, data_timestamp, preclose_px, open_px, high_px, low_px, close_px, business_count, business_amount, business_balance)
VALUES
    -- 第一组数据
    ('MIC1', 'CODE1', 20231014, 1634131240, 100.000, 105.000, 110.000, 95.000, 100.500, 1000, 50000, 25000.000),
    ('MIC1', 'CODE1', 20231014, 1634131300, 101.000, 106.000, 111.000, 96.000, 101.500, 1100, 55000, 28000.000),
    ('MIC1', 'CODE1', 20231014, 1634131350, 102.000, 107.000, 112.000, 97.000, 102.500, 1200, 60000, 30000.000),
    ('MIC1', 'CODE1', 20231014, 1634131410, 103.000, 108.000, 113.000, 98.000, 103.500, 1300, 65000, 32000.000),
    ('MIC1', 'CODE1', 20231014, 1634131470, 104.000, 109.000, 114.000, 99.000, 104.500, 1400, 70000, 34000.000),
    -- 第二组数据
    ('MIC1', 'CODE1', 20231015, 1634131550, 105.000, 110.000, 115.000, 100.000, 105.500, 1500, 75000, 36000.000),
    ('MIC1', 'CODE1', 20231015, 1634131610, 106.000, 111.000, 116.000, 101.000, 106.500, 1600, 80000, 38000.000),
    ('MIC1', 'CODE1', 20231015, 1634131670, 107.000, 112.000, 117.000, 102.000, 107.500, 1700, 85000, 40000.000),
    ('MIC1', 'CODE1', 20231015, 1634131730, 108.000, 113.000, 118.000, 103.000, 108.500, 1800, 90000, 42000.000),
    ('MIC1', 'CODE1', 20231015, 1634131790, 109.000, 114.000, 119.000, 104.000, 109.500, 1900, 95000, 44000.000),
    -- 第三组数据
    ('MIC1', 'CODE1', 20231016, 1634142550, 110.000, 115.000, 120.000, 105.000, 110.500, 2000, 100000, 46000.000),
    ('MIC1', 'CODE1', 20231016, 1634142600, 111.000, 116.000, 121.000, 106.000, 111.500, 2100, 105000, 48000.000),
    ('MIC1', 'CODE1', 20231016, 1634142610, 112.000, 117.000, 122.000, 107.000, 112.500, 2200, 110000, 50000.000),
    ('MIC1', 'CODE1', 20231016, 1634142620, 113.000, 118.000, 123.000, 108.000, 113.500, 2300, 115000, 52000.000),
    ('MIC1', 'CODE1', 20231016, 1634142700, 114.000, 119.000, 124.000, 109.000, 114.500, 2400, 120000, 54000.000);
//按照每五分钟分组统计,以每组最大时间为新记录的时间戳,计算每组的business_count,business_amount,business_balance总和作为对应的结果,其他字段为每组的最大时间的同行数据。
SELECT
    finance_mic,
    prod_code,
    MAX(data_timestamp) AS new_data_timestamp,
    SUM(business_count) AS total_business_count,
    SUM(business_amount) AS total_business_amount,
    SUM(business_balance) AS total_business_balance,
    MAX(open_px) AS last_open_px,
    MAX(high_px) AS last_high_px,
    MAX(low_px) AS last_low_px,
    MAX(close_px) AS last_close_px,
    MAX(preclose_px) AS last_preclose_px
FROM (
    SELECT
        finance_mic,
        prod_code,
        FROM_UNIXTIME(data_timestamp - MOD(data_timestamp, 300)) AS group_timestamp,
        MAX(data_timestamp) AS data_timestamp,
        SUM(business_count) AS business_count,
        SUM(business_amount) AS business_amount,
        SUM(business_balance) AS business_balance,
        MAX(open_px) AS open_px,
        MAX(high_px) AS high_px,
        MAX(low_px) AS low_px,
        MAX(close_px) AS close_px,
        MAX(preclose_px) AS preclose_px
    FROM hq_kline
    GROUP BY finance_mic, prod_code, group_timestamp
) AS subquery
GROUP BY finance_mic, prod_code, group_timestamp;

SELECT
    MAX(data_timestamp) AS new_data_timestamp,
    MAX(finance_mic) AS finance_mic,
    MAX(prod_code) AS prod_code,
    MAX(trade_date) AS trade_date,
    MAX(preclose_px) AS preclose_px,
    MAX(open_px) AS open_px,
    MAX(high_px) AS high_px,
    MAX(low_px) AS low_px,
    MAX(close_px) AS close_px,
    SUM(business_count) AS total_business_count,
    SUM(business_amount) AS total_business_amount,
    SUM(business_balance) AS total_business_balance
FROM (
    SELECT
        *,
        CEIL(data_timestamp / 300) AS group_number
    FROM hq_kline
) AS numbered_data
GROUP BY group_number;


//每五个记录为一组,分组统计,以每组最大时间为新记录的时间戳,计算每组的business_count,business_amount,business_balance总和作为对应的结果,其他字段为每组的最大时间的同行数据。
SELECT
    MAX(data_timestamp) AS new_data_timestamp,
    SUM(business_count) AS total_business_count,
    SUM(business_amount) AS total_business_amount,
    SUM(business_balance) AS total_business_balance,
    MAX(preclose_px) AS last_preclose_px,
    MAX(open_px) AS last_open_px,
    MAX(high_px) AS last_high_px,
    MAX(low_px) AS last_low_px,
    MAX(close_px) AS last_close_px
FROM (
    SELECT
        *,
        CEIL(ROW_NUMBER() OVER (ORDER BY data_timestamp) / 5) AS group_number
    FROM hq_kline
) AS numbered_data
GROUP BY group_number;

 

标签:聚合,business,MAX,px,MIC1,timestamp,分组,MySQL,data
From: https://www.cnblogs.com/cjhtxdy/p/17764086.html

相关文章

  • 索引是不是建的越多越好?MySQL索引用的什么数据结构?一棵B+树能存储多少条数据?
    一、索引是不是建的越多越好当然不是。索引会占据磁盘空间索引虽然会提高查询效率,但是会降低更新表的效率。比如每次对表进行增删改操作,MySQL不仅要保存数据,还有保存或者更新对应的索引文件。二、MySQL索引用的什么数据结构MySQL的默认存储引擎是InnoDB,它采用的是B+树结构的索引......
  • LINUX:sqoop连接:ERROR manager.CatalogQueryManager: Failed to list databases com.my
    可见是连接的jar包出现了错误  通过命令发现我的是5.7版本,将驱动jar包替换为5.几的  之后通过命令成功解决......
  • # yyds干货盘点 # Pandas将三个聚合结果的列,如何合并到一张表里?
    大家好,我是皮皮。一、前言前几天在Python最强王者交流群【斌】问了一个Pandas数据处理的问题,一起来看看吧。求教:将三个聚合结果的列,如何合并到一张表里?这是前两列,能够合并。这是第三列,加权平均,也算出来了。但我不会合并。。。。二、实现过程后来【隔壁......
  • MySQL学习(5)记录存在那里——表空间
    前言存储引擎都是把数据存储在文件系统上,通过通过查询命令,可以查看数据目录所在的本机路径。mysql>SHOWVARIABLESLIKE'datadir';+---------------+-----------------+|Variable_name|Value|+---------------+-----------------+|datadir|/var/l......
  • 将excel文件导入到Navicat的mysql中步骤详解
    将转换好的csv文件导入到Navicat中1、右键数据库中表的名称,选中导入向导:2、之后选择导入的文件结构:我们在这里选中excel类型;点击下一步;3、浏览到excel文件的路径然后选中该表:点击下一步;4、自定义一些附加选项点击下一步;5、选中这个新建表然后点击下一步;6、修改表......
  • MySQL删除表的命令是什么
    MySQL删除表命令:删除表命令是DROPTABLE,其基本语法如下:删除单个表DROPTABLEtable_name;注:这个命令会直接删除指定表,如果这个表不存在就会报错。另外,删除表之前最好做好备份。同时删除多个表DROPTABLEtable_name1,table_name2,...;注:这个命令可以同时删除多张......
  • 文件默认打开方式 + mysql导入错误 + 输入法问题
    文件默认打开方式默认应用修改:设置—》应用—》默认应用—》按文件类型指定默认应用mysql导入错误Unknowncollation:'utf8mb4_0900_ai_ci'Mysql导入sql文件时,出Unknowncollation:'utf8mb4_0900_ai_ci'错误。原因:sql文件是从高版本mysql(8.0)中导出的,导入到......
  • mysql数据库性能优化
    数据库的性能优化可以从以下几个方面进行优化:1.硬件和操作系统:硬件可以从cpu、内存、I/O,网络带宽等方面进行优化。系统层可以从文件句柄数,网络配置等方面2.数据库的架构:比如主从集群以及主从架构的变种可以做高可用及容灾,读写分离可以避免读操作比较高的服务影响数据写入,分库分表......
  • 深入理解MySQL中的Join算法
    本文已收录至GitHub,推荐阅读......
  • mysql 分区表
    场景:数据库磁盘爆满,使用delete无法清理磁盘,清理会有binlog,redolog,磁盘无法释放。个人遇到的真实场景:数据库数据上千万,要清理历史数据,但是delete删除数据无法释放磁盘,运维老大哥给的方案,先分区表,然后再truncate这样就能清理历史数据,磁盘也能释放。 操作步骤:1,查看数据库......