如何计算另一个 table 中多列的特定值并更新结果

How to count specific value from multiple column in another table and update the results

我有如下表 1

a b c d e f
10 23 29 33 37 40
9 13 21 25 32 42
11 16 19 21 27 31
14 27 30 31 40 42
16 24 29 40 41 42
14 15 26 27 40 42
2 9 16 25 26 40
8 19 25 34 37 39
2 4 16 17 36 39
9 25 30 33 41 44
1 7 36 37 41 42
2 11 21 25 39 45
22 23 25 37 38 42
2 6 12 31 33 40
3 4 16 30 31 37

表2如下

numbs result
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

我想更新 table2.result 购买计数总匹配 numbstable1列(a、b、c、d、e、f)

已经尝试了下面提到的脚本,但是完全更新需要很长时间,所以如果有人可以提供任何计算速度更快的替代脚本,我将不胜感激。

UPDATE public.table2 AS t2 SET result = (select sum(
    (CASE WHEN t2.numbs=t1.a THEN 1 ELSE 0 END) +
    (CASE WHEN t2.numbs=t1.b THEN 1 ELSE 0 END) +
    (CASE WHEN t2.numbs=t1.c THEN 1 ELSE 0 END) +
    (CASE WHEN t2.numbs=t1.d THEN 1 ELSE 0 END) +
    (CASE WHEN t2.numbs=t1.e THEN 1 ELSE 0 END) +
    (CASE WHEN t2.numbs=t1.f THEN 1 ELSE 0 END) )     
                FROM public.table1 AS t1 )

好的,这样就可以了。如此丑陋的事实表明您的数据库设计不当。如果数字可以独立处理,那么它们应该在不同的行中。

DROP TABLE numbers;
CREATE TABLE numbers (
    a int,
    b int,
    c int,
    d int,
    e int,
    f int
);

INSERT INTO numbers  VALUES
(10, 23, 29, 33, 37, 40),
(9, 13, 21, 25, 32, 42),
(11, 16, 19, 21, 27, 31),
(14, 27, 30, 31, 40, 42),
(16, 24, 29, 40, 41, 42),
(14, 15, 26, 27, 40, 42),
(2, 9, 16, 25, 26, 40),
(8, 19, 25, 34, 37, 39),
(2, 4, 16, 17, 36, 39),
(9, 25, 30, 33, 41, 44),
(1, 7, 36, 37, 41, 42),
(2, 11, 21, 25, 39, 45),
(22, 23, 25, 37, 38, 42),
(2, 6, 12, 31, 33, 40),
(3, 4, 16, 30, 31, 37)
;
DROP TABLE table2;
CREATE TABLE table2 (
    numbs int,
    result int
);

INSERT INTO table2 VALUES
(1,0), (2,0), (3,0), (4,0), (5,0), (6,0), (7,0), (8,0), 
(9,0), (10,0), (11,0), (12,0), (13,0), (14,0), (15,0),
(16,0), (17,0), (17,0), (18,0), (19,0), (20,0), (21,0),
(22,0), (23,0), (24,0);

UPDATE table2 SET result=0;


UPDATE table2 SET result = result + n.cnt
    FROM (SELECT a,count(*) cnt FROM numbers GROUP BY a) n
    WHERE table2.numbs = n.a;
UPDATE table2 SET result = result + n.cnt
    FROM (SELECT b,count(*) cnt FROM numbers GROUP BY b) n
    WHERE table2.numbs = n.b;
UPDATE table2 SET result = result + n.cnt
    FROM (SELECT c,count(*) cnt FROM numbers GROUP BY c) n
    WHERE table2.numbs = n.c;
UPDATE table2 SET result = result + n.cnt
    FROM (SELECT d,count(*) cnt FROM numbers GROUP BY d) n
    WHERE table2.numbs = n.d;
UPDATE table2 SET result = result + n.cnt
    FROM (SELECT e,count(*) cnt FROM numbers GROUP BY e) n
    WHERE table2.numbs = n.e;
UPDATE table2 SET result = result + n.cnt
    FROM (SELECT f,count(*) cnt FROM numbers GROUP BY f) n
    WHERE table2.numbs = n.f;

输出:

sqlite> .mode box               
sqlite> select * from table2;   
┌───────┬────────┐              
│ numbs │ result │              
├───────┼────────┤              
│ 1     │ 1      │              
│ 2     │ 4      │              
│ 3     │ 1      │              
│ 4     │ 2      │              
│ 5     │ 0      │              
│ 6     │ 1      │              
│ 7     │ 1      │              
│ 8     │ 1      │              
│ 9     │ 3      │              
│ 10    │ 1      │              
│ 11    │ 2      │              
│ 12    │ 1      │              
│ 13    │ 1      │              
│ 14    │ 2      │              
│ 15    │ 1      │              
│ 16    │ 5      │              
│ 17    │ 1      │              
│ 17    │ 1      │              
│ 18    │ 0      │              
│ 19    │ 2      │              
│ 20    │ 0      │              
│ 21    │ 3      │              
│ 22    │ 1      │              
│ 23    │ 2      │              
│ 24    │ 1      │              
└───────┴────────┘              
sqlite>                         

看起来table1的每一行都没有重复的数字(有点像彩票号码)。

如果我的假设是正确的,你可以简化你的代码:

UPDATE table2 AS t2
SET result = (
  SELECT COUNT(*) 
  FROM table1 AS t1 
  WHERE t2.numbs IN (t1.a, t1.b, t1.c, t1.d, t1.e, t1.f)
); 

参见demo

这是一种方法:

update table2 
set result = (
  select sum(case when numbs in (a,b,c,d,e,f) then 1 else 0 end)
  from numbers
);

db<>fiddle here