如何从同一行中加/减时间值

时间:2017-10-18 00:17:10

标签: postgresql time sum subtraction

我想要求和减去两个或更多时间戳列。

我正在使用PostgreSQL,我有一个结构,你可以看到: enter image description here

我无法计算分钟或秒数,因此我尝试提取EPOCH并在之后执行操作,但我总是收到错误,因为第一个EXTRACT识别出列,但是当我将第二个EXTRACT放在同一个SQL命令中时,我收到一条错误消息,指出第二列不存在。

我会举个例子:

SELECT 
    EXAMPLE.PERSON_ID,
    COALESCE(EXTRACT(EPOCH from EXAMPLE.LEFT_AT),0) +
    COALESCE(EXTRACT(EPOCH from EXAMPLE.ARRIVED_AT),0) AS CREDIT
FROM
    EXAMPLE
WHERE 
    EXAMPLE.PERSON_ID = 1;

在这个例子中,我会收到如下错误:

  

列ARRIVED_AT不存在

为什么会这样? 我可以从同一行中加/减时间值吗?

1 个答案:

答案 0 :(得分:1)

let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45Wcs/PT89JLchJrVDSUTI21zMxMjIwMACydQ2NjPQMLEwMTM2VYnWilRwLgIoUPPPSMvMyS1IVfPLzCyA6jI0NzczN4DqMDQwtLJViYwE=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Place = _t, Lat = _t, Long = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"Place", type text}}), #"Invoked Custom Function" = Table.AddColumn(#"Changed Type", "GetCountry", each GetCountry([Lat], [Long])), #"Expanded GetCountry" = Table.ExpandRecordColumn(#"Invoked Custom Function", "GetCountry", {"resourceSets"}, {"GetCountry.resourceSets"}), #"Expanded GetCountry.resourceSets" = Table.ExpandListColumn(#"Expanded GetCountry", "GetCountry.resourceSets"), #"Expanded GetCountry.resourceSets1" = Table.ExpandRecordColumn(#"Expanded GetCountry.resourceSets", "GetCountry.resourceSets", {"resources"}, {"resources"}), #"Expanded resources" = Table.ExpandListColumn(#"Expanded GetCountry.resourceSets1", "resources"), #"Expanded resources1" = Table.ExpandRecordColumn(#"Expanded resources", "resources", {"address"}, {"address"}), #"Expanded address" = Table.ExpandRecordColumn(#"Expanded resources1", "address", {"countryRegion"}, {"countryRegion"}) in #"Expanded address" 是计算值而不是列吗?您运行了什么来获取您发布的查询结果图像显示这些列?

以下脚本可以满足您的期望,因此您可以查询表格的结构,不是您期望的结果。

ARRIVED_AT