javascript - 从不规则的日期行创建连续数据集

标签 javascript python sqlalchemy d3.js postgresql-9.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 上玩一个实例

最佳答案

您想要generate_series 一组覆盖目标区域的日期,然后针对它左外连接您的交易表。参见 this SQLFiddle example .

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;

您可以根据 this 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;

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

关于javascript - 从不规则的日期行创建连续数据集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14926995/

相关文章:

python - NOT NULL 约束失败但字段不为空

python - Alembic Django Flask 迁移数据库

python - 在 Sqlalchemy 查询中忽略 MYSQL 数据库中的锁

javascript - 从 Javascript 中运行的代码访问变量 "eval"

python - 如何从 scipy 结果中提取 p 值

javascript - 延迟加载 : progressive vs on-demand

python - 标准化特定语言的发布/工具组

python - 确定从 spacy 中提取的文本是否是一个完整的句子

javascript - Sequelize 和 Node.js : Initializing Multiple Databases On One Server Programmatically

javascript - 无法读取未定义的属性 "replace"