将 Bigint 数据转换为人类可读的日期时间在 Sql

huangapple go评论74阅读模式
英文:

Convert Bigint data to human readable date-time in Sql

问题

我已创建一个MySql表,并使用以下代码在其中填充数据

CREATE TABLE IF NOT EXISTS DB (
    INN VARCHAR(200) NOT NULL, 
    Time BIGINT not NULL
);

现在我想要获取一个表,按INN分组时报告Time的最大值和最小值。以下是我的代码 -

SELECT INN, from_unixtime(MIN(Time)), from_unixtime(MAX(Time)) FROM DB GROUP BY INN

我希望将Time的值报告为普通日期时间,而不是Epoch时间。但是,使用上面的代码,我得到了<NA>值。

典型的Time值类似于1546380001264082944

请问有人可以帮助我获取正确的代码以实现相同的目标吗?

英文:

I have created a MySql table and feed data therein with below code

CREATE TABLE IF NOT EXISTS DB (
    INN VARCHAR(200) NOT NULL, 
    Time BIGINT not NULL
);

Now I want to get a table which will report the Maximum and Minimum values of Time when grouped by INN. Below is my code -

SELECT INN, from_unixtime(MIN(Time)), from_unixtime(MAX(Time)) FROM DB GROUP BY INN

I want to get the Time values reported as normal date-time instead of Epoch time. But with above code, I am getting <NA> values.

A typical Time value is like 1546380001264082944

Can someone please help me to get the correct code to achieve the same.

答案1

得分: 1

问题在于您使用的Unix时间戳的精度。

考虑以下内容:

SELECT FROM_UNIXTIME(1546380001), FROM_UNIXTIME(1546380001264082944)

输出结果为:

2019-01-01T22:00:01Z	(null)

您所拥有的时间戳值1546380001264082944包含了超出MySQL接受范围的精度。

FROM_UNIXTIME的定义如下:

FROM_UNIXTIME(unix_timestamp[,format])

文档说明:

unix_timestamp是一个内部的时间戳值,表示自'1970-01-01 00:00:00' UTC以来的秒数

您的时间戳精度远远超过了Unix纪元以来的秒数。

文档可以在这里找到。

英文:

The problem here is to do with the precision of the unix timestamp you are using.

Consider this:

SELECT FROM_UNIXTIME(1546380001), FROM_UNIXTIME(1546380001264082944)

The output is:

2019-01-01T22:00:01Z	(null)

The timestampt value you have, 1546380001264082944, contains a level of precision beyond that accepted by MySQL.

The definition of FROM_UNIXTIME is:

FROM_UNIXTIME(unix_timestamp[,format])

The doc states:

> unix_timestamp is an internal timestamp value representing seconds
> since '1970-01-01 00:00:00' UTC

The precision of your timestamp is significantly greater than seconds since the Unix Epoch.

The docs are available here.

答案2

得分: 0

The value 1546380001264082944 is too big to be epoch seconds or even milliseconds. This is easily verified by putting this value on <https://currentmillis.com/>.

你所提供的值 1546380001264082944 太大了,不可能是秒或毫秒的时间戳。你可以在 https://currentmillis.com/ 上验证这一点。

You have stored precision upto a nanosecond. So, divide the column value by 1e9 before passing them to from_unixtime.

你已经存储了纳秒级的精度。因此,在传递给 from_unixtime 函数之前,将列的值除以 1e9。


SELECT INN, from_unixtime(MIN(Time) / 1000000000), from_unixtime(MAX(Time) / 1000000000)
FROM DB
GROUP BY INN


SELECT INN, from_unixtime(MIN(Time) / 1000000000), from_unixtime(MAX(Time) / 1000000000)
FROM DB
GROUP BY INN

英文:

The value 1546380001264082944 is too big to be epoch seconds or even milliseconds. This is easily verified by putting this value on <https://currentmillis.com/>.

You have stored precision upto a nanosecond. So, divide the column value by 1e9 before passing them to from_unixtime.


SELECT INN, from_unixtime(MIN(Time) / 1000000000), from_unixtime(MAX(Time) / 1000000000)
FROM DB
GROUP BY INN

huangapple
  • 本文由 发表于 2020年1月3日 19:15:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/59577624.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定