作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 postgres 数据库,在数据库迁移过程中,我有一个空表 tableB
,我想用另一个 tableA
中的现有数据填充它。
tableB
有以下列
CREATE TABLE contributors (
id bigint NOT NULL,
pps double precision NOT NULL,
contributor_user_id bigint,
project_id bigint
);
而 tableA
有以下列
CREATE TABLE tableA (
id bigint NOT NULL,
assigned_ppoints double precision NOT NULL,
state integer,
contributor_id bigint,
project_id bigint
);
所有*_id
其实都是外键。
我需要为 tableA
的 contributor_id
和 project_id
的每个现有组合在 tableB
上添加一个新行如下
project_id
中,tableA
的project_id
contributor_user_id
中,我需要tableA
的contributor_id
pps
中,我需要 contributor_user_id
和 project_id
的 assigned_ppoints
的 sum> 对于 tableA
中的 state=2
。 我的起点(也是很遥远的)是
INSERT INTO tableB (id, project_id, contributor_user_id, pps)
SELECT MAX(id) FROM tableB, project_id, contributor_id, SUM(assigned_ppoints)
FROM tableA WHERE project_id=1 AND state=2 AND contributor_id=1;
这是错误的,只会添加与 project_id
和 contributor_id
的一种组合对应的一行。
我该怎么做?
最佳答案
select
max(id),
contributor_id,
project_id,
sum(assigned_ppoints) filter (where state = 2)
from t
group by 2,3
对于 9.3 及之前的版本:
select
max(id),
contributor_id,
project_id,
sum(assigned_ppoints * (state = 2)::integer)
from t
group by 2,3
关于sql - 将唯一键、几列和子查询的总和插入表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41941089/
我正在尝试使用描述的方法将数据表转换为字典 here , 但我得到一个错误 Cannot implicitly convert type System.Collections.Generic.Dict
我想在几个列上使用 orderBY,但它们应该像一列一样。 该表看起来像这样: col1 | col2 5 | 2 | | 3 7 | | 1 | 1
我有这张表 mytable: +----+--------------------------------------+ | id | date1 | date2 | date3
我是一名优秀的程序员,十分优秀!