我正在尝试使用表字段和值
返回一个函数我有这样的功能:
CREATE FUNCTION books(
id integer)
RETURNS TABLE(
id integer,
title text,
type text
)
LANGUAGE 'sql'
AS $BODY$
WITH RECURSIVE chain(id, seq) AS (
SELECT s.id, s.sequence
FROM product s
WHERE s.id = $1
UNION ALL
SELECT s.id, s.sequence
FROM product s, chain t
WHERE s.id = t.id
)
select * from chain
$BODY$;
我的查询目前正在返回,如下所示
1, book title1, new
2, book title2, new
3, book title3, old
我希望返回类似
的内容id: 1, title: book title1, type: new
id: 2, title: book title2, type: new
id: 3, title: book title3, type: old
所以我有关键来映射api方面的值。
有可能吗?
非常感谢!
更新
谢谢,递归后的实际select语句类似于:
SELECT c.id, c.title, cu.name
FROM customer cu, book b
WHERE cu.id = b.id
UNION
(
SELECT c.id, c.title, f.cost
FROM chain c, foo f
WHERE c.id = f.id
)
如何使用row_to_json
来隐藏它们?
答案 0 :(得分:1)
一种方法是将行转换为json:
In [1]: import sys
...: import numpy as np
...:
In [2]: print(sys.version)
...:
3.6.2 |Continuum Analytics, Inc.| (default, Jul 20 2017, 13:14:59)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)]
In [3]: print(np.__version__)
...:
1.12.1
In [4]: # Method 1 (Original)
...: def transform1(matrix):
...: mat = np.asarray(matrix)
...: mat[np.logical_not(np.not_equal(mat, 0).cumprod(axis=0))] = 0
...: return mat
...:
In [5]: # Method 2:
...: def transform2(matrix):
...: mat = np.asarray(matrix)
...: mat *= (mat != 0).cumprod(axis=0, dtype=np.bool)
...: return mat
...:
In [6]: # @DSM method:
...: def transform_DSM(matrix):
...: mat = np.asarray(matrix)
...: mat *= np.minimum.accumulate(mat != 0)
...: return mat
...:
In [7]: # @DanielF method:
...: def transform_DanielF(matrix):
...: mat = np.asarray(matrix)
...: mat[~np.logical_and.accumulate(mat, axis = 0)] = 0
...: return mat
...:
In [8]: # Optimized @DanielF method:
...: def transform_DanielF_optimized(matrix):
...: mat = np.asarray(matrix)
...: mat *= np.logical_and.accumulate(mat, dtype=np.bool)
...: return mat
...:
In [9]: matrix = np.random.randint(0, 20000, (20000, 20000))
In [10]: %timeit -n1 transform1(matrix)
22.1 s ± 241 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
In [11]: %timeit -n1 transform2(matrix)
9.29 s ± 185 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
In [12]: %timeit -n1 transform3(matrix)
9.23 s ± 180 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
In [13]: %timeit -n1 transform_DSM(matrix)
9.24 s ± 195 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
In [14]: %timeit -n1 transform_DanielF(matrix)
10.3 s ± 219 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
In [15]: %timeit -n1 transform_DanielF_optimized(matrix)
9.27 s ± 187 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)