Can someone help me convert this sqlite query to a postgres query?
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
update, I found the answer:
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
The
strftimebusiness was taken care of elsewhere so we only have to sort out thedatetime(...)stuff here and compensate fordatebeing a timestamp. And I’ll switch to an explicit join condition (rather than the implicit one in the WHERE clause) while I’m here.PostgreSQL can compare timestamps with ISO8601 date strings on its own so you don’t need any casting or reformatting for the comparisons.
The two
count(*)still look a bit funny to me but I don’t know the context that the query is used in so the duplicates might make sense.