使用 postgres tablefunc crosstab() 返回空的单个值
Posted
技术标签:
【中文标题】使用 postgres tablefunc crosstab() 返回空的单个值【英文标题】:Returning null individual values with postgres tablefunc crosstab() 【发布时间】:2018-06-19 19:35:53 【问题描述】:我正在尝试将 null 值合并到返回的列表中,这样:
batch_id |test_name |test_value
-----------------------------------
10 | pH | 4.7
10 | Temp | 154
11 | pH | 4.8
11 | Temp | 152
12 | pH | 4.5
13 | Temp | 155
14 | pH | 4.9
14 | Temp | 152
15 | Temp | 149
16 | pH | 4.7
16 | Temp | 150
会返回:
batch_id | pH |Temp
---------------------------------------
10 | 4.7 | 154
11 | 4.8 | 152
12 | 4.5 | <null>
13 | <null> | 155
14 | 4.9 | 152
15 | <null> | 149
16 | 4.7 | 150
但是,它目前返回这个:
batch_id | pH |Temp
---------------------------------------
10 | 4.7 | 154
11 | 4.8 | 152
12 | 4.5 | <null>
13 | 155 | <null>
14 | 4.9 | 152
15 | 149 | <null>
16 | 4.7 | 150
这是对先前问题的扩展 - Can the categories in the postgres tablefunc crosstab() function be integers? - 这导致了当前的查询:
SELECT *
FROM crosstab('SELECT lab_tests_results.batch_id, lab_tests.test_name, lab_tests_results.test_result::FLOAT
FROM lab_tests_results, lab_tests
WHERE lab_tests.id=lab_tests_results.lab_test AND (lab_tests.test_name LIKE ''Test Name 1'' OR lab_tests.test_name LIKE ''Test Name 2'')
ORDER BY 1,2'
) AS final_result(batch_id VARCHAR, test_name_1 FLOAT, test_name_2 FLOAT);
我也知道我不是第一个普遍提出这个问题的人,但我还没有找到适合这些情况的解决方案。例如,这个 - How to include null values in `tablefunc` query in postgresql? - 每次都假定相同的批次 ID。我不想指定批次 ID,而是指定所有可用的 ID。
这导致了我在那里找到的另一组解决方案,它们解决了来自指定类别的空列表结果。但是,由于我只是采用已经存在的东西,所以这不是问题。这是导致问题的空 individual 值并导致数据向左移动的数据透视表。
非常感谢任何建议!
编辑:在克林的帮助下,解决了。需要注意的是,VALUES 部分必须与您所追求的实际 lab_tests.test_name 值匹配,例如:
SELECT *
FROM crosstab(
$$
SELECT lab_tests_results.batch_id, lab_tests.test_name, lab_tests_results.test_result::FLOAT
FROM lab_tests_results, lab_tests
WHERE lab_tests.id = lab_tests_results.lab_test
AND (
lab_tests_results.lab_test = 1
OR lab_tests_results.lab_test = 2
OR lab_tests_results.lab_test = 3
OR lab_tests_results.lab_test = 4
OR lab_tests_results.lab_test = 5
OR lab_tests_results.lab_test = 50 )
ORDER BY 1 DESC, 2
$$,
$$
VALUES('Mash pH'),
('Sparge pH'),
('Final Lauter pH'),
('Wort pH'),
('Wort FAN'),
('Original Gravity'),
('Mash Temperature')
$$
) AS final_result(batch_id VARCHAR,
ph_mash FLOAT,
ph_sparge FLOAT,
ph_final_lauter FLOAT,
ph_wort FLOAT,
FAN_wort FLOAT,
original_gravity FLOAT,
mash_temperature FLOAT)
感谢您的帮助!
【问题讨论】:
【参考方案1】:使用second form of the function:
crosstab(text source_sql, text category_sql) - 生成一个“数据透视表”,其中包含由第二个查询指定的值列。
例如:
SELECT *
FROM crosstab(
$$
SELECT lab_tests_results.batch_id, lab_tests.test_name, lab_tests_results.test_result::FLOAT
FROM lab_tests_results, lab_tests
WHERE lab_tests.id=lab_tests_results.lab_test
AND (
lab_tests.test_name LIKE 'Test Name 1'
OR lab_tests.test_name LIKE 'Test Name 2')
ORDER BY 1,2
$$,
$$
VALUES('pH'), ('Temp')
$$
) AS final_result(batch_id VARCHAR, "pH" FLOAT, "Temp" FLOAT);
【讨论】:
以上是关于使用 postgres tablefunc crosstab() 返回空的单个值的主要内容,如果未能解决你的问题,请参考以下文章
Greenplum 的dblink与tablefunc模块安装