gpt4 book ai didi

具有唯一表单元格的 SQL 算术

转载 作者:行者123 更新时间:2023-11-29 12:27:23 29 4
gpt4 key购买 nike

我有两个表(使用 PostgreSQL),它们看起来如下:

表1(p点从1到450递增1)

--------+-------+--------+---------+---------+-------+
ppoint | tom08 | tom920 | tom2135 | tom3650 | tom51 |
--------+-------+--------+---------+---------+-------+
1 | 2.5 | 125 | 52.5 | 15 | 2.5 |
... | ... | ... | ... | ... | ... |
450 | 0 | 7.5 | 87.5 | 0 | 0 |
--------+-------+--------+---------+---------+-------+

表2

--------+-------+
ppoint | tom |
--------+-------+
1 | 197.5 |
... | ... |
450 | 95 |
--------+-------+

表 2 的“tom”列是表 1“tom...”值的总和。

我想将表 1 中的每个“tom...”单元格与表 2 的相应汇总值相除。

我使用以下方法获得了一个值:

SELECT
(SELECT tom08 FROM Table1 WHERE ppoint = 1)/
(SELECT tom FROM Table 2 WHERE ppoint = 1)
FROM Table1 WHERE ppoint = 1

但它仍然只有一个值,我想将每个单元格与相应的 Table2 汇总值分开并放入一个新表中(覆盖 Table1 也是一种选择)。

这个新表的第 1 行(第 1 列是相应的“ppoint”)应该包含:

tom08 (FROM Table1 WHERE ppoint=1)/tom (FROM Table2 WHERE ppoint=1)
tom920 (FROM Table1 WHERE ppoint=1)/tom (FROM Table2 WHERE ppoint=1)
tom2135 (FROM Table1 WHERE ppoint=1)/tom (FROM Table2 WHERE ppoint=1)
tom3650 (FROM Table1 WHERE ppoint=1)/tom (FROM Table2 WHERE ppoint=1)
tom51 (FROM Table1 WHERE ppoint=1)/tom (FROM Table2 WHERE ppoint=1)

这个新表的第 2 行应该包含:

tom08 (FROM Table1 WHERE ppoint=2)/tom (FROM Table2 WHERE ppoint=2)
tom920 (FROM Table1 WHERE ppoint=2)/tom (FROM Table2 WHERE ppoint=2)
tom2135 (FROM Table1 WHERE ppoint=2)/tom (FROM Table2 WHERE ppoint=2)
tom3650 (FROM Table1 WHERE ppoint=2)/tom (FROM Table2 WHERE ppoint=2)
tom51 (FROM Table1 WHERE ppoint=2)/tom (FROM Table2 WHERE ppoint=2)

依此类推,直到 450,所以决赛 table 应该看起来像下面这样:

+--------+-------------+-------------+-------------+-------------+-------------+
| ppoint | tom08 | tom920 | tom2135 | tom3650 | tom51 |
+--------+-------------+-------------+-------------+-------------+-------------+
| 1 | 0.012658228 | 0.632911392 | 0.265822785 | 0.075949367 | 0.012658228 |
| ...| ... | ... | ... | ... | ... |
| 2 | 0 | 0.078947368 | 0.921052632 | 0 | 0 |
+--------+-------------+-------------+-------------+-------------+-------------+

有什么解决办法吗?你有什么建议吗?对不起我的英语 - 这可能不是解释我的问题的最佳方式:)谢谢!

最佳答案

如果我没理解错的话,我想这就是您要找的。

SELECT tom08/tom, tom920/tom, tom2135/tom, tom3650/tom, tom51/tom  
FROM table1 t1, table2 t2
WHERE t1.ppoint = t2.ppoint;

关于具有唯一表单元格的 SQL 算术,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25141164/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com