2011-09-09 95 views
1

我有另一個需要轉換爲postgres的sqlite腳本。採取甘德,如果你能幫助:)將sqlite轉換爲postgres

SELECT count(*), strftime('%H', sentdate) as hour FROM latency l, contacts me 
    WHERE l.lat < 1 and datetime(sentdate) > datetime('2009-01-01') and datetime(sentdate)  
     < datetime('2011-02-01') and (me.id = l.replyuid or me.id = l.senduid) 
    GROUP BY hour ORDER BY hour asc; 

回答

1

假設sentdate是日期/時間戳字段那麼這應該工作:

SELECT COUNT(*), 
    date_part('hour', sentdate) AS hour 
FROM latency l, 
    contacts me 
WHERE l.lat < 1 
    AND date_trunc ('day', sentdate) > DATE ('2009-01-01') 
    AND date_trunc ('day', sentdate) < DATE ('2011-02-01') 
    AND (me.id = l.replyuid 
     OR me.id = l.senduid) 
GROUP BY date_part('hour', sentdate) 
ORDER BY hour ASC; 
+0

如果我寫此查詢,我可能會嘗試做這個一個內部連接(而不是逗號連接),並且由於在連接條件中有一個'or',我將它表示爲一個聯合,相同的查詢,但也許更習慣於SQL。 – SingleNegationElimination

+0

@TokenMacGuy - 夠真實的。我非常喜歡ANSI加入,但是我試圖不偏離原始查詢太遠... – gsiems