【问题标题】:Hive Query to get records between two dates of string typeHive 查询以获取字符串类型的两个日期之间的记录
【发布时间】:2014-06-18 20:43:50
【问题描述】:

我正在尝试使用 Hue 上的字段 st_date 和 end_date 查询表。这些字段采用字符串类型值,例如'2014-04-04', '2009-10-10' 等。假设我想在st_date = 2014-04-04' and end_date = '2014-10-10' 之间查找包含两个日期的记录:

如何编写查询来检索记录WHERE st_date > 2014-04-03 and end_date < 2014-10-09

更具体地说,我遇到了与此查询中的日期转换相关的问题。

【问题讨论】:

标签: hive


【解决方案1】:

查询

   WHERE st_date > '2014-04-03' and end_date < '2014-10-11' 

应该给你想要的结果,因为即使它是一个刺,它也会按字典顺序进行比较,即 '2014-04-04' 总是大于 '2014-04-03'

我在我的示例表上运行它,它工作得非常好。

【讨论】:

    【解决方案2】:

    试试下面的查询。

    SELECT *
    FROM TABLE
    WHERE CAST(TRANSLATE(st_date,"-","") AS BIGINT) > CAST(TRANSLATE("2014-04-03","-","") AS BIGINT)  AND CAST(TRANSLATE(end_date,"-","") AS BIGINT) < CAST(TRANSLATE("2014-10-09","-","") AS BIGINT)
    

    【讨论】:

      【解决方案3】:
      hive> select * from salesdata01 where from_unixtime(unix_timestamp(Order_date, 'dd-MM-yyyy'),'yyyy-MM-dd') >= from_unixtime(unix_timestamp('2010-09-01', 'yyyy-MM-dd'),'yyyy-MM-dd') and from_unixtime(unix_timestamp(Order_date, 'dd-MM-yyyy'),'yyyy-MM-dd') <= from_unixtime(unix_timestamp('2011-09-01', 'yyyy-MM-dd'),'yyyy-MM-dd') limit 10;
      
      OK
      1   3   13-10-2010  Low 6.0 261.54  0.04    Regular Air -213.25 38.94
      80  483 10-07-2011  High    30.0    4965.7593   0.08    Regular Air 1198.97 195.99
      97  613 17-06-2011  High    12.0    93.54   0.03    Regular Air -54.04  7.3
      98  613 17-06-2011  High    22.0    905.08  0.09    Regular Air 127.7   42.76
      103 643 24-03-2011  High    21.0    2781.82 0.07    Express Air -695.26 138.14
      127 807 23-11-2010  Medium  45.0    196.85  0.01    Regular Air -166.85 4.28
      128 807 23-11-2010  Medium  32.0    124.56  0.04    Regular Air -14.33  3.95
      160 995 30-05-2011  Medium  46.0    1815.49 0.03    Regular Air 782.91  39.89
      229 1539    09-03-2011  Low 33.0    511.83  0.1 Regular Air -172.88 15.99
      230 1539    09-03-2011  Low 38.0    184.99  0.05    Regular Air -144.55 4.89
      Time taken: 0.166 seconds, Fetched: 10 row(s)
      
      hive> select * from salesdata01 where from_unixtime(unix_timestamp(Order_date, 'dd-MM-yyyy'),'yyyy-MM-dd') >= from_unixtime(unix_timestamp('2010-09-01', 'yyyy-MM-dd'),'yyyy-MM-dd') and from_unixtime(unix_timestamp(Order_date, 'dd-MM-yyyy'),'yyyy-MM-dd') <= from_unixtime(unix_timestamp('2010-12-01', 'yyyy-MM-dd'),'yyyy-MM-dd') limit 10;
      
      OK
      1   3   13-10-2010  Low 6.0 261.54  0.04    Regular Air -213.25 38.94
      127 807 23-11-2010  Medium  45.0    196.85  0.01    Regular Air -166.85 4.28
      128 807 23-11-2010  Medium  32.0    124.56  0.04    Regular Air -14.33  3.95
      256 1792    08-11-2010  Low 28.0    370.48  0.04    Regular Air -5.45   13.48
      381 2631    23-09-2010  Low 27.0    1078.49 0.08    Regular Air 252.66  40.96
      656 4612    19-09-2010  Medium  9.0 89.55   0.06    Regular Air -375.64 4.48
      769 5506    07-11-2010  Critical    22.0    129.62  0.05    Regular Air 4.41    5.88
      1457    10499   16-11-2010  Not Specified   29.0    6250.936    0.01    Delivery Truck  31.21   262.11
      1654    11911   10-11-2010  Critical    25.0    397.84  0.0 Regular Air -14.75  15.22
      2323    16741   30-09-2010  Medium  6.0 157.97  0.01    Regular Air -42.38  22.84
      Time taken: 0.17 seconds, Fetched: 10 row(s)
      

      【讨论】:

      • 请添加一些文字,解释您的示例会话显示的内容以及它与问题的关系
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-02-11
      • 2019-03-14
      • 1970-01-01
      • 2017-05-02
      • 1970-01-01
      • 1970-01-01
      • 2012-10-17
      相关资源
      最近更新 更多