1

我有这张桌子:

                                 Table "public.transaction"
   Column   |            Type             |                        Modifiers                         
------------+-----------------------------+----------------------------------------------------------
 id         | integer                     | not null default nextval('transaction_id_seq'::regclass)
 account_id | integer                     | 
 note       | character varying           | 
 date       | timestamp without time zone | 
 amount     | numeric                     |

它包含以下格式的交易:

 id | account_id |               note               |        date         | amount 
----+------------+----------------------------------+---------------------+--------
  1 |          1 | Loopia AB                        | 2013-02-07 00:00:00 |   -178
  2 |          1 | ÅSGATAN 2 KÖK &                  | 2013-02-07 00:00:00 |   -226
  3 |          1 | BURGER KING ODEN                 | 2013-02-06 00:00:00 |    -89
  4 |          1 | OLEARYS 917                      | 2013-02-06 00:00:00 |   -309
  5 |          1 | TAXI STOCKHOLM                   | 2013-02-06 00:00:00 |   -875
  6 |          1 | GRET INDIAN REST                 | 2013-02-06 00:00:00 |    -85
  8 |          1 | VIDEO RULLEN                     | 2013-02-04 00:00:00 |   -169
  9 |          1 | ICA SUPERMARKET                  | 2013-02-04 00:00:00 |   -196
 10 |          1 | ICA SUPERMARKET                  | 2013-02-03 00:00:00 |   -110

然后我以以下格式将数据提供给 D3:

[
    {
        "note": "TEXAS LONGHORN",
        "date": "2013-01-10T00:00:00",
        "amount": 110,
        "id": 74,
        "account_id": 1
    },
    {
        "note": "GOOGLE *FEO Medi",
        "date": "2013-01-10T00:00:00",
        "amount": 22,
        "id": 73,
        "account_id": 1
    },
    {
        "note": "Pressbyran 5122",
        "date": "2013-01-10T00:00:00",
        "amount": 13,
        "id": 77,
        "account_id": 1
    },
    {
        "note": "ICA SUPERMARKET",
        "date": "2013-01-10T00:00:00",
        "amount": 106,
        "id": 76,
        "account_id": 1
    },
    {
        "note": "HÅR 3000",
        "date": "2013-01-10T00:00:00",
        "amount": 345,
        "id": 75,
        "account_id": 1
    },
    {
        "note": "Pressbyran 5122",
        "date": "2013-01-11T00:00:00",
        "amount": 19,
        "id": 72,
        "account_id": 1
    },
    {
        "note": "BIRKA PUNKTEN",
        "date": "2013-01-11T00:00:00",
        "amount": 79,
        "id": 71,
        "account_id": 1
    }
]

然而,D3 流图要求所有数据点都存在。因此,我必须将所有日期,即使是那些没有任何交易的日期,都放在我提供给 D3 的数据中。

我希望您能就如何使用任何可用的工具有效地实现这一点提供意见。您可以在http://bl.ocks.org/joar/4747134/a702cf79bf10b1438cc665a2438b3f5cf9ab8bf0上玩一个活生生的例子

4

1 回答 1

3

您想要generate_series一组日期覆盖目标区域,然后将您的事务表左外连接到它。请参阅此 SQLFiddle 示例

SELECT
  x.gendate,
  t.account_id, t.id, t.note, t.amount
FROM 
  generate_series(
    (SELECT min("date") FROM transaction),
    (SELECT max("date") FROM transaction),
    INTERVAL '1' DAY
  ) AS x(gendate)
  LEFT OUTER JOIN transaction t ON (t."date" = x.gendate)
ORDER BY x.gendate;

您可以按照SQLFiddle使用 PostgreSQL 的 json 函数生成所需的数据格式。

WITH continuous_tx AS (
  SELECT
    x.gendate AS "date",
    t.account_id, t.id, t.note, t.amount
  FROM 
    generate_series(
      (SELECT min("date") FROM transaction),
      (SELECT max("date") FROM transaction),
      INTERVAL '1' DAY
    ) AS x(gendate)
    LEFT OUTER JOIN transaction t ON (t."date" = x.gendate)
  ORDER BY x.gendate
)
SELECT array_to_json(array_agg(continuous_tx ),'t')
FROM continuous_tx;

...虽然我还没有测试将它输入到绘图工具中。

于 2013-02-17T23:23:13.050 回答