我是SQL的新手,我在这里有一个问题。我在Postgresql工作。 我的表看起来像
更改
id action_id(fk) field_id(fk) old_value new_value
------------------------------------------------------------------
39 15 14 testPool
40 15 15 testSystem
41 15 16 61019
动作
id description audited_table audited_row audited_type
-----------------------------------------------------------------------------
15 Added system systemtable 61019 insert
字段
id audited_table name
---------------------------------------------------------------------------
14 systemtable pool
15 systemtable storagesystem
15 systemtable id
我想写一个查询来获取以下视图
id description audited_table audited_row audited_type field.name1 name1->new_value field.name2 name2->new_value
-----------------------------------------------------------------------------------------------------------------------------------------------------------
15 Added system systemtable 61019 insert pool testPool storagesystem testSystem
基本上我想将(更改行)列展平(行),并将其与'action'表连接起来。请注意'更改'表引用'action'和'field'表
架构图如下所示
感谢任何帮助,谢谢
答案 0 :(得分:1)
实际上并不完全清楚你所追求的是什么,但让我尝试回答。
首先让我们创建表格和示例数据:
PostgreSQL 9.1.9架构设置:
CREATE TABLE Change
("id" int, "action_id" int, "field_id" int, "old_value" varchar(10), "new_value" varchar(10))
;
INSERT INTO Change
("id", "action_id", "field_id", "old_value", "new_value")
VALUES
(39, 15, 14,'', 'testPool'),
(40, 15, 15,'', 'testSystem'),
(41, 15, 16,'', '61019')
;
CREATE TABLE Action
("id" int, "description" varchar(12), "audited_table" varchar(11), "audited_row" int, "audited_type" varchar(6))
;
INSERT INTO Action
("id", "description", "audited_table", "audited_row", "audited_type")
VALUES
(15, 'Added system', 'systemtable', 61019, 'insert')
;
CREATE TABLE Field
("id" int, "audited_table" varchar(11), "name" varchar(13))
;
INSERT INTO Field
("id", "audited_table", "name")
VALUES
(14, 'systemtable', 'pool'),
(15, 'systemtable', 'storagesystem'),
(15, 'systemtable', 'id')
;
如果您的问题是关于如何加入表格,答案是相当直接的:
查询1 :
SELECT C.id,
A.description,
F.audited_table,
A.audited_row,
A.audited_type,
F.name AS field_name,
C.new_value
FROM Change AS C
JOIN Action AS A
ON C.action_id = A.id
JOIN Field AS F
ON C.field_id = F.id;
<强> Results 强>:
| ID | DESCRIPTION | AUDITED_TABLE | AUDITED_ROW | AUDITED_TYPE | FIELD_NAME | NEW_VALUE |
|----|--------------|---------------|-------------|--------------|---------------|------------|
| 39 | Added system | systemtable | 61019 | insert | pool | testPool |
| 40 | Added system | systemtable | 61019 | insert | storagesystem | testSystem |
| 40 | Added system | systemtable | 61019 | insert | id | testSystem |
有关联接的详细信息,请查看我的博客系列:sqlity.net/en/1146/a-join-a-day-introduction/ 它基于SQL Server,但大部分信息都与您使用的RDBMS无关。
如果您想将多行变成一行,那么您正在寻找一个支点。
您可以通过将行组合在一起,然后使用MAX()
和CASE
的组合来选择单个值来执行数据透视表。您的问题的问题是您没有指定字段“field1”与“field2”的对应关系。为了解决这个问题,我使用ROW_NUMBER()
函数按字段id的顺序在字段中添加了编号。使用它,第一个字段是具有最小field_id的字段。枢轴现在看起来像这样:
查询2 :
SELECT action_id,
audited_table,
MAX(CASE WHEN field_number = 1 THEN name END) AS f1_name,
MAX(CASE WHEN field_number = 1 THEN new_value END) AS f1_new_value,
MAX(CASE WHEN field_number = 2 THEN name END) AS f2_name,
MAX(CASE WHEN field_number = 2 THEN new_value END) AS f2_new_value
FROM(
SELECT *,ROW_NUMBER()OVER(PARTITION BY C.action_id, F.audited_table ORDER BY F.id) field_number
FROM Change AS C
JOIN Field AS F
ON C.field_id = F.id
)X
GROUP BY action_id, audited_table;
<强> Results 强>:
| ACTION_ID | AUDITED_TABLE | F1_NAME | F1_NEW_VALUE | F2_NAME | F2_NEW_VALUE |
|-----------|---------------|---------|--------------|---------|--------------|
| 15 | systemtable | pool | testPool | id | testSystem |
查询2只获取Field和Change表的信息。要获取Action信息,我们还需要添加另一个连接:
查询3 :
SELECT A.id,
A.description,
X2.audited_table,
A.audited_row,
A.audited_type,
X2.f1_name,
X2.f1_new_value,
X2.f2_name,
X2.f2_new_value
FROM Action AS A
JOIN (
SELECT action_id,
audited_table,
MAX(CASE WHEN field_number = 1 THEN name END) AS f1_name,
MAX(CASE WHEN field_number = 1 THEN new_value END) AS f1_new_value,
MAX(CASE WHEN field_number = 2 THEN name END) AS f2_name,
MAX(CASE WHEN field_number = 2 THEN new_value END) AS f2_new_value
FROM(
SELECT *,ROW_NUMBER()OVER(PARTITION BY C.action_id, F.audited_table ORDER BY F.id) field_number
FROM Change AS C
JOIN Field AS F
ON C.field_id = F.id
)X1
GROUP BY action_id, audited_table
)X2
ON A.id = X2.action_id
<强> Results 强>:
| ID | DESCRIPTION | AUDITED_TABLE | AUDITED_ROW | AUDITED_TYPE | F1_NAME | F1_NEW_VALUE | F2_NAME | F2_NEW_VALUE |
|----|--------------|---------------|-------------|--------------|---------|--------------|---------|--------------|
| 15 | Added system | systemtable | 61019 | insert | pool | testPool | id | testSystem |
我希望这能回答你的问题。