更新 TABLE 计数
UPDATE TABLE with count
我在更新时遇到了一些问题
我在 informix 数据库中使用 posgress
我有两个 table.
1-st table with call data (anslogin, grade_1, grade_2, grade_3, grade_4, grade_5)
具有代理 ID 和每个代理的等级计数的第二数据按登录 ID 分组。
我创建了 1-st table 并从 2-nd table.
复制了所有登录 ID
然后我想为每个年级 (1,2,3,4,5) 创建另外 5 个请求,但我在更新时遇到问题:
UPDATE grade a
SET (grade_1) = (SELECT count(lastdigigts)
FROM call_rec b WHERE a.anslogin = b.anslogin
AND lastdigits ='1')
我不认为有五个请求的想法很好,但另一个我看不到的可能是你看到另一种解决我的任务的方法
tables 为了更好地理解:
第一 table
| login | grade_1 | grade_2 | grade_3 | grade_4 | grade_5 |
| Ivan | 2 | 3 | 4 | 1 | null |
| Masha | 1 | 3 | 4 | null | 4 |
| Kolya | 5 | null | 4 | 1 | null |
第 2 table
|anslogin|lastfigits|
|Kolya | 2 |
|Ivan | 2 |
|Kolya | 2 |
|Kolya | 2 |
|Kolya | 2 |
|Masha | 2 |
|Ivan | 2 |
如果我没理解错的话,你可以使用5个相关子查询:
UPDATE grade g
SET grade_1 = (SELECT count(*) FROM call_rec cr WHERE g.anslogin = cr.anslogin AND lastdigits = '1'),
grade_2 = (SELECT count(*) FROM call_rec cr WHERE g.anslogin = cr.anslogin AND lastdigits = '2'),
grade_3 = (SELECT count(*) FROM call_rec cr WHERE g.anslogin = cr.anslogin AND lastdigits = '3'),
grade_4 = (SELECT count(*) FROM call_rec cr WHERE g.anslogin = cr.anslogin AND lastdigits = '4'),
grade_5 = (SELECT count(*) FROM call_rec cr WHERE g.anslogin = cr.anslogin AND lastdigits = '5');
请注意,我将 table 别名更改为有意义的缩写而不是任意字母。
在 Postgres 中,您可以将其简化为:
UPGRADE grade g
SET grade_1 = cr.grade_1,
grade_2 = cr.grade_2,
grade_3 = cr.grade_3,
grade_4 = cr.grade_4,
grade_5 = cr.grade_5
FROM (SELECT anslogin,
COUNT(*) FILTER (WHERE lastdigits = '1') as grade_1,
COUNT(*) FILTER (WHERE lastdigits = '2') as grade_2,
COUNT(*) FILTER (WHERE lastdigits = '3') as grade_3,
COUNT(*) FILTER (WHERE lastdigits = '4') as grade_4,
COUNT(*) FILTER (WHERE lastdigits = '5') as grade_5
FROM call_rec cr
GROUP BY anslogin
) cr
WHERE cr.anslogin = g.anslogin;
此变体有效!!!
UPDATE grade g
SET grade_1 = (SELECT count(*) FROM call_rec cr WHERE g.anslogin = r.anslogin AND lastdigits = '1'),
grade_2 = (SELECT count(*) FROM call_rec cr WHERE g.anslogin = cr.anslogin AND lastdigits = '2'),
grade_3 = (SELECT count(*) FROM call_rec cr WHERE g.anslogin = cr.anslogin AND lastdigits = '3'),
grade_4 = (SELECT count(*) FROM call_rec cr WHERE g.anslogin = cr.anslogin AND lastdigits = '4'),
grade_5 = (SELECT count(*) FROM call_rec cr WHERE g.anslogin = cr.anslogin AND lastdigits = '5');
可能有用。
UPDATE grade
SET (grade_1,grade_2,grade_3,grade_4,grade_5) = (
(
SELECT
sum(case when lastdigits = '1' then 1 else 0 end)
,sum(case when lastdigits = '2' then 1 else 0 end)
,sum(case when lastdigits = '3' then 1 else 0 end)
,sum(case when lastdigits = '4' then 1 else 0 end)
,sum(case when lastdigits = '5' then 1 else 0 end)
FROM call_rec cr WHERE g.anslogin = r.anslogin
)
);
我在更新时遇到了一些问题 我在 informix 数据库中使用 posgress 我有两个 table.
1-st table with call data (anslogin, grade_1, grade_2, grade_3, grade_4, grade_5) 具有代理 ID 和每个代理的等级计数的第二数据按登录 ID 分组。 我创建了 1-st table 并从 2-nd table.
复制了所有登录 ID然后我想为每个年级 (1,2,3,4,5) 创建另外 5 个请求,但我在更新时遇到问题:
UPDATE grade a
SET (grade_1) = (SELECT count(lastdigigts)
FROM call_rec b WHERE a.anslogin = b.anslogin
AND lastdigits ='1')
我不认为有五个请求的想法很好,但另一个我看不到的可能是你看到另一种解决我的任务的方法
tables 为了更好地理解: 第一 table
| login | grade_1 | grade_2 | grade_3 | grade_4 | grade_5 |
| Ivan | 2 | 3 | 4 | 1 | null |
| Masha | 1 | 3 | 4 | null | 4 |
| Kolya | 5 | null | 4 | 1 | null |
第 2 table
|anslogin|lastfigits|
|Kolya | 2 |
|Ivan | 2 |
|Kolya | 2 |
|Kolya | 2 |
|Kolya | 2 |
|Masha | 2 |
|Ivan | 2 |
如果我没理解错的话,你可以使用5个相关子查询:
UPDATE grade g
SET grade_1 = (SELECT count(*) FROM call_rec cr WHERE g.anslogin = cr.anslogin AND lastdigits = '1'),
grade_2 = (SELECT count(*) FROM call_rec cr WHERE g.anslogin = cr.anslogin AND lastdigits = '2'),
grade_3 = (SELECT count(*) FROM call_rec cr WHERE g.anslogin = cr.anslogin AND lastdigits = '3'),
grade_4 = (SELECT count(*) FROM call_rec cr WHERE g.anslogin = cr.anslogin AND lastdigits = '4'),
grade_5 = (SELECT count(*) FROM call_rec cr WHERE g.anslogin = cr.anslogin AND lastdigits = '5');
请注意,我将 table 别名更改为有意义的缩写而不是任意字母。
在 Postgres 中,您可以将其简化为:
UPGRADE grade g
SET grade_1 = cr.grade_1,
grade_2 = cr.grade_2,
grade_3 = cr.grade_3,
grade_4 = cr.grade_4,
grade_5 = cr.grade_5
FROM (SELECT anslogin,
COUNT(*) FILTER (WHERE lastdigits = '1') as grade_1,
COUNT(*) FILTER (WHERE lastdigits = '2') as grade_2,
COUNT(*) FILTER (WHERE lastdigits = '3') as grade_3,
COUNT(*) FILTER (WHERE lastdigits = '4') as grade_4,
COUNT(*) FILTER (WHERE lastdigits = '5') as grade_5
FROM call_rec cr
GROUP BY anslogin
) cr
WHERE cr.anslogin = g.anslogin;
此变体有效!!!
UPDATE grade g
SET grade_1 = (SELECT count(*) FROM call_rec cr WHERE g.anslogin = r.anslogin AND lastdigits = '1'),
grade_2 = (SELECT count(*) FROM call_rec cr WHERE g.anslogin = cr.anslogin AND lastdigits = '2'),
grade_3 = (SELECT count(*) FROM call_rec cr WHERE g.anslogin = cr.anslogin AND lastdigits = '3'),
grade_4 = (SELECT count(*) FROM call_rec cr WHERE g.anslogin = cr.anslogin AND lastdigits = '4'),
grade_5 = (SELECT count(*) FROM call_rec cr WHERE g.anslogin = cr.anslogin AND lastdigits = '5');
可能有用。
UPDATE grade
SET (grade_1,grade_2,grade_3,grade_4,grade_5) = (
(
SELECT
sum(case when lastdigits = '1' then 1 else 0 end)
,sum(case when lastdigits = '2' then 1 else 0 end)
,sum(case when lastdigits = '3' then 1 else 0 end)
,sum(case when lastdigits = '4' then 1 else 0 end)
,sum(case when lastdigits = '5' then 1 else 0 end)
FROM call_rec cr WHERE g.anslogin = r.anslogin
)
);