将sqlite转换为postgres查询

时间:2011-08-25 17:54:06

标签: sql postgresql sqlite

有人可以帮助我将此sqlite查询转换为postgres查询吗?

SELECT count(*), count(*), date(date, '-'||strftime('%w',date)||' days') as date
FROM emails as m, contacts as me
WHERE datetime(date) > datetime('2010-08-25')
  and datetime(date) < datetime('2011-08-25')
  and (me.id = m.fr)
  and me.email like '%gmail.com%'
GROUP BY date
ORDER BY date asc

更新,我找到了答案:

select count(*), (m.date::date - extract(dow from m.date)::int) as dat
from emails as m join contacts as me on m.fr = me.id
where m.date > '2010-08-25'
  and m.date < '2011-08-25'
  and me.email like '%gmail.com%'
group by dat
order by dat

1 个答案:

答案 0 :(得分:1)

strftime业务由elsewhere处理,因此我们只需要在此处整理datetime(...)内容并补偿date作为时间戳。当我在这里时,我将切换到显式连接条件(而不是WHERE子句中的隐式连接条件)。

select count(*), count(*), m.date::date - extract(dow from m.date)::int as date
from emails as m join contacts as me on m.fr = me.id
where m.date > '2010-08-25'
  and m.date < '2011-08-25'
  and me.email like '%gmail.com%'
group by m.date
order by m.date asc

PostgreSQL可以将时间戳与ISO8601日期字符串进行比较,因此您无需进行任何转换或重新格式化比较。

这两个count(*)对我来说看起来仍然有点滑稽,但我不知道查询的上下文所以重复可能有意义。