0

小提琴

我需要做一个 JasperReport。weekly intervals我需要显示的是帐户进程的总数,分为numberofactivateddeclined帐户。

对于到目前为止我得到的每周间隔查询:

SELECT *
FROM account_details
WHERE DATE date_opened = DATE_ADD(2014-01-01, INTERVAL(1-DAYOFWEEK(2014-01-01)) +1 DAY)

这似乎是正确的,但并不POSTGRES正确。它一直在抱怨1-DAYOFWEEK. 这是我希望实现的目标:

在此处输入图像描述

更新 它很丑陋,但我不知道有什么更好的。Id 完成了这项工作。但不知道它是否可以重新分解,至少看起来更好。我现在也不知道如何处理除以零。

        SELECT to_char(d.day, 'YYYY/MM/DD  -  ') || to_char(d.day + 6, 'YYYY/MM/DD') AS Month
         , SUM(CASE WHEN LOWER(situation) LIKE '%active%' THEN 1 ELSE 0 END)     AS Activated
         , SUM(CASE WHEN LOWER(situation) LIKE '%declined%' THEN 1 ELSE 0 END) AS Declined
         , SUM(CASE WHEN LOWER(situation) LIKE '%declined%' OR LOWER(situation) LIKE '%active%' THEN 1 ELSE 0 END) AS Total
         , to_char( 100.0 *( (SUM(CASE WHEN LOWER(situation) LIKE '%active%' THEN 1 ELSE 0 END)) / (SUM(CASE WHEN LOWER(situation) LIKE '%declined%' OR LOWER(situation) LIKE '%active%' THEN 1 ELSE 0 END))::real) , '99.9') AS percent_activated
         , to_char( 100.0 *( (SUM(CASE WHEN LOWER(situation) LIKE '%declined%' THEN 1 ELSE 0 END)) / (SUM(CASE WHEN LOWER(situation) LIKE '%declined%' OR LOWER(situation) LIKE '%active%' THEN 1 ELSE 0 END))::real) , '99.9') AS percent_declined
    FROM   (
       SELECT day::date
       FROM   generate_series('2014-08-01'::date, '2014-09-14'::date, interval '1 week') day
       ) d
    JOIN   account_details a ON a.date_opened >= d.day 
                            AND a.date_opened <  d.day + 6
    GROUP  BY d.day;
4

1 回答 1

1
SELECT to_char(d.day, 'YYYY/MM/DD" - "')
    || to_char(d.day + 6, 'YYYY/MM/DD') AS week
     , count(situation ILIKE '%active%' OR NULL) AS activated
     , ...
FROM   (
   SELECT day::date
   FROM   generate_series('2014-08-11'::date
                        , '2014-09-14'::date
                        , '1 week'::interval) day
   ) d
LEFT   JOIN account_details a ON a.date_opened >= d.day 
                             AND a.date_opened <  d.day + 7  -- 7, not 6!
GROUP  BY d.day;

相关答案:

有关计算特定值的更多信息:

旁白:您通常会使用一个enum或一个查找表,并且只存储一个 ID situation,而不是冗余的冗长文本。

于 2014-09-15T17:30:35.503 回答