Get created as well as deleted entries of last week

后端 未结 2 533
星月不相逢
星月不相逢 2021-01-27 07:26

With this query I can get all entries created during the last week:

SELECT day, COALESCE(ct, 0) AS ct
FROM  (SELECT now::date - d AS day FROM generate_series (0,         


        
2条回答
  •  独厮守ぢ
    2021-01-27 08:29

    At first glance it looks like your performing a count function rather than a sum which is what you need, your simply just counting each record twice.

    sum( CASE WHEN (created_at >= date_trunc('day', now()) - interval '6d') 
           THEN 1 ELSE 0 END) AS ct,
    sum(CASE WHEN (canceled_at >= date_trunc('day', now()) - interval '6d') 
           THEN 1 ELSE 0 END) AS dl  
    

    You need to use sum which will then add all the cases where you case when is returning 1 rather than count which just counts all the values regardless whether they are 1 or 0!

提交回复
热议问题