首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将多个select和update子句截断为单个子句

将多个select和update子句截断为单个子句
EN

Stack Overflow用户
提问于 2016-11-13 13:25:55
回答 1查看 44关注 0票数 2

下面的查询似乎效率低下,因为我所做的只是为每个查询交换一个变量(共同品牌)。是否有方法将此查询合并为一个子句并获得相同的结果?

代码语言:javascript
复制
UPDATE temp_08.members
SET distinct_count=
(select distinct_count
from temp_08.members
WHERE cobrand='10001372' and month = '2016-09')
WHERE cobrand='10001372' and month = '2016-10' or month = '2016-11';


UPDATE temp_08.members
SET distinct_count=
(select distinct_count
from temp_08.members
WHERE cobrand='10006164' and month = '2016-09')
WHERE cobrand='10006164' and month = '2016-10' or month = '2016-11';



UPDATE temp_08.members
SET distinct_count=
(select distinct_count
from temp_08.members
WHERE cobrand='10005640' and month = '2016-09')
WHERE cobrand='10005640' and month = '2016-10' or month = '2016-11';



UPDATE temp_08.members
SET distinct_count=
(select distinct_count
from temp_08.members
WHERE cobrand='10005244' and month = '2016-09')
WHERE cobrand='10005244' and month = '2016-10' or month = '2016-11';
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-11-13 13:44:35

使用Postgres的语法:

代码语言:javascript
复制
UPDATE temp_08.members
SET distinct_count = dc
FROM (SELECT cobrand, distinct_count dc
      FROM temp_08.members
      WHERE month = '2016-09') x
WHERE temp_08.members.cobrand = x.cobrand
AND month IN ('2016-10', '2016-11')

如果只想更新某些共同品牌,则可以将其添加到内部查询:

代码语言:javascript
复制
AND cobrand IN ('10001372', '10006164', '10005640', '10005244')
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40574325

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档