我有一个应用程序可以跟踪管理员在admin_comments
表中执行的操作。
最近,我被要求创建一个新表admin_stats
,以跟踪应用程序某些区域的生产力。
我已经在我的代码逻辑中实现了这一点,因此我们会跟踪所有统计信息,但是他们问我是否也可以通过在admin_comments
表中搜索来添加所有以前的统计信息适当的领域。
我的admin_comments
表格如下:
+----+---------+----------+---------+------+
| id | user_id | admin_id | comment | type |
+----+---------+----------+---------+------+
这样的admin_stats
表:
+----+----------+---------+------+--------+
| id | admin_id | user_id | type | action |
+----+----------+---------+------+--------+
首先,我只想将声明导入此表。声明可以是approved
,rejected
,temporarily rejected
或permanently rejected
,我已使用此查询获取管理员的每项操作的计数:
select admin_id,
SUM(CASE WHEN comment like 'Approved the claim for%' THEN 1 ELSE 0 END) as Approved,
SUM(CASE WHEN comment like 'Rejected the claim for%' THEN 1 ELSE 0 END) as Rejected,
SUM(CASE WHEN comment like 'Temporarily rejected (24 hour hold) the claim for%' THEN 1 ELSE 0 END) as TempReject,
SUM(CASE WHEN comment like 'Permanently rejected the claim for%' THEN 1 ELSE 0 END ) as PermReject
from admin_comments
where admin_id > 0
group by admin_id
产生如下结果:
有没有办法可以根据每个admin_id
的计数执行插入?
即从上图中我想将{1}}中的一条记录插入admin_stats
,其中action
为应用,{1}为拒绝type
的记录使用action
作为type
1的应用程序。
对于admin_id
4,我想插入25个已批准的admin_id
,12个被拒绝的actions
和2个actions
行动。
因此,每个操作都应作为新行输入,tempreject
将永远是应用程序。
答案 0 :(得分:1)
我认为你正在尝试这样做。
insert into admin_stats(admin_id,type,action) --add other columns as needed
select admin_id,'application' as type,
case when comment like 'Approved the claim for%' then 'Approved'
when comment like 'Rejected the claim for%' then 'Rejected'
when comment like 'Temporarily rejected (24 hour hold) the claim for%' then 'TempReject'
when comment like 'Permanently rejected the claim for%' then 'PermReject'
end as action
from admin_comments
where admin_id > 0
and (comment like 'Approved the claim for%'
or comment like 'Rejected the claim for%'
or comment like 'Temporarily rejected (24 hour hold) the claim for%'
or comment like 'Permanently rejected the claim for%')