안녕하세요
mssql 공부하는 초보인데 update 해야할것이 있는데 쿼리를 어떻게 짜야할지 감이 안잡혀서 질문드립니다
작업전
no orddat data1 type
1 2012 1 a
2 2012 2 b
3 2012 3 c
4 2013 null a
5 2013 null b
6 2013 null c
작업후
no orddat data1 type
1 2012 1 a
2 2012 2 b
3 2012 3 c
4 2013 1 a
5 2013 2 b
6 2013 3 c
orddat 2012 type a,b,c data1 내용 1,2,3을 2013 type이 같은 데이터에 data1 null쪽에 넣을려고 하는데
쿼리를 어떻게 짜야 하는걸까요? 부탁좀 드립니다
Comment 4
-
History
2013.01.10 17:10
-
minsouk
2013.01.11 06:56
create table tblx
(no int
, orddat int
, data1 int
, type varchar(10))
goinsert tblx values (1,2012,1,'a')
insert tblx values (2,2012,2,'b')
insert tblx values (3,2013,null,'a')
insert tblx values (4,2013,null,'b')
goupdate t
set t.data1 = a.data1
from (select data1, TYPE
from tblx where orddat =2012) a
join tblx t
on t.type = a.type
where t.orddat = 2013select * from tblx
-
건우아빠
2013.01.11 10:07
update ,delete는 select 조인조건이 동일합니다.
select문으로 표현해보시고
그걸 update문으로만 바꾸시면 손쉽게 작성이 가능 합니다.
위에 민석님 답글에서 update ~ set 부분을 select * 만 해보시면 이해가 쉬우실듯 합니다.
-
왕초보임돠
2013.01.11 14:48
답변 감사합니다 더 열심히 공부 하겠습니다 ^^
Update 테이블 Set data1 = 1 where type='a' And data1 is null
Update 테이블 Set data1 = 2 where type='b' And data1 is null
Update 테이블 Set data1 = 3 where type='c' And data1 is null
이걸 원하시는지