안녕하세요 MS SQL 사용하고 있는데
현테이블에서 원하는값은 아래와 같습니다.
작지기준으로 작업장코드 연속되는것만 뽑고 싶습니다. 당최 머리가 안돌아가네요?
고수님들 한수 가르침 부탁 드립니다.
현 테이블 | 원하는값 | |||||||
공정순서 | 작업장코드 | 작지번호 | 공정순서 | 작업장코드 | 작지번호 | |||
1 | Z01 | A010 | 1 | Z01 | A010 | |||
2 | Z01 | A010 | 2 | Z01 | A010 | |||
3 | X01 | A010 | 4 | Z01 | A010 | |||
4 | Z01 | A010 | 5 | Z01 | A010 | |||
5 | Z01 | A010 | 6 | A01 | A010 | |||
6 | A01 | A010 | 7 | A01 | A010 | |||
7 | A01 | A010 | 1 | A01 | A020 | |||
8 | B01 | A010 | 2 | A01 | A020 | |||
9 | Z01 | A010 | 4 | C01 | A020 | |||
10 | I01 | A010 | 5 | C01 | A020 | |||
1 | A01 | A020 | 6 | A01 | A020 | |||
2 | A01 | A020 | 7 | A01 | A020 | |||
3 | X01 | A020 | ||||||
4 | C01 | A020 | ||||||
5 | C01 | A020 | ||||||
6 | A01 | A020 | ||||||
7 | A01 | A020 | ||||||
8 | B01 | A020 | ||||||
9 | Z01 | A020 | ||||||
10 | I01 | A020 |
-- SQL-SERVER 2008 R2에서 테스트했어요. -- LAG() OVER() , LEAD() OVER() 지원안되네요. WITH T ( SEQ , WORK_CODE , NO ) AS ( SELECT 1 , 'Z01' , 'A010' UNION ALL SELECT 2 , 'Z01' , 'A010' UNION ALL SELECT 3 , 'X01' , 'A010' UNION ALL SELECT 4 , 'Z01' , 'A010' UNION ALL SELECT 5 , 'Z01' , 'A010' UNION ALL SELECT 6 , 'A01' , 'A010' UNION ALL SELECT 7 , 'A01' , 'A010' UNION ALL SELECT 8 , 'B01' , 'A010' UNION ALL SELECT 9 , 'Z01' , 'A010' UNION ALL SELECT 10 , 'I01' , 'A010' UNION ALL SELECT 1 , 'A01' , 'A020' UNION ALL SELECT 2 , 'A01' , 'A020' UNION ALL SELECT 3 , 'X01' , 'A020' UNION ALL SELECT 4 , 'C01' , 'A020' UNION ALL SELECT 5 , 'C01' , 'A020' UNION ALL SELECT 6 , 'A01' , 'A020' UNION ALL SELECT 7 , 'A01' , 'A020' UNION ALL SELECT 8 , 'B01' , 'A020' UNION ALL SELECT 9 , 'Z01' , 'A020' UNION ALL SELECT 10 , 'I01' , 'A020' ) SELECT SEQ , WORK_CODE , NO FROM (SELECT A.SEQ , A.WORK_CODE , A.NO ,B.SEQ AS SEQ_2 , B.WORK_CODE AS WORK_CODE_2 , B.NO AS NO_2 FROM T A LEFT OUTER JOIN T B ON A.SEQ = B.SEQ + 1 AND A.NO = B.NO ) Z WHERE Z.WORK_CODE = Z.WORK_CODE_2 UNION ALL SELECT SEQ_2 , WORK_CODE_2 , NO_2 FROM (SELECT A.SEQ , A.WORK_CODE , A.NO ,B.SEQ AS SEQ_2 , B.WORK_CODE AS WORK_CODE_2 , B.NO AS NO_2 FROM T A LEFT OUTER JOIN T B ON A.SEQ = B.SEQ + 1 AND A.NO = B.NO ) Z WHERE Z.WORK_CODE = Z.WORK_CODE_2 ORDER BY NO , SEQ
WITH LIST AS
(
SELECT 1 공정순서, 'Z01' 작업장코드,'A010' 작지번호 UNION ALL
SELECT 2,'Z01','A010' UNION ALL
SELECT 3,'X01','A010' UNION ALL
SELECT 4,'Z01','A010' UNION ALL
SELECT 5,'Z01','A010' UNION ALL
SELECT 6,'A01','A010' UNION ALL
SELECT 7,'A01','A010' UNION ALL
SELECT 8,'B01','A010' UNION ALL
SELECT 9,'Z01','A010' UNION ALL
SELECT 10,'I01','A010' UNION ALL
SELECT 1,'A01','A020' UNION ALL
SELECT 2,'A01','A020' UNION ALL
SELECT 3,'X01','A020' UNION ALL
SELECT 4,'C01','A020' UNION ALL
SELECT 5,'C01','A020' UNION ALL
SELECT 6,'A01','A020' UNION ALL
SELECT 7,'A01','A020' UNION ALL
SELECT 8,'B01','A020' UNION ALL
SELECT 9,'Z01','A020' UNION ALL
SELECT 10,'I01','A020'
)
SELECT 공정순서,작업장코드,작지번호
FROM (
SELECT A.*,
LEAD(작업장코드) OVER(PARTITION BY 작지번호 ORDER BY 작지번호,공정순서) NEXT_작업장코드,
LAG(작업장코드) OVER(PARTITION BY 작지번호 ORDER BY 작지번호,공정순서) BEF_작업장코드
FROM LIST A
) A
WHERE (A.작업장코드 = A.NEXT_작업장코드 OR
A.작업장코드 = A.BEF_작업장코드)