Redian新闻
>
一个看似简单的sql语句
avatar
一个看似简单的sql语句# Database - 数据库
c*d
1
有两个表,相同的结构,相同的行数
比如
A:
1
0
1
B:
0
1
1
怎么用最简单的语句得到A+B
1
1
2
avatar
t*g
2
how do you know the order of records?

【在 c*****d 的大作中提到】
: 有两个表,相同的结构,相同的行数
: 比如
: A:
: 1
: 0
: 1
: B:
: 0
: 1
: 1

avatar
c*e
3
join condition? use row num?

【在 c*****d 的大作中提到】
: 有两个表,相同的结构,相同的行数
: 比如
: A:
: 1
: 0
: 1
: B:
: 0
: 1
: 1

avatar
a*t
4
can new table be used?
not sure if it is the shortest/cleanest, but it's simple
select *
into tmpTableC
from tableA
update tmpTableC
set tmpTableC.column1 = tmpTableC.column1 + tableB.column1
select *
from tmpTableC

【在 c*****d 的大作中提到】
: 有两个表,相同的结构,相同的行数
: 比如
: A:
: 1
: 0
: 1
: B:
: 0
: 1
: 1

avatar
c*e
5
这不成吧,我估计错误多半是update by multiple rows

【在 a*******t 的大作中提到】
: can new table be used?
: not sure if it is the shortest/cleanest, but it's simple
: select *
: into tmpTableC
: from tableA
: update tmpTableC
: set tmpTableC.column1 = tmpTableC.column1 + tableB.column1
: select *
: from tmpTableC

avatar
a*t
6
what's the answer?

【在 c*****d 的大作中提到】
: 有两个表,相同的结构,相同的行数
: 比如
: A:
: 1
: 0
: 1
: B:
: 0
: 1
: 1

avatar
c*d
7
maybe, I do not know

【在 c*******e 的大作中提到】
: join condition? use row num?
avatar
c*d
8
I am using subquery, which is the same as "create new table"
but it is not the answer he expected

【在 a*******t 的大作中提到】
: can new table be used?
: not sure if it is the shortest/cleanest, but it's simple
: select *
: into tmpTableC
: from tableA
: update tmpTableC
: set tmpTableC.column1 = tmpTableC.column1 + tableB.column1
: select *
: from tmpTableC

avatar
a*t
9
there is a row identifier, right?

【在 c*****d 的大作中提到】
: I am using subquery, which is the same as "create new table"
: but it is not the answer he expected

avatar
w*r
10
首先你这个提法是有问题的, Table应该看成是无序的set, 你要说有序就要根据某一个
/数个attribute排序
你说的表的行数一样,然后求A+B那么就要假定是有序的
如果是有序的话,那么好办用OLAP做一个sequence出来就完了
下面的SQL是Teradata的,其他的平台有相似的OLAP
select a.CA + b.CA
from
(Select a.* , row_number() over (partition by .. order by ..) as seq from A)
a
(select b.*, row_number() over(partition by .. order by ..) as seq from b) b
where a.seq = b.seq

【在 c*****d 的大作中提到】
: 有两个表,相同的结构,相同的行数
: 比如
: A:
: 1
: 0
: 1
: B:
: 0
: 1
: 1

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。