有没有人知道如何在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)
这可能吗?
是否可以定义默认情况下创建新表的模式?(由"不合格的表名称"引用.)
我已经看到了在Postgres中使用"搜索路径"的一些细节,但我认为它只在检索数据时有效,而不是创建.
我有一堆SQL脚本,它们创建了许多表.我没有修改脚本,而是希望默认情况下在特定模式中设置数据库创建表 - 当它们具有非限定名称时.
这可能吗?
我是PostgreSQL的新手.
假设我有一张桌子
colorname Hexa rgb rgbvalue
Violet #8B00FF r 139
Violet #8B00FF g 0
Violet #8B00FF b 255
Indigo #4B0082 r 75
Indigo #4B0082 g 0
Indigo #4B0082 b 130
Blue #0000FF r 0
Blue #0000FF g 0
Blue #0000FF b 255
Run Code Online (Sandbox Code Playgroud)
如果我在SQL Server中做一个Pivot
SELECT colorname,hexa,[r], [g], [b]
FROM
(SELECT colorname,hexa,rgb,rgbvalue
FROM tblPivot) AS TableToBePivoted
PIVOT
(
sum(rgbvalue)
FOR rgb IN ([r], [g], [b])
) AS PivotedTable;
Run Code Online (Sandbox Code Playgroud)
我把输出作为
colorname hexa r g b
Blue #0000FF 0 0 255
Indigo #4B0082 …Run Code Online (Sandbox Code Playgroud) 我已经pg_trgm安装了模块。
pg_trgm | 1.0 | extensions | text similarity measurement and index ...
Run Code Online (Sandbox Code Playgroud)
模式集为extensions。要使用它,我必须运行以下选择:
extensions.similarity('hello','hallo');
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用%运算符运行一条语句,并收到以下消息。
mydb=# select * from rssdata where description % 'Brazil';
ERROR: operator does not exist: character varying % unknown
LINE 1: select * from rssdata where description % 'Brazil';
^
HINT: No operator matches the given name and argument type(s).
You might need to add explicit type casts.
Run Code Online (Sandbox Code Playgroud)
运行%或<->操作员需要什么?