블로그 이미지
보미아빠

카테고리

보미아빠, 석이 (532)
밥벌이 (16)
싸이클 (1)
일상 (1)
Total
Today
Yesterday

달력

« » 2025.11
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30

공지사항

최근에 올라온 글



--1. http://www.postman.pe.kr/zipcode/ 에서 3번 다운로드
--2. excel 에서 csv 로 변환 후 db insert

if object_id('tblx') is not null
drop table tblx
go

create table tblx
(
ZIPCODE varchar(1000)
,SIDO varchar(1000)
,GUGUN varchar(1000)
,DONG varchar(1000)
,RI varchar(1000)
,ST_BUNJI varchar(1000)
,ED_BUNJI varchar(1000)
,SEQ varchar(1000)
)
go

BULK INSERT l.dbo.[TBLx]
FROM '\\127.0.0.1\c$\db1\zipcode_20110311.txt'
WITH
(
FIELDTERMINATOR = ',',
TABLOCK,
FIRSTROW =2
)
go

delete from tblx where seq is null
go

--3. 모의 데이터 삽입
if object_id('tbly') is not null
drop table tbly
go

create table tbly
(idx int identity(1,1)
,h_address varchar(1000)
)
go

insert into tbly values ('서울특별시 강남구 논현1동 21번지')
insert into tbly values ('대구시 동구 신암4동 139번지')
insert into tbly values ('경기도 용인시 기흥구 중동 참솔마을 109동')
insert into tbly values ('경기 용인 기흥 중동 참솔마을')
insert into tbly values ('대구동구신암3동 어쩔시구리')
insert into tbly values ('이상한나라의 엘리스')
go

select top 3 * from tblx order by cast(seq as int)
select * from tbly

-- 원하는 데이터         
select sido, gugun, count(*) cnt
  from (select idx, h_address, zipcode, sido, gugun
    from tbly a
   cross apply (select top 1 *
      from tblx
        where a.h_address like +'%'+left(SIDO,2)+'%'+'%'+left(GUGUN,2)+'%') b ) a
 group by sido, gugun
go

-- 오류 데이터

select a.*
  from tbly a
  left join (select idx, h_address, zipcode, sido, gugun
     from tbly a
    cross apply (select top 1 *
       from tblx
       where a.h_address like +'%'+left(SIDO,2)+'%'+'%'+left(GUGUN,2)+'%') b ) b
    on a.idx = b.idx
 where b.idx is null

Posted by 보미아빠
, |

퀴즈

카테고리 없음 / 2011. 4. 23. 03:03


--dynamic 쿼리가 몹시 귀찮은 경우
--다른 컬럼으로 index 를 타고, 필터조건만 처리 할 경우 유용한 쿼리 로직

if object_id ('tblx') is not null
drop table tblx
go

create table tblx
(idx int
,c1 int
,c2 int
)
go

insert into tblx values(1,1,null),(2,null,2),(3,3,3), (4, null,null)

-- @c1 의 값이 null 이면 필터하지 않고, 값이 있으면 값으로 뿌린다.
-- 보통 다음과 같은 쿼리를 짜는데, 이는 null 값에 문제를 일으킨다.
-- 아래 문제를 해결하는 것이 미션 1 이다.

declare @c1 int = null

select *
  from tblx
 where c1 = case when @c1 is null then c1 else @c1 end
go

-- 해결방안

숙제임 (iq 90)

-- @c1 의 값이 9999 이면 필터하지 않고, 값이 있으면 값으로 출력하며, null 이면 null 만 출력한다. 
-- 이것이 미션 2 이다.

숙제임 (iq 100)

Posted by 보미아빠
, |

--dynamic 쿼리가 몹시 귀찮은 경우
--다른 컬럼으로 index 를 타고, 필터조건만 처리 할 경우 유용한 쿼리 로직

if object_id ('tblx') is not null
drop table tblx
go

create table tblx
(idx int
,c1 int
,c2 int
)
go

insert into tblx values(1,1,null),(2,null,2),(3,3,3), (4, null,null)

-- @c1 의 값이 null 이면 필터하지 않고, 값이 있으면 값으로 뿌린다.
-- 보통 다음과 같은 쿼리를 짜는데, 이는 null 값에 문제를 일으킨다.
-- 아래 문제를 해결하는 것이 미션 1 이다.

declare @c1 int = null

select *
  from tblx
 where c1 = case when @c1 is null then c1 else @c1 end
go

-- 해결방안

declare @c1 int = null

select *
  from tblx
 where (c1 = @c1 or 1=case when @c1 is null  then 1 else 0 end)
go

-- @c1 의 값이 9999 이면 필터하지 않고, 값이 있으면 값으로 출력하며, null 이면 null 만 출력한다. 
-- 이것이 미션 2 이다.

declare @c1 int = 9999

select *
  from tblx
 where (c1 = @c1 or 1=case when @c1 = 9999  then 1 else 0 end) or (c1 is null and 1=case when @c1 is null then 1 else 0 end)
go

-- 복합 필터 조건
declare @c1 int = null
declare @c2 int = null

select *
  from tblx
 where ((c1 = @c1 or 1=case when @c1 = 9999  then 1 else 0 end) or (c1 is null and 1=case when @c1 is null then 1 else 0 end))
   and ((c2 = @c2 or 1=case when @c2 = 9999  then 1 else 0 end) or (c2 is null and 1=case when @c2 is null then 1 else 0 end))
go

 select *
  from tbly
 where (a = @col1 or @col1 is null)

Posted by 보미아빠
, |

최근에 달린 댓글

최근에 받은 트랙백

글 보관함