销售数据包含可以转换任何字符的动态产品名称。
动态数据透视表是根据来自的示例创建的 Crosstab with a large or undefined number of categories
translate()用于删除不良字符。
在结果数据透视表中,列名已损坏:删除了缺少的字符和空格。 如何返回与源数据中的列名相同的数据? 我试着用
quote_ident(productname) as tootjakood,
而不是
'C'||upper(Translate(productname,'Ø. &/+-,%','O')) as tootjakood,
但它返回错误
错误:列“Ø,12.3 / 3mm”不存在
测试用例:
create temp table sales ( saledate date, productname char(20), quantity int );
insert into sales values ( '2016-1-1', 'Ø 12.3/3mm', 2);
insert into sales values ( '2016-1-1', '+-3,4%/3mm', 52);
insert into sales values ( '2016-1-3', '/3,2m-', 246);
do $do$
declare
voter_list text;
begin
create temp table myyk on commit drop as
select saledate as kuupaev,
'C'||upper(Translate(productname,'Ø. &/+-,%','O')) as tootjakood,
sum(quantity)::int as kogus
from sales
group by 1,2
;
drop table if exists pivot;
voter_list := (
select string_agg(distinct tootjakood, ' ' order by tootjakood) from myyk
);
execute(format('
create table pivot (
kuupaev date,
%1$s
)', (replace(voter_list, ' ', ' integer, ') || ' integer')
));
execute (format($f$
insert into pivot
select
kuupaev,
%2$s
from crosstab($ct$
select
kuupaev,tootjakood,kogus
from myyk
order by 1
$ct$,$ct$
select distinct tootjakood
from myyk
order by 1
$ct$
) as (
kuupaev date,
%4$s
);$f$,
replace(voter_list, ' ', ' + '),
replace(voter_list, ' ', ', '),
'',
replace(voter_list, ' ', ' integer, ') || ' integer' -- 4.
));
end; $do$;
select * from pivot;
使用Postgres 9.1。
答案 0 :(得分:1)
你应该使用双引号。因为您使用空格来标识列分隔符,所以应该从列名中删除空格(或更改分隔符标识的方式)。
使用
//...
while ((line = br.readLine()) != null) {
String[] values = line.split("\t");
//To ensure it's still valid data
if (values.length >= 1 && values.length <= 2) {
String addedTask = values[0];
String enteredDueDate;
//Check whether dueDate has a value or is empty
if (values.length == 1)
enteredDueDate = "";
else
enteredDueDate = values[1];
DueDate d = new DueDate(addedTask, enteredDueDate);
tasks.add(d);
}
}
//...
你会得到:
//...
while ((line = br.readLine()) != null) {
String[] values = line.split("\t");
if (values.length >= 1 && values.length <= 2)
tasks.add(new DueDate(values[0], values.length == 1 ? "" : values[1]));
}
//...