我有一张桌子test(id,name).
我需要插入样值: user's log,'my user',customer's.
insert into test values (1,'user's log');
insert into test values (2,''my users'');
insert into test values (3,'customer's');
Run Code Online (Sandbox Code Playgroud)
如果我运行上述任何语句,我会收到错误.
如果有任何方法可以正确地做到这一点请分享.我不想要任何准备好的陈述.
是否可以使用sql转义机制?
有没有人知道如何在PostgreSQL中创建交叉表查询?
例如,我有下表:
Section Status Count
A Active 1
A Inactive 2
B Active 4
B Inactive 5
Run Code Online (Sandbox Code Playgroud)
我想查询返回以下交叉表:
Section Active Inactive
A 1 2
B 4 5
Run Code Online (Sandbox Code Playgroud)
这可能吗?
有没有人过去常常使用tablefunc多个变量,而不是只使用行名?文档说明:
对于具有相同row_name值的所有行,"额外"列应该是相同的.
我不知道如何在没有组合我想要转向的列的情况下如何做到这一点(我非常怀疑它会给我我需要的速度).一种可能的方法是将实体设为数字,并将其作为毫秒添加到localt,但这似乎是一种不稳定的方式.
我编辑了回答这个问题的数据:PostgreSQL Crosstab Query.
CREATE TEMP TABLE t4 (
timeof timestamp
,entity character
,status integer
,ct integer);
INSERT INTO t4 VALUES
('2012-01-01', 'a', 1, 1)
,('2012-01-01', 'a', 0, 2)
,('2012-01-02', 'b', 1, 3)
,('2012-01-02', 'c', 0, 4);
SELECT * FROM crosstab(
'SELECT timeof, entity, status, ct
FROM t4
ORDER BY 1,2,3'
,$$VALUES (1::text), (0::text)$$)
AS ct ("Section" timestamp, "Attribute" character, "1" int, "0" int);
Run Code Online (Sandbox Code Playgroud)
返回:
Section | Attribute | 1 …
在订购查询降序或升序时,您何时会首先想要NULLS?
在我看来,绝大多数时候,无论是升序还是降序,所期望的行为都是NULLS LAST.相反,我们必须指定NULLS FIRST.
有谁知道如何在 PostgreSQL 中将行转置为列?例如,我有下表:-
Period T1 T2 Difference
---------- ------- -------- -----------
MAR-2013 34525 319 34206
AUG-2014 35632 14453 21179
OCT-2014 28124 10082 18042
JUN-2014 20571 9353 11218
MAY-2015 44963 39097 5866
FEB-2013 1941 127 1814
JUL-2014 14510 12965 1545
APR-2015 32446 30992 1454
MAY-2014 13510 12136 1374
APR-2014 8042 6967 1075
JAN-2013 1107 86 1021
DEC-2014 30764 30076 688
SEP-2014 6886 6380 506
MAR-2014 4695 4242 453
Run Code Online (Sandbox Code Playgroud)
但我需要输出为
Period MAR-2013 AUG-2014 OCT-2014 JUN-2014 MAY-2015 FEB-2013 JUL-2014 APR-2015 MAY-2014 APR-2014 JAN-2013 …Run Code Online (Sandbox Code Playgroud) statement我在 Postgres 中有下表:
stock | year | statement | amount
ACME | 2003 | Q1 Earnings | 100
ACME | 2003 | Q2 Earnings | 200
ACME | 2004 | Q2 Earnings | 300
Run Code Online (Sandbox Code Playgroud)
如何制作一个新表,将一年的 4 个季度全部排在一行?以及缺失语句的空值。
stock | year | Q1 Earnings | Q2 Earnings | Q3 Earnings | Q4 Earnings
ACME | 2003 | 100 | 200 | Null | Null
ACME | 2004 | NULL | 300 | Null | Null
Run Code Online (Sandbox Code Playgroud)
找到了这个答案:Postgres - Transpose Rows …
我有一张“瘦高”事实表:
CREATE TABLE facts(
eff_date timestamp NOT NULL,
update_date timestamp NOT NULL,
symbol_id int4 NOT NULL,
data_type_id int4 NOT NULL,
source_id char(3) NOT NULL,
fact decimal
/* Keys */
CONSTRAINT fact_pk
PRIMARY KEY (source_id, symbol_id, data_type_id, eff_date),
)
Run Code Online (Sandbox Code Playgroud)
我想“透视”这个报告,所以标题看起来像这样:
eff_date, symbol_id, source_id, datatypeValue1, ... DatatypeValueN
Run Code Online (Sandbox Code Playgroud)
即,对于 eff_date、symbol_id 和 source_id 的每个唯一组合,我想要一行。
但是,postgresql crosstab() 函数只允许键列。
有任何想法吗?