1

我有一个 PostgreSQL 数据库,其中包含一个test_table包含单个记录的表。第一列是简单的store_id,第二列meausurement是嵌套的 json。

store_id | measurement
----------------------
0        | {...}

列的格式measurement如下:

{
    'file_info': 'xxxx', 
    'data': {
        'contour_data': {
            'X': [-97.0, -97.0, -97.0, -97.0, -97.0, -97.0],
            'Y': [-43.0, -41.0, -39.0, -39.0, -38.0, -36.0]
        }
    }
}

我想在 Tableau 的散点图中绘制Yvs。X因此,我使用 Tableau 的 PostgreSQL 连接器成功连接了数据库。从这个页面我了解到,我必须使用自定义 SQL 查询从 json 对象中提取数据,因为 Tableau 不直接支持jsonPostgres 的数据类型。我已经在 Tableau 中尝试了以下自定义 SQL 查询:

select
    store_id as store_id,
    measurement#>>'{data, contour_data, X}' as contour_points_x,
    measurement#>>'{data, contour_data, Y}' as contour_points_y
from test_table

它成功地将两个数组提取到两个新列contour_points_xcontour_points_y. 但是,这两个新列都在 Tableau 类型string中,因此我不能将它们用作绘图的数据源。

如何调整自定义 SQL 查询以使数据数组可在 Tableau 散点图中绘制?

4

2 回答 2

2

看起来您需要拆分列。检查此https://help.tableau.com/current/pro/desktop/en-us/split.htm

编辑 - 当您可以可靠地假设每个列表中点数的上限时,链接方法有效。此处描述了一种拆分任意大小列表的方法https://apogeeintegration.com/blog/apogee-busts-out-multi-value-cells-using-tableau-prep-builder

于 2020-06-17T14:34:55.700 回答
0

答案是几个函数和/或语法操作的串联。一个必须

  • 使用#>运算符挖掘json并将其作为json类型返回(而不是作为text类型返回>>#)。
  • 用于json_array_elements_text()将 扩展json为一组text.
  • 使用类型转换运算符::转换textfloat
/* custom SQL Query in Tableau */
select 
    store_id as store_id,
    json_array_elements_text(measurement#>'{data, contour_data, X}')::float as contour_points_x,
    json_array_elements_text(measurement#>'{data, contour_data, Y}')::float as contour_points_y,
from test_table

两个结果列现在都作为离散度量显示在 Tableau 工作表中。更改为离散尺寸允许根据需要绘制contour_points_ycontour_points_x

于 2020-06-25T08:18:02.877 回答