Redian新闻
>
问一道SQL的题 (转载)
avatar
问一道SQL的题 (转载)# Database - 数据库
p*c
1
【 以下文字转载自 JobHunting 讨论区 】
发信人: pic (喜欢画画么?), 信区: JobHunting
标 题: 问一道SQL的题
发信站: BBS 未名空间站 (Mon Feb 23 14:52:14 2009)
怎么在一个TABLE的一个格子里插入MULTIPLE VALUE?
举个例子有个TABLE1
employee_id, employee_name, employee_code
1 Mary a, b, c
2 Nancy a
3 John c, m
4 Leo b, a
employee_code是从另外一个table2读进来的
比如Mary在table2里
employee_id, employee_code
1 a
1 b
1 c
怎么写sql的语句能update tabl1, 把a,b,c全都写到field
avatar
c*t
2
Try either nested database or data array.

【在 p*c 的大作中提到】
: 【 以下文字转载自 JobHunting 讨论区 】
: 发信人: pic (喜欢画画么?), 信区: JobHunting
: 标 题: 问一道SQL的题
: 发信站: BBS 未名空间站 (Mon Feb 23 14:52:14 2009)
: 怎么在一个TABLE的一个格子里插入MULTIPLE VALUE?
: 举个例子有个TABLE1
: employee_id, employee_name, employee_code
: 1 Mary a, b, c
: 2 Nancy a
: 3 John c, m

avatar
B*g
3
kick.
啥DB啥version都不说?
oracle 9i + 有solution,oracle 10g + 有function。
要是oracle你会这篇,明天贴答案。
(其实考古前面有答案)

【在 p*c 的大作中提到】
: 【 以下文字转载自 JobHunting 讨论区 】
: 发信人: pic (喜欢画画么?), 信区: JobHunting
: 标 题: 问一道SQL的题
: 发信站: BBS 未名空间站 (Mon Feb 23 14:52:14 2009)
: 怎么在一个TABLE的一个格子里插入MULTIPLE VALUE?
: 举个例子有个TABLE1
: employee_id, employee_name, employee_code
: 1 Mary a, b, c
: 2 Nancy a
: 3 John c, m

avatar
p*c
4
by default SQL refers to MS SQL Server?
thanks anyway.

【在 B*****g 的大作中提到】
: kick.
: 啥DB啥version都不说?
: oracle 9i + 有solution,oracle 10g + 有function。
: 要是oracle你会这篇,明天贴答案。
: (其实考古前面有答案)

avatar
B*g
5
default is oracle.

【在 p*c 的大作中提到】
: by default SQL refers to MS SQL Server?
: thanks anyway.

avatar
B*g
6
BTW, no sql solution for sql server till 2008

【在 B*****g 的大作中提到】
: default is oracle.
avatar
c*t
7
不会吧。最多写个 aggregate 就是了。

【在 B*****g 的大作中提到】
: BTW, no sql solution for sql server till 2008
avatar
B*g
8
no user-defined sp allowed

【在 c*****t 的大作中提到】
: 不会吧。最多写个 aggregate 就是了。
avatar
w*r
9
default is teradata ..

e

【在 B*****g 的大作中提到】
: default is oracle.
avatar
i*i
10
select employee_id,max(sys_connect_by_path(employee_code,',')) result from
(
select employee_id,employee_code,(row_number() over(order by employee
_id,employee_code desc) + rank() over(order by employee_id)) rn
from table2
)
connect by rn-1 = prior rn
group by employee_id;
oracle 9i或以上可实现,结果是对table2聚合
出现
employee_id employ_code
1 a,b,c
接下来就是再写个outer join了就不说了
avatar
B*g
11
actually default is access

【在 w*r 的大作中提到】
: default is teradata ..
:
: e

avatar
c*t
12
那无非是 Oracle 提供了个 sp 。。。多此一举嘛。写个 aggregate 也没多难。

【在 B*****g 的大作中提到】
: no user-defined sp allowed
avatar
B*g
13
用sql比用sp县得nb, hehe.
要我写sp,我这个肯定用java

【在 c*****t 的大作中提到】
: 那无非是 Oracle 提供了个 sp 。。。多此一举嘛。写个 aggregate 也没多难。
avatar
p*c
14
declare @emp_code varchar(1000)
select @emp_code = COALESCE(@emp_code + ' ', '') + table2.emp_code
from table1 table1
LEFT JOIN table2 table2 ON table1.emp_id = table2.emp_id
where emp_id = 1
select @emp_code
this does return for Mary who is emp_id=1:
a b c
however, if i supply more emp_id in the where clause, for example:
declare @emp_code varchar(1000)
select @emp_code = COALESCE(@emp_code + ' ', '') + table2.emp_code
from table1 table1
LEFT JOIN table2 table2 ON table1.emp_id = table2.emp_id

【在 B*****g 的大作中提到】
: 用sql比用sp县得nb, hehe.
: 要我写sp,我这个肯定用java

avatar
p*c
15
i am using sql server 2008.
what's the solution?

【在 B*****g 的大作中提到】
: BTW, no sql solution for sql server till 2008
avatar
t*n
16
This is easy in SQL Server
update t1
set t1.employee_code = comb
from TABLE1 t1 join
(
SELECT employee_id,
STUFF((SELECT ',' + employee_code AS [text()]
FROM TABLE2 AS G2
WHERE G2.employee_id = G1.employee_id
ORDER BY employee_code
FOR XML PATH('')), 1, 1, '') AS comb
FROM TABLE2 AS G1
GROUP BY employee_id
) t2
on t1.employee_id = t2.employee_id

【在 p*c 的大作中提到】
: i am using sql server 2008.
: what's the solution?

avatar
B*g
17
赞一个,这个2005有还是一定要2008

【在 t****n 的大作中提到】
: This is easy in SQL Server
: update t1
: set t1.employee_code = comb
: from TABLE1 t1 join
: (
: SELECT employee_id,
: STUFF((SELECT ',' + employee_code AS [text()]
: FROM TABLE2 AS G2
: WHERE G2.employee_id = G1.employee_id
: ORDER BY employee_code

avatar
t*n
18
it works on 2005 & 2008

【在 B*****g 的大作中提到】
: 赞一个,这个2005有还是一定要2008
avatar
p*c
19
很棒! 包子给你和Beijing. 谢谢.

【在 t****n 的大作中提到】
: This is easy in SQL Server
: update t1
: set t1.employee_code = comb
: from TABLE1 t1 join
: (
: SELECT employee_id,
: STUFF((SELECT ',' + employee_code AS [text()]
: FROM TABLE2 AS G2
: WHERE G2.employee_id = G1.employee_id
: ORDER BY employee_code

avatar
B*g
20
can you compare the performance between this one and the store procedure one?

【在 p*c 的大作中提到】
: 很棒! 包子给你和Beijing. 谢谢.
avatar
c*t
21
那个 STUFF function 就是个 aggregate 吧。这样的话,应该没啥
太大的区别。

one?

【在 B*****g 的大作中提到】
: can you compare the performance between this one and the store procedure one?
avatar
p*c
22
oh boy, i happened to have time to play with it today. you know what, this
STUFF is much faster than an aggregated function. again, my company is on
sql2008.
http://www.kodyaz.com/articles/concatenate-using-xml-path.aspx

one?

【在 B*****g 的大作中提到】
: can you compare the performance between this one and the store procedure one?
avatar
p*c
23
here's more idea about the comparison...
i was testing on a table with 100K records, updating which by merging
multiple values from multiple rows in another table into one field for one
column per record. STUFF was completed in 14 seconds (I was stunned), vesus,
my cursor aggregation fn got everything returned in 02:43 minutes...
thought i'd brush up my XML feature from now on...

【在 p*c 的大作中提到】
: oh boy, i happened to have time to play with it today. you know what, this
: STUFF is much faster than an aggregated function. again, my company is on
: sql2008.
: http://www.kodyaz.com/articles/concatenate-using-xml-path.aspx
:
: one?

avatar
p*l
24
Zan~~

【在 t****n 的大作中提到】
: This is easy in SQL Server
: update t1
: set t1.employee_code = comb
: from TABLE1 t1 join
: (
: SELECT employee_id,
: STUFF((SELECT ',' + employee_code AS [text()]
: FROM TABLE2 AS G2
: WHERE G2.employee_id = G1.employee_id
: ORDER BY employee_code

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