相关疑难解决方法(0)

在PostgreSQL中插入带单引号的文本

我有一张桌子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 quotes insert special-characters

377
推荐指数
5
解决办法
32万
查看次数

PostgreSQL交叉表查询

有没有人知道如何在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)

这可能吗?

sql postgresql pivot case crosstab

180
推荐指数
4
解决办法
17万
查看次数

使用Tablefunc在多列上进行透视

有没有人过去常常使用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 …

sql postgresql pivot crosstab

11
推荐指数
2
解决办法
3万
查看次数

为什么在PostgreSQL查询中排序DESC时会出现NULL值?

在订购查询降序或升序时,您何时会首先想要NULLS?

在我看来,绝大多数时候,无论是升序还是降序,所期望的行为都是NULLS LAST.相反,我们必须指定NULLS FIRST.

sql database postgresql null sql-order-by

9
推荐指数
1
解决办法
7117
查看次数

将行转换为 PostgreSQL 中的列?

有谁知道如何在 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)

sql postgresql pivot

5
推荐指数
1
解决办法
7529
查看次数

Postgres 根据列值将行转置为列

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 …

sql postgresql transpose crosstab switch-statement

4
推荐指数
1
解决办法
8677
查看次数

具有多个“行名称”列的 Postgresql 交叉表查询

我有一张“瘦高”事实表:

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() 函数只允许键列。

有任何想法吗?

postgresql crosstab postgresql-9.4

2
推荐指数
1
解决办法
6703
查看次数