• SQL查询语句


    -------------------------------------------------------------------

    源文地址: http://www.cnblogs.com/yunlei0821/p/7644509.html ,转载请务必保留此出处.

    • 建表:
      drop table if exists tt_max_price;
      create table tt_max_price(
          id int(11) not null AUTO_INCREMENT,
          dt datetime,
          price float,
          primary key(id)
      );
      建表语句
    • 模拟数据:
      insert into tt_max_price(dt,price) VALUES('2015-01-01',23.5);
      insert into tt_max_price(dt,price) VALUES('2015-01-02',23.2);
      insert into tt_max_price(dt,price) VALUES('2015-01-03',23.3);
      insert into tt_max_price(dt,price) VALUES('2015-01-04',13.5);
      insert into tt_max_price(dt,price) VALUES('2015-01-05',233.5);
      insert into tt_max_price(dt,price) VALUES('2015-01-06',253.5);
      insert into tt_max_price(dt,price) VALUES('2015-01-07',221.5);
      insert into tt_max_price(dt,price) VALUES('2015-01-08',2233.5);
      insert into tt_max_price(dt,price) VALUES('2015-01-09',213.5);
      insert into tt_max_price(dt,price) VALUES('2015-01-10',23.54);
      insert into tt_max_price(dt,price) VALUES('2015-01-11',23.45);
      insert into tt_max_price(dt,price) VALUES('2015-01-12',123.5);
      insert into tt_max_price(dt,price) VALUES('2015-01-13',223.5);
      insert into tt_max_price(dt,price) VALUES('2015-01-14',213.5);
      insert into tt_max_price(dt,price) VALUES('2015-01-15',23.5);
      insert into tt_max_price(dt,price) VALUES('2015-01-16',233.5);
      insert into tt_max_price(dt,price) VALUES('2015-01-17',2343.5);
      insert into tt_max_price(dt,price) VALUES('2015-01-18',243.5);
      insert into tt_max_price(dt,price) VALUES('2015-01-19',2113.5);
      insert into tt_max_price(dt,price) VALUES('2015-01-20',23.5);
      insert into tt_max_price(dt,price) VALUES('2015-01-21',2333.5);
      insert into tt_max_price(dt,price) VALUES('2015-01-22',213.5);
      insert into tt_max_price(dt,price) VALUES('2015-01-23',213.5);
      insert into tt_max_price(dt,price) VALUES('2015-01-24',231.5);
      insert into tt_max_price(dt,price) VALUES('2015-01-25',233.5);
      insert into tt_max_price(dt,price) VALUES('2015-01-26',233.5);
      insert into tt_max_price(dt,price) VALUES('2015-01-27',223.5);
      insert into tt_max_price(dt,price) VALUES('2015-01-28',323.5);
      insert into tt_max_price(dt,price) VALUES('2015-01-29',233.5);
      insert into tt_max_price(dt,price) VALUES('2015-01-30',233.5);
      insert into tt_max_price(dt,price) VALUES('2015-01-31',2903.5);
      
      insert into tt_max_price(dt,price) VALUES('2015-02-01',23.5);
      insert into tt_max_price(dt,price) VALUES('2015-02-02',23.2);
      insert into tt_max_price(dt,price) VALUES('2015-02-03',23.3);
      insert into tt_max_price(dt,price) VALUES('2015-02-04',13.5);
      insert into tt_max_price(dt,price) VALUES('2015-02-05',233.5);
      insert into tt_max_price(dt,price) VALUES('2015-02-06',253.5);
      insert into tt_max_price(dt,price) VALUES('2015-02-07',221.5);
      insert into tt_max_price(dt,price) VALUES('2015-02-08',2233.5);
      insert into tt_max_price(dt,price) VALUES('2015-02-09',213.5);
      insert into tt_max_price(dt,price) VALUES('2015-02-10',23.54);
      insert into tt_max_price(dt,price) VALUES('2015-02-11',23.45);
      insert into tt_max_price(dt,price) VALUES('2015-02-12',123.5);
      insert into tt_max_price(dt,price) VALUES('2015-02-13',223.5);
      insert into tt_max_price(dt,price) VALUES('2015-02-14',213.5);
      insert into tt_max_price(dt,price) VALUES('2015-02-15',23.5);
      insert into tt_max_price(dt,price) VALUES('2015-02-16',233.5);
      insert into tt_max_price(dt,price) VALUES('2015-02-17',23643.5);
      insert into tt_max_price(dt,price) VALUES('2015-02-18',243.5);
      insert into tt_max_price(dt,price) VALUES('2015-02-19',2113.5);
      insert into tt_max_price(dt,price) VALUES('2015-02-20',23.5);
      insert into tt_max_price(dt,price) VALUES('2015-02-21',2333.5);
      insert into tt_max_price(dt,price) VALUES('2015-02-22',213.5);
      insert into tt_max_price(dt,price) VALUES('2015-02-23',213.5);
      insert into tt_max_price(dt,price) VALUES('2015-02-24',231.5);
      insert into tt_max_price(dt,price) VALUES('2015-02-25',233.5);
      insert into tt_max_price(dt,price) VALUES('2015-02-26',233.5);
      insert into tt_max_price(dt,price) VALUES('2015-02-27',223.5);
      insert into tt_max_price(dt,price) VALUES('2015-02-28',323.5);
      
      insert into tt_max_price(dt,price) VALUES('2015-03-01',23.5);
      insert into tt_max_price(dt,price) VALUES('2015-03-02',23.2);
      insert into tt_max_price(dt,price) VALUES('2015-03-03',23.3);
      insert into tt_max_price(dt,price) VALUES('2015-03-04',13.5);
      insert into tt_max_price(dt,price) VALUES('2015-03-05',233.5);
      insert into tt_max_price(dt,price) VALUES('2015-03-06',253.5);
      insert into tt_max_price(dt,price) VALUES('2015-03-07',221.5);
      insert into tt_max_price(dt,price) VALUES('2015-03-08',2233.5);
      insert into tt_max_price(dt,price) VALUES('2015-03-09',213.5);
      insert into tt_max_price(dt,price) VALUES('2015-03-10',23.54);
      insert into tt_max_price(dt,price) VALUES('2015-03-11',23.45);
      insert into tt_max_price(dt,price) VALUES('2015-03-12',123.5);
      insert into tt_max_price(dt,price) VALUES('2015-03-13',223.5);
      insert into tt_max_price(dt,price) VALUES('2015-03-14',213.5);
      insert into tt_max_price(dt,price) VALUES('2015-03-15',23.5);
      insert into tt_max_price(dt,price) VALUES('2015-03-16',233.5);
      insert into tt_max_price(dt,price) VALUES('2015-03-17',2343.5);
      insert into tt_max_price(dt,price) VALUES('2015-03-18',243.5);
      insert into tt_max_price(dt,price) VALUES('2015-03-19',2113.5);
      insert into tt_max_price(dt,price) VALUES('2015-03-20',23.5);
      insert into tt_max_price(dt,price) VALUES('2015-03-21',2333.5);
      insert into tt_max_price(dt,price) VALUES('2015-03-22',213.5);
      insert into tt_max_price(dt,price) VALUES('2015-03-23',29003.5);
      insert into tt_max_price(dt,price) VALUES('2015-03-24',231.5);
      insert into tt_max_price(dt,price) VALUES('2015-03-25',233.5);
      insert into tt_max_price(dt,price) VALUES('2015-03-26',233.5);
      insert into tt_max_price(dt,price) VALUES('2015-03-27',223.5);
      insert into tt_max_price(dt,price) VALUES('2015-03-28',323.5);
      insert into tt_max_price(dt,price) VALUES('2015-03-29',233.5);
      insert into tt_max_price(dt,price) VALUES('2015-03-30',233.5);
      insert into tt_max_price(dt,price) VALUES('2015-03-31',23.5);
      模拟数据
    • 查询:
      1. 查询每个月存在记录的最后一天:

        select a.* from tt_max_price a,
        (select max(dt) dtb from tt_max_price where price is not null group by extract(month from dt)) b
        where a.dt=b.dtb;

      2. 查询每个月price记录的最大值:

    select a.* from tt_max_price a,
    (select max(price) bp from tt_max_price where price is not null group by extract(month from dt)) b
    where a.price=b.bp;

      




    出自博客 http://www.cnblogs.com/yunlei0821/,欢迎留言补充指正,仅供互相交流学习.转载请务必保留此出处..

    本文为作者原创 ,欢迎转载和收藏,转载请保留作者及出处,谢谢您的配合,如有侵权,请第一时间联系 yunlei0821@vip.qq.com,以便及时删除...

    晨露&无尘
  • 相关阅读:
    三、单例模式
    二、工厂模式
    一、设计模式六大原则
    十二、Spring之IOC容器初始化
    PythonWeb开发教程(二),搭建第一个django项目
    PythonWeb开发教程(一),开发之前需要准备什么
    基于python的性能测试工具–locust
    在成为测试大牛的路上,我推荐BestTest
    移动端自动化测试(一)appium环境搭建
    常用工具篇(二)死链接扫描工具–Xenu
  • 原文地址:https://www.cnblogs.com/yunlei0821/p/7644509.html
Copyright © 2020-2023  润新知