加入表格不在postgres中工作

时间:2017-09-14 07:16:12

标签: sql postgresql join casting

我将我的数据库(sql server)更改为postgres,但我以前使用的查询并不适用于postgres。加入不起作用

SELECT DISTINCT CONCAT(TOTALIZER_METER_READINGS.date,' ',TOTALIZER_METER_READINGS.time)
FROM TOTALIZER_METER_READINGS 
JOIN (
SELECT CONCAT(date,' ',MIN(time))::timestamp as mints FROM TOTALIZER_METER_READINGS    
WHERE date = '2017-08-20'
GROUP BY date, date_part('hour', time)      
)tt 
ON TOTALIZER_METER_READINGS.time = tt.mints AND TOTALIZER_METER_READINGS.date = tt.mints
ORDER BY 1 ASC

错误是:

  

错误:运算符不存在:没有时区的时间=没有时区的时间戳   第8行:ON TOTALIZER_METER_READINGS.time = tt.mints AND TOTALIZE ...   **********错误**********

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

您需要将不同列中的时间和日期值(为什么?)转换为单个时间戳,以便能够加入时间戳列。

{ "25-34": { "Clicks": 30, "Visits": 4 }, "45-54": { "Clicks": 12, "Visits": 6 }, } 值上添加time值会返回date,因此您可以使用:

timestamp

无关,但是:

ON (TOTALIZER_METER_READINGS.date + TOTALIZER_METER_READINGS.time) = tt.mints 用于连接字符串值,因此您(隐式)转换日期和时间。时间值到字符串。通常最好保留正确的数据类型,而不是将所有内容转换为字符串:

concat()

将保留返回正确的select distinct TOTALIZER_METER_READINGS.date + TOTALIZER_METER_READINGS.time 值,而不是timestamp

答案 1 :(得分:0)

根据你的错误行,你需要明确的演员:

SELECT DISTINCT  date || ' ' || FORMAT(to_char(TOTALIZER_METER_READINGS.time, 'HH')),
    TOTALIZER_METER_READINGS.Ia, TOTALIZER_METER_READINGS.Ib, TOTALIZER_METER_READINGS.Ic,
    TOTALIZER_METER_READINGS.Ineutral, TOTALIZER_METER_READINGS.Iang, TOTALIZER_METER_READINGS.Ibang,
    TOTALIZER_METER_READINGS.Icang, TOTALIZER_METER_READINGS.Va, TOTALIZER_METER_READINGS.Vb,
    TOTALIZER_METER_READINGS.Vc,TOTALIZER_METER_READINGS.Vaang, TOTALIZER_METER_READINGS.Vbang,
    TOTALIZER_METER_READINGS.Vcang,TOTALIZER_METER_READINGS.pftotal
    FROM TOTALIZER_METER_READINGS 
    JOIN (
    SELECT CONCAT(date,' ',MIN(time))::timestamp as mints FROM TOTALIZER_METER_READINGS    
    WHERE date = '2017-08-20'
    GROUP BY date, date_part('hour', time)      
)tt 
    ON TOTALIZER_METER_READINGS.time = tt.mints AND TOTALIZER_METER_READINGS.date = tt.mints
 ORDER BY 1 ASC

concat返回文字...