我需要一个简单的函数来返回动态的列集.我在SO上找到了几个例子并最终得到以下内容:
CREATE or replace FUNCTION getColumn(_column1 text, _column2 text, _column3 text, _table text)
RETURNS TABLE(cmf1 text, cmf2 text, cmf3 text) AS $$
BEGIN
RETURN QUERY EXECUTE
'SELECT '
|| quote_ident(_column1)::text || ' as cmf1,'
|| quote_ident(_column2)::text || ' as cmf2,'
|| quote_ident(_column3)::text || ' as cmf3'
' FROM '
|| quote_ident(_table);
END;
$$ LANGUAGE plpgsql;
Run Code Online (Sandbox Code Playgroud)
我需要这个函数只能用于varchar/text列,所以我创建了这个测试表:
create table test20130205 (
a text,
b text,
c varchar,
d text)
;
Run Code Online (Sandbox Code Playgroud)
最后,我可以运行一些测试:
select * from getColumn('a','b','d','test20130205');
-- ok
select * from getColumn('a','b','c','test20130205'); …Run Code Online (Sandbox Code Playgroud) 我应该使用cast(x as typename)或x::typename在PostgreSQL的?
x::typename除了文档中的一个句子之外,我无法找到任何关于强制转换版本的文档(" 默认情况下,只能通过显式强制转换请求调用强制转换,即显式CAST(x AS类型名称)或x :: typename构造".
唯一不同的cast(x as typename)是sql-standard而x::typename不是?
我总是喜欢x::typename它,因为它更短.
我有以下功能,其中只有一个参数,df.df是数据帧:
test_function <- function(df) {
df_name <- df #get name of dataframe (does not work)
df_name
}
test_function(mtcars)
Run Code Online (Sandbox Code Playgroud)
如何从此函数返回数据集的名称?对于test_function(mtcars)我需要分配字符串mtcars到df_name.
R中是否有任何整数分区的实现?
例如对于输入4我想得到5个向量:
4
3 , 1
2 , 2
2 , 1 , 1
1 , 1 , 1 , 1
Run Code Online (Sandbox Code Playgroud)
有Python,Erlang,Java,C,Perl的实现,但我在R中找不到任何东西.
有没有办法如何将以下字符串转换回人类可读的值?我有一些外部数据,其中所有非ascii字符都被转义.
示例字符串:
16 StringProvider_111=Telefon\u00ED kontakty
17 StringProvider_116=Odpov\u011Bdn\u00E1 osoba
Run Code Online (Sandbox Code Playgroud)
要求结果:
16 StringProvider_111=Telefoní kontakty
17 StringProvider_116=Odpov?dná osoba
Run Code Online (Sandbox Code Playgroud)
该数据库具有UTF8编码和排序规则 cs_CZ.UTF-8
为什么我们不能在聚合函数中使用布尔值而不首先转换为某种整数类型?在许多情况下,从布尔数据类型的列计算总和,平均值或相关性是完全合理的.
请考虑以下示例,其中必须始终将boolean输入转换int为使其工作:
select
sum(boolinput::int),
avg(boolinput::int),
max(boolinput::int),
min(boolinput::int),
stddev(boolinput::int),
corr(boolinput::int,boolinputb::int)
from
(select
(random() > .5)::boolean as boolinput,
(random() > .5)::boolean as boolinputB
from
generate_series(1,100)
) a
Run Code Online (Sandbox Code Playgroud)
从PostgreSQL文档:
"真实"状态的有效字面值为:"1"上的"真","是","是"
对于"假"状态,可以使用以下值:FALSE'f''false''n''no''off''0'
因为根据定义TRUE等于1和FALSE等于0我不明白为什么铸造是必要的.
在聚合中允许布尔值也会产生有趣的副作用 - 例如,我们可以简化许多case语句:
当前版本(干净且易于理解):
select sum(case when gs > 50 then 1 else 0 end) from generate_series(1,100) gs;
Run Code Online (Sandbox Code Playgroud)
使用老式铸造操作员:::
select sum((gs > 50)::int) from generate_series(1,100) gs;
Run Code Online (Sandbox Code Playgroud)
直接聚合布尔值(当前不工作):
select sum(gs > 50) from generate_series(1,100) gs;
Run Code Online (Sandbox Code Playgroud)
是否可以在其他DBMS中直接聚合布尔值?为什么在PostgreSQL中无法做到这一点?
我确实有带gps数据的数据框,我需要计算两行(当前行和上一行)之间的距离
id time lat long heartrate altitude
1 20130424.tcx 2013-04-24T04:53:22Z 50.024818 14.522724 151 <NA>
2 20130424.tcx 2013-04-24T04:53:26Z 50.024818 14.522724 96 <NA>
3 20130424.tcx 2013-04-24T04:53:30Z 50.024818 14.522724 104 <NA>
4 20130424.tcx 2013-04-24T04:53:34Z 50.024818 14.522724 107 <NA>
5 20130424.tcx 2013-04-24T04:53:38Z 50.024818 14.522724 108 <NA>
6 20130424.tcx 2013-04-24T04:53:42Z 50.024818 14.522724 112 372.0
7 20130424.tcx 2013-04-24T04:53:46Z 50.024818 14.522724 151 372.0
8 20130424.tcx 2013-04-24T04:53:47Z 50.024677 14.522874 151 356.0
9 20130424.tcx 2013-04-24T04:53:50Z 50.024677 14.522874 118 356.0
10 20130424.tcx 2013-04-24T04:53:54Z 50.024677 14.522874 118 356.0
11 20130424.tcx 2013-04-24T04:53:58Z …Run Code Online (Sandbox Code Playgroud) 我想为表中每个唯一的值组合添加唯一的数字。
样本数据:
create table tmp (
id int primary key,
a varchar,
b varchar,
c varchar,
d varchar,
f int
);
insert into tmp values (1,'a','b','e','h',1);
insert into tmp values (2,'a','b','e','h',2);
insert into tmp values (3,'a','b','e','h',3);
insert into tmp values (4,'b','c','f','i',2);
insert into tmp values (5,'b','c','f','i',1);
insert into tmp values (6,'b','c','f','i',2);
insert into tmp values (7,'c','d','g','j',3);
insert into tmp values (8,'c','d','g','j',1);
insert into tmp values (9,'c','d','g','j',2);
Run Code Online (Sandbox Code Playgroud)
现在我需要为列a, b, c, d和返回列的每个唯一组合分配编号id以及gid(组标识符)
示例输出(例如 id 为 1,2 …
任何人都可以分享一个例子,我如何为PostgreSQL创建C函数,它将两个整数的数组作为输入并返回数组作为输出?
对于简单的整数我有:
#include "postgres.h"
#include <fmgr.h>
#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif
int
add_one(int arg) {
arg++;
return arg;
}
Run Code Online (Sandbox Code Playgroud)
在PostgreSQL中编译之后:
load '/usr/lib/postgresql/9.1/lib/add_one';
create or replace function add_one(integer)
returns integer as
'/usr/lib/postgresql/9.1/lib/add_one', 'add_one'
language c;
select add_one(1);
Run Code Online (Sandbox Code Playgroud)
我需要这样的东西:
#include "postgres.h"
#include <fmgr.h>
#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif
int
add_one(int[] arg) {
arg[1]++;
arg[2] = arg[2] + 2
return arg[];
}
Run Code Online (Sandbox Code Playgroud)
在PostgreSQL中:
load '/usr/lib/postgresql/9.1/lib/add_one';
create or replace function add_one(integer[])
returns integer[] as
'/usr/lib/postgresql/9.1/lib/add_one', 'add_one'
language c;
select add_one(ARRAY[1::int,1::int]);
Run Code Online (Sandbox Code Playgroud)
我试图从numeric.c修改一些函数,但到目前为止没有任何成功.
我想在我的图表中绘制动态垂直标记线.标记的位置ml_data在列中命名的数据集中可用ml_position.这是一个只有一行和一列的数据集.有没有办法让我ml_position在javascript中获得价值?目前我有以下工作示例:
function beforeDrawMarkerLine(axis, markerLine, icsc)
{
importPackage(Packages.org.eclipse.birt.chart.model.data.impl);
importPackage(Packages.org.eclipse.birt.chart.model.component.impl);
var ml_value = 20;
markerLine.setValue(NumberDataElementImpl.create(ml_value)) ;
}
Run Code Online (Sandbox Code Playgroud)
该值目前是固定的(20).我想指定的值ml_position来ml_value.
我正在尝试将 JSON 数据映射到列。我需要的一切都包含在data数组中。例子:
{"data":
[
{"stamp":1348249585,"date":"2012-09-21 17:46","blur":"blurs/1.jpg","img":["imgs/1.jpg",[1600,1200]],"thumb":["thumbs/1.jpg",[150,113]]},
{"stamp":1375607177,"date":"2013-08-04 09:06","blur":"blurs/2.jpg","img":["imgs/2.jpg",[1600,1200]],"thumb":["thumbs/2.jpg",[150,113]]},
{"stamp":1376242046,"date":"2013-08-11 17:27","blur":"blurs/3.jpg","img":["imgs/3.jpg",[1600,1200]],"thumb":["thumbs/3.jpg",[150,113]]},
...
Run Code Online (Sandbox Code Playgroud)
目前,我正在使用#>>具有动态生成条件的运算符:
data1)计算数组中元素的数量
2)创建varchar数组条件来匹配每个“行”
3) 处理各行上的元素。
我的解决方案是:
select
json_row,
json_row#>>'{stamp}' as stamp,
json_row#>>'{date}' as date,
json_row#>>'{img,0}' as img,
json_row#>>'{img,1,0}' as img_width,
json_row#>>'{img,1,1}' as img_height,
json_row#>>'{thumb,0}' as thumb,
json_row#>>'{thumb,1,0}' as thumb_width,
json_row#>>'{thumb,1,1}' as thumb_height,
json_row#>>'{thumb,2,0}' as th_x1,
json_row#>>'{thumb,2,1}' as th_y1,
json_row#>>'{thumb,3,0}' as th_x2,
json_row#>>'{thumb,3,1}' as th_y2,
json_row#>>'{blur}'
from
(
select
(gjson#>>c.cond)::json json_row
from
gallery_json
cross join (
select ('{data,'|| generate_series(0,
(select json_array_length((gjson#>>'{data}')::json) from gallery_json) - …Run Code Online (Sandbox Code Playgroud)