p*c 发帖数: 421 | 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 |
c*****t 发帖数: 1879 | 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
|
B*****g 发帖数: 34098 | 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
|
p*c 发帖数: 421 | 4 by default SQL refers to MS SQL Server?
thanks anyway.
【在 B*****g 的大作中提到】 : kick. : 啥DB啥version都不说? : oracle 9i + 有solution,oracle 10g + 有function。 : 要是oracle你会这篇,明天贴答案。 : (其实考古前面有答案)
|
B*****g 发帖数: 34098 | 5 default is oracle.
【在 p*c 的大作中提到】 : by default SQL refers to MS SQL Server? : thanks anyway.
|
B*****g 发帖数: 34098 | 6 BTW, no sql solution for sql server till 2008
【在 B*****g 的大作中提到】 : default is oracle.
|
c*****t 发帖数: 1879 | 7 不会吧。最多写个 aggregate 就是了。
【在 B*****g 的大作中提到】 : BTW, no sql solution for sql server till 2008
|
B*****g 发帖数: 34098 | 8 no user-defined sp allowed
【在 c*****t 的大作中提到】 : 不会吧。最多写个 aggregate 就是了。
|
w*r 发帖数: 2421 | 9 default is teradata ..
e
【在 B*****g 的大作中提到】 : default is oracle.
|
i*i 发帖数: 25 | 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了就不说了 |
|
|
B*****g 发帖数: 34098 | 11 actually default is access
【在 w*r 的大作中提到】 : default is teradata .. : : e
|
c*****t 发帖数: 1879 | 12 那无非是 Oracle 提供了个 sp 。。。多此一举嘛。写个 aggregate 也没多难。
【在 B*****g 的大作中提到】 : no user-defined sp allowed
|
B*****g 发帖数: 34098 | 13 用sql比用sp县得nb, hehe.
要我写sp,我这个肯定用java
【在 c*****t 的大作中提到】 : 那无非是 Oracle 提供了个 sp 。。。多此一举嘛。写个 aggregate 也没多难。
|
p*c 发帖数: 421 | 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
|
p*c 发帖数: 421 | 15 i am using sql server 2008.
what's the solution?
【在 B*****g 的大作中提到】 : BTW, no sql solution for sql server till 2008
|
t****n 发帖数: 263 | 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?
|
B*****g 发帖数: 34098 | 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
|
t****n 发帖数: 263 | 18 it works on 2005 & 2008
【在 B*****g 的大作中提到】 : 赞一个,这个2005有还是一定要2008
|
p*c 发帖数: 421 | 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
|
B*****g 发帖数: 34098 | 20 can you compare the performance between this one and the store procedure one?
【在 p*c 的大作中提到】 : 很棒! 包子给你和Beijing. 谢谢.
|
|
|
c*****t 发帖数: 1879 | 21 那个 STUFF function 就是个 aggregate 吧。这样的话,应该没啥
太大的区别。
one?
【在 B*****g 的大作中提到】 : can you compare the performance between this one and the store procedure one?
|
p*c 发帖数: 421 | 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?
|
p*c 发帖数: 421 | 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?
|
p********l 发帖数: 279 | 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
|