我尝试使用to_char格式化带时区的时间戳列,因为我不想包含列的区域部分,但是运行to_char且没有它的查询之间的区别是比如很长一段时间的10秒,我对数据库没有多少经验,也许我做错了。
查询没有to_char时间:1313毫秒:
select distinct on ("Results"."Timestamp") "Results"."Timestamp",
"TotalParticlesAccum", "BioAccumulated", "FlowVolume",
"DCOffsetCh0", "DCOffsetCh1", "DCOffsetCh2",
"LaserPower", "LaserCurrent", "LaserTemperature",
"LaserRunHour", "FlowRate", "FlowPressure",
"FlowTemperature", "CpuTemperature", "PwbTemperature",
"Temperature1", "Temperature2", "Temperature3",
"Temperature4", "TotalParticles", "Bio"
from "Results"
Left join "SensorLog" On "Results"."SampleID" = "SensorLog"."SampleID"
where "Results"."SampleID" = id order by 1 asc;
使用to_char时间查询:12354 ms
select distinct on (to_char("Results"."Timestamp",'YYYY/MM/DD HH24:MI:SS'))
to_char("Results"."Timestamp",'YYYY/MM/DD HH24:MI:SS'),
"TotalParticlesAccum", "BioAccumulated", "FlowVolume",
"DCOffsetCh0", "DCOffsetCh1", "DCOffsetCh2",
"LaserPower", "LaserCurrent", "LaserTemperature",
"LaserRunHour", "FlowRate", "FlowPressure",
"FlowTemperature", "CpuTemperature", "PwbTemperature",
"Temperature1", "Temperature2", "Temperature3",
"Temperature4", "TotalParticles", "Bio"
from "Results"
Left join "SensorLog" On "Results"."SampleID" = "SensorLog"."SampleID"
where "Results"."SampleID" = id order by 1 asc;
我想我知道问题是我有两次to_char,但如果我没有那个,那就给我一个错误 错误:SELECT DISTINCT ON表达式必须匹配初始ORDER BY表达式
答案 0 :(得分:1)
我的建议是使用raw字段进行分组和排序,但是在select子句中按照你想要的方式格式化它:
SELECT DISTINCT ON ("Timestamp")
to_char("Results"."Timestamp", 'YYYY/MM/DD HH24:MI:SS')
,"TotalParticlesAccum"
,"BioAccumulated"
,"FlowVolume"
,"DCOffsetCh0"
,"DCOffsetCh1"
,"DCOffsetCh2"
,"LaserPower"
,"LaserCurrent"
,"LaserTemperature"
,"LaserRunHour"
,"FlowRate"
,"FlowPressure"
,"FlowTemperature"
,"CpuTemperature"
,"PwbTemperature"
,"Temperature1"
,"Temperature2"
,"Temperature3"
,"Temperature4"
,"TotalParticles"
,"Bio"
FROM "Results"
Left join "SensorLog" On "Results"."SampleID" = "SensorLog"."SampleID"
WHERE "Results"."SampleID" = 839
ORDER BY "Timestamp" ASC
如果没有一个sqlfiddle我不会百分之百地说这会运行,但它可能是正确的选择。
答案 1 :(得分:1)
使用具有所需格式的快速查询包装快速查询
select
to_char("Timestamp",'YYYY/MM/DD HH24:MI:SS') as "Timestamp",
"TotalParticlesAccum", "BioAccumulated", "FlowVolume",
"DCOffsetCh0", "DCOffsetCh1", "DCOffsetCh2",
"LaserPower", "LaserCurrent", "LaserTemperature",
"LaserRunHour", "FlowRate", "FlowPressure",
"FlowTemperature", "CpuTemperature", "PwbTemperature",
"Temperature1", "Temperature2", "Temperature3",
"Temperature4", "TotalParticles", "Bio"
from (
select distinct on ("Results"."Timestamp")
"Results"."Timestamp",
"TotalParticlesAccum", "BioAccumulated", "FlowVolume",
"DCOffsetCh0", "DCOffsetCh1", "DCOffsetCh2",
"LaserPower", "LaserCurrent", "LaserTemperature",
"LaserRunHour", "FlowRate", "FlowPressure",
"FlowTemperature", "CpuTemperature", "PwbTemperature",
"Temperature1", "Temperature2", "Temperature3",
"Temperature4", "TotalParticles", "Bio"
from
"Results"
Left join
"SensorLog" On "Results"."SampleID" = "SensorLog"."SampleID"
where "Results"."SampleID" = id
order by 1 asc
) s
答案 2 :(得分:0)
时间戳是保留字;您无法在没有问题的情况下为列时间戳命名。尝试更改它。