一些presto语句记录

1、找出时间大于2018-12-17的所有内容

select * from table where "time" > date('2018-12-17') 

与关系型数据库不同的是完全不同类型的不可以比较,类似的才可以比较,关系型数据库的

2、针对完全不同类型的进行比较,也可以使用cast函数,用法:cast(value as type)

比如      select * from table where "time" > cast('2018-12-17' as date)  limit 100,

3、表示日期的函数:
   (1)now()  精确到今天的时分秒

   (2) current_date 精确到今天的年月日

   (3) current_date - interval '1' day, 精确到昨天的年月日

   (4)日期类型不能与字符串比较,运行错误,需要显式转换
   (5)字符串格式化成日期格式

           date_parse('2018-12-17 12:12:12', '%Y-%m-%d %H:%i:%s'), 

           cast('2018-09-09' as date),

           cast('2018-09-09 12:12:12' as timestamp),

   (6) 日期转指定格式化字符串
           date_format(now(), '%Y-%m-%d %H:%i:%s')

    (7)转换为时间戳

       to_unixtime(cast ('2018-12-18 09:18:09.000' as timestamp))   

 关于时间的文档:https://prestodb.io/docs/current/functions/datetime.html

4、一些presto在实际上的应用

比如:某段时间内 获取金币数的人数分布,0~1000金币的用户数 有多少人  1001~5000金币的有多少人

  SELECT count(1)
    ,col
FROM (
    SELECT "user_id"
        ,CASE 
            WHEN total< 500
                THEN '0-500'
            WHEN total>= 500
                AND total < 1000
                THEN '500-1000'
            ELSE '>1000'
            END col
    FROM (
        SELECT "user_id"
            ,sum(count) AS total_coin
        FROM event
        WHERE "date" >= '2018-11-01'
            AND "event" = 'consume_coin'
        GROUP BY "user_id"
        ) a
    ) b
GROUP BY col

结果如下:

 


转载来自:https://blog.csdn.net/c13232906050/article/details/82623028 ,新加了一些生活中了一些应用