我有这张桌子:
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
处播放实时示例答案 0 :(得分:3)
您希望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;
...虽然我没有测试过将它喂入图形工具。