2022年4月

技术原理:

   date_trunc() 截断日期函数,完成定时时间语法

实现案例:

select to_char(date_trunc('quarter',add_date),'yyyy-MM') quarter from dr_information group by date_trunc('quarter',add_date) order by quarter desc

select date_trunc('month',now()) +interval '12 h';
//每月1号 12点

select date_trunc('month',now()) + interval '15 d 9 h 30 min';
//每月15号9点半

select date_trunc('day',now()) + interval '9 h';
//每天9点
select date_trunc('day',now()) + interval '7 d';
//每周的今天
select date_trunc('weak',now()) + interval '1d 1minute';
//每周的周二第一分钟

select date_trunc('h',now()) + interval '30 minute';
//每小时

select date_trunc('minute',now()) + interval '30 second';
//每分钟

select date_trunc('minute',now()) + interval '30 minute 30 second';
//每30分钟

select date_trunc('quarter',now()) + interval '15 d 15 h 15 minute 30 second';
//本季度的第15天,15小时 15分 30秒

select date_trunc('quarter',now() ) - interval '1 h';
//每个季度最后一天的晚上11点

select date_trunc('quarter',now() + interval '3 month') - interval '1 h';
//每个季度的最后一天的晚上的11点(从下个季度开始算起)

select product_id,survey_date+interval '1 DAYS'

 select * from (

    select *,

    case when last_week_avg_price=0 then 0 else (this_week_avg_price-last_week_avg_price)/last_week_avg_price*100 end  ringvalue,
    (select sum(cast(this_day_trading as double)) from dr_information_items  where good_id=v.good_id and date_week=to_char(to_date(#{surveyDateWeek},'yyyy-MM-dd')+interval '-7 DAYS', 'yyyy-mm-dd') group by good_id,date_week) lastWeekTrading
    from
    (

    SELECT
    min(dr.id) id,
    items.cateid AS class_code,
    min(items.good_name) AS class_name,
    min(items.good_id) AS good_id,
    min(items.product) AS product,
    sum(items.this_day_count_price) AS this_week_price,
    sum(cast(items.this_day_trading as double)) AS this_day_trading,
    case when sum(cast(items.this_day_trading as double))=0 then avg(items.last_week_avg_price)  else  sum(items.this_day_count_price)/sum(cast(items.this_day_trading as double)) end this_week_avg_price ,
    avg(items.last_week_avg_price) AS last_week_avg_price,
    min(items.unit) AS unit,
    max(u.user_name) shop_number
    FROM dr_information_items items
    left JOIN dr_information dr ON dr.id = items.infomationid
    left JOIN sys_user u on LTrim(RTrim(items.create_by))=cast(LTrim(RTrim(u.user_name)) as text)
    WHERE  items.date_week IS NOT NULL AND dr.status =#{status}
    <if test="marketId != null  and marketId != ''">
        and dr.market_id=#{marketId}
    </if>
    <if test="surveyDateWeek != null">
        and to_date(items.date_week,'YYYY-MM-DD')=#{surveyDateWeek}
    </if>
    GROUP BY items.date_week, items.cateid
    ) v
    ) t where 1=1
    <if test="classCode != null">
        and LEFT(class_code,4)=#{classCode}
    </if>
    <if test="ringvalue != null">
        and ringvalue <![CDATA[ >= ]]> #{ringvalue}
    </if>

    UNION

    select  id,class_id class_code,name class_name,'' good_id,'' product,null this_week_price,null this_day_trading,null this_week_avg_price,null last_week_avg_price,'' unit,'' shop_number,null ringvalue,null lastWeekTrading

    from dr_lybase_class where  LEFT(class_id,4)=#{classCode}  and LENGTH(class_id)=8 and  LEFT(class_id,8)  in (select LEFT(cateid,8) from   dr_information_items
     where  to_date(date_week,'YYYY-MM-DD')=#{surveyDateWeek})

    order by class_code

float: 10^38 1e+38

float:单精度类型,精度是8位有效数字,取值范围是10的-38次方到10的38次方,float占用4个字节的存储空间。
也就是说(1的后面38个零)

double:双精度类型,精度是17位有效数字,取值范围是10的-308次方到10的308次方,double占用8个字节的存储空间
也就是说(1的后面308个零)

网友1:
一般情况下float即可以满足实数计算要求,当你的精度要求非常小的时候,比如1e-6的时候就可以考虑用double了,float和double主要的区别就是精度问题

网友2:
类型 符号位 阶码 尾数 长度
float 1 8 23 32
double 1 11 52 64
double 和 float 的区别是 double 精度高,有效数字 16 位,float 精度 7 位。但 double 消耗内存是 float 的两倍,double 的运算速度比 float 慢得多,能用单精度时不要用双精度(以省内存,加快运算速度)。

#include <iostream>

include <iomanip>

using namespace std;

int main()
{

float a=12.257902012398877;
double b=12.257902012398877;
const float PI=3.1415926;         // 常量定义
cout<<setprecision(15)<<a<<endl;  // 只有6-7位有效数字,后面的就不精确
cout<<setprecision(15)<<b<<endl;  // 有15-16位有效数字,所以完全正确
cout<<setprecision(15)<<PI<<endl; 
return 0;