有没有人知道如何在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)
这可能吗?
有谁知道如何在 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) 我有表数据
select * from tbltaxamount ;
id | taxname | taxinfoid | taxvalue | taxamt | zoneid | invoiceid | transid
----+-------------+-----------+----------+--------+--------+-----------+---------
1 | Service Tax | 0 | 0.00 | 28.69 | 2 | 119 | -1
2 | ABC Tax | 0 | 0.00 | 25.78 | 2 | 119 | -1
Run Code Online (Sandbox Code Playgroud)
现在,如何使用PostgreSQL的任何功能获得以下结果?
invoiceid | Service Tax | ABC Tax
----------+-------------+--------
119 | 28.69 | 25.78
Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个动态系统,允许用户从Excel导入数据列表,所以我需要有动态列,例如:
custom_columns_table
id list_id data_type column_name data ....
1 1 VARCHAR(255) email jhon@example.com ....
2 1 VARCHAR(255) name Jhon ....
list_table
id
1
Run Code Online (Sandbox Code Playgroud)
我需要这样的结果:
id email name ....
1 jhon@example.com Jhon ....
Run Code Online (Sandbox Code Playgroud)
我找到了一些使用交叉表的例子,但我不知道它是否适用于这种情况.
有谁知道我该怎么做?
可能重复:
转置sql结果,以便一列转到多列
我想在我的PSQL数据库中进行一种行/列交换.这是我的示例数据库:
id place price year
1 U.S. 80 2000
2 U.S. 60 2001
3 U.S. 40 2002
4 U.K. 600 2000
5 U.K. 500 2001
6 U.K. 350 2002
Run Code Online (Sandbox Code Playgroud)
我想将该表转换为以下示例:
year U.S. U.K.
2000 80 600
2001 60 500
2002 40 350
Run Code Online (Sandbox Code Playgroud)
这在PostgreSQL中是否可行?