【问题标题】:rest every two rows of the same column in sql and and convert the result in seconds在sql中每两行的同一列休息,并在几秒钟内转换结果
【发布时间】:2019-10-17 02:19:30
【问题描述】:

我的 Hive 表需要帮助。

   DATE               |    ID     |    RESTDATESINSECONDS     

 2019-03-28 10:05:27        1             (2019-03-28 10:05:38)-(2019-03-28 10:05:27)
 2019-03-28 10:05:38        1             (2019-03-28 10:14:14)- (2019-03-28 10:05:38)
 2019-03-28 10:14:14        1             (2019-03-28 10:14:16) -(2019-03-28 10:14:14)
 2019-03-28 10:14:16        1             (2019-03-28 10:14:46) -(2019-03-28 10:14:16)
 2019-03-28 10:14:46        1             (2019-03-28 10:15:30) -(2019-03-28 10:15:30)
 2019-03-28 10:15:30        1                """
     """                    """              """

我有二十万行。我想将其返回为:

   DATE               |    ID     |    RESTDATESINSECONDS     

 2019-03-28 10:05:27        1             11
 2019-03-28 10:05:38        1             516
 2019-03-28 10:14:14        1             2
 2019-03-28 10:14:16        1             30
 2019-03-28 10:14:46        1             44
 2019-03-28 10:15:30        1             ""

我尝试了各种括号和计数,但我尝试的每件事都会引发不同的语法错误!有人有什么想法吗?

非常感谢:)

【问题讨论】:

  • (1) 您使用的是 SQL Server 还是 HQL? (2)RESTDATESINSECONDS的数据类型是什么?
  • 嗨@GordonLinoff!我正在使用 HQL,“RESTDATESINSECONDS”的格式将是 int。谢谢:)

标签: sql hive hql hiveql


【解决方案1】:
with cte_test_table
as
(select * from
  ( values('2019-03-28 10:05:27',1)
         ,('2019-03-28 10:05:38',1)
         ,('2019-03-28 10:14:14',1)
         ,('2019-03-28 10:14:16',1)
         ,('2019-03-28 10:14:46',1)
         ,('2019-03-28 10:15:30',1)
  ) as t([Date],ID)
)

select ID, [Date]
,datediff(ss,[Date],LAG ([Date], 1, [Date]) over (partition by ID order by [Date] desc)) as RESTDATESINSECONDS
from cte_test_table
order by [Date]

【讨论】:

    【解决方案2】:

    如果不保证您想要时差的行是连续的,您可以这样做(在任何情况下都有效):

    select 
      t.id,
      t.date,
      datediff(
        s, 
        t.date, 
        (select min(date) from tablename where id = t.id and date > t.date)
      ) restdateinseconds
    from tablename t 
    

    请参阅demo
    结果:

    > id | date                | restdateinseconds
    > -: | :------------------ | ----------------:
    >  1 | 28/03/2019 10:05:27 |                11
    >  1 | 28/03/2019 10:05:38 |               516
    >  1 | 28/03/2019 10:14:14 |                 2
    >  1 | 28/03/2019 10:14:16 |                30
    >  1 | 28/03/2019 10:14:46 |                44
    >  1 | 28/03/2019 10:15:30 |              
    

    【讨论】:

      【解决方案3】:

      使用lead() 函数获取下一行dateunix_timestamp() 将日期转换为秒,然后减去:

      with test_data as (
      select stack(6,
      '2019-03-28 10:05:27',
      '2019-03-28 10:05:38',
      '2019-03-28 10:14:14',
      '2019-03-28 10:14:16',
      '2019-03-28 10:14:46',
      '2019-03-28 10:15:30') as `date`
      )
      
      select `date`, unix_timestamp(lead(`date`) over(order by `date`)) - unix_timestamp(`date`) as restdateinseconds
        from test_data;
      

      返回:

      date                 restdateinseconds
      2019-03-28 10:05:27    11   
      2019-03-28 10:05:38    516  
      2019-03-28 10:14:14    2    
      2019-03-28 10:14:16    30   
      2019-03-28 10:14:46    44   
      2019-03-28 10:15:30    NULL 
      

      【讨论】:

        猜你喜欢
        • 2020-10-15
        • 2016-06-25
        • 1970-01-01
        • 2020-04-09
        • 2012-07-14
        • 2014-04-12
        • 1970-01-01
        • 2020-08-23
        • 1970-01-01
        相关资源
        最近更新 更多