如何从多行创建 VIEW 组合和过滤结果
How to create VIEW witch combined and filtraded result from multiple rows
嗨,我需要完成的任务有问题。
我在 SQL 中有一个 table 显示用户(订阅者)的日志,其中包含添加时间戳、执行的操作和订阅者名称的触发器,看起来像这样:
TABLE: audit_subscibers
ID
Name
Action
Time
0
John
Insert a subscriber
2020-1-1
1
John
Deleted a subscriber
2020-3-1
2
Mark
Insert a subscriber
2020-4-5
3
Andrew
Insert a subscriber
2020-5-1
4
Andrew
Updated a subscriber
2020-5-15
现在我需要创建一个仅显示已使用 DELETE TIME 和 INSERTION TIME 删除的订阅者(姓名)的视图,以实现如下目的:
'John' 已添加(与所有其他成员一样)但也已被删除,不涉及仅 INSERTED
的成员
Name
Date added
Date deleted
John
2020-1-1
2020-3-1
some other
2020-x-x
2020-y-y
如何实现这一点,只接受具有 >1 个条目的订阅者,并且一个条目必须表明用户已被删除:删除订阅者,并将结果合并为一行?
我有另一个类似的任务,但这次我必须创建视图(仅基于 audit_subscibers table)我必须只显示仍然存在的订阅者(获取所有订阅者。使用“插入一个subscriber”,但从结果中拒绝那些有更多行的人,包括“Deleted a subscriber”
我真的很欣赏答案...
也许这可以给你提示或帮助:
CREATE TABLE audit_subscibers (
id int ,
name varchar(30),
action varchar(60),
time date );
INSERT INTO audit_subscibers VALUES
(0,'John','Insert a subscriber','2020-01-01'),
(1,'John','Deleted a subscriber','2020-03-01'),
(2,'Mark','Insert a subscriber','2020-04-05'),
(3,'Andrew','Insert a subscriber','2020-05-01'),
(4,'Andrew','Updated a subscriber','2020-05-15');
SELECT name,
MAX(case when action='Insert a subscriber' then time end) as Date_added,
MAX(case when action='Deleted a subscriber' then time end) as Date_deleted
FROM (
SELECT name,time,action
FROM audit_subscibers
WHERE name in (SELECT name
FROM audit_subscibers
WHERE action in ('Insert a subscriber','Deleted a subscriber')
GROUP BY name
HAVING COUNT(action) = 2 )
) as t1
group by name;
Result:
name Date_added Date_deleted
John 2020-01-01 2020-03-01
演示 https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=ef8a766a516951166161419a75e49cc6
嗨,我需要完成的任务有问题。 我在 SQL 中有一个 table 显示用户(订阅者)的日志,其中包含添加时间戳、执行的操作和订阅者名称的触发器,看起来像这样:
TABLE: audit_subscibers
ID | Name | Action | Time |
---|---|---|---|
0 | John | Insert a subscriber | 2020-1-1 |
1 | John | Deleted a subscriber | 2020-3-1 |
2 | Mark | Insert a subscriber | 2020-4-5 |
3 | Andrew | Insert a subscriber | 2020-5-1 |
4 | Andrew | Updated a subscriber | 2020-5-15 |
现在我需要创建一个仅显示已使用 DELETE TIME 和 INSERTION TIME 删除的订阅者(姓名)的视图,以实现如下目的:
'John' 已添加(与所有其他成员一样)但也已被删除,不涉及仅 INSERTED
的成员Name | Date added | Date deleted |
---|---|---|
John | 2020-1-1 | 2020-3-1 |
some other | 2020-x-x | 2020-y-y |
如何实现这一点,只接受具有 >1 个条目的订阅者,并且一个条目必须表明用户已被删除:删除订阅者,并将结果合并为一行?
我有另一个类似的任务,但这次我必须创建视图(仅基于 audit_subscibers table)我必须只显示仍然存在的订阅者(获取所有订阅者。使用“插入一个subscriber”,但从结果中拒绝那些有更多行的人,包括“Deleted a subscriber”
我真的很欣赏答案...
也许这可以给你提示或帮助:
CREATE TABLE audit_subscibers (
id int ,
name varchar(30),
action varchar(60),
time date );
INSERT INTO audit_subscibers VALUES
(0,'John','Insert a subscriber','2020-01-01'),
(1,'John','Deleted a subscriber','2020-03-01'),
(2,'Mark','Insert a subscriber','2020-04-05'),
(3,'Andrew','Insert a subscriber','2020-05-01'),
(4,'Andrew','Updated a subscriber','2020-05-15');
SELECT name,
MAX(case when action='Insert a subscriber' then time end) as Date_added,
MAX(case when action='Deleted a subscriber' then time end) as Date_deleted
FROM (
SELECT name,time,action
FROM audit_subscibers
WHERE name in (SELECT name
FROM audit_subscibers
WHERE action in ('Insert a subscriber','Deleted a subscriber')
GROUP BY name
HAVING COUNT(action) = 2 )
) as t1
group by name;
Result:
name Date_added Date_deleted John 2020-01-01 2020-03-01
演示 https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=ef8a766a516951166161419a75e49cc6