1) 2)번을 조인해서 3번과 같이 나오고 싶은데 4번과 같이 조회 되나요? 어떻게 해야 하는지?
저는 당연히 4번과 같이 입력하면 당연히 나와야 되는데 안 나오는게 어떻게 해야 할지.. 신기하네요
1)
SELECT maqpono, maqposeq,maqhqty,maqbqty from MA170MP
PO-131210-RM-01 | 1 | 9 | 1 |
PO-131210-RM-01 | 2 | 10 | 2 |
PO-131210-RM-01 | 3 | 18 | 0 |
2)
SELECt MARPONO ,MARPOSEQ ,marhtno, marhqty, marbqty from MA180MP
PO-131210-RM-01 | 1 | AAA | 9 | 1 |
PO-131210-RM-01 | 2 | BBB | 3 | 1 |
PO-131210-RM-01 | 2 | CCC | 7 | 1 |
PO-131210-RM-01 | 3 | DDD | 18 | 0 |
4) 제가 쿼리 한 문장
SELECT maqpono, maqposeq,maqhqty,maqbqty,marhtno, marhqty, marbqty from MA170MP
left OUTER JOIN MA180MP ON MAQPONO=MARPONO AND MAQPOSEQ = MARPOSEQ
결과값
PO-131210-RM-01 | 1 | 9 | 1 | AAA | 9 | 1 |
PO-131210-RM-01 | 2 | 10 | 2 | BBB | 3 | 1 |
PO-131210-RM-01 | 2 | 10 | 2 | CCC | 7 | 1 |
PO-131210-RM-01 | 3 | 18 | 0 | DDD | 18 | 0 |
원하는 결과 값
3)
PO-131210-RM-01 | 1 | 9 | 1 | AAA | 9 | 1 |
PO-131210-RM-01 | 2 | 10 | 2 | BBB | 3 | 1 |
CCC | 7 | 1 | ||||
PO-131210-RM-01 | 3 | 18 | 0 | DDD | 18 | 0 |
ROW_NUMBER 추가로 쓰고 번호줘서 1번만 출력하게 하면 될것 같습니다.
다만 공백으로 나오게 할려면 숫자부분을 문자로 바꾸셔야 될것 같습니다.
;with MA170MP AS
(
SELECT 'PO-131210-RM-01' AS maqpono, '1' AS maqposeq, '9' AS maqhqty, '1' AS maqbqty
UNION ALL
SELECT 'PO-131210-RM-01' AS maqpono, '2' AS maqposeq, '10' AS maqhqty, '2' AS maqbqty
UNION ALL
SELECT 'PO-131210-RM-01' AS maqpono, '3' AS maqposeq, '18' AS maqhqty, '0' AS maqbqty
)
, MA180MP AS
(
SELECT 'PO-131210-RM-01' AS MARPONO, '1' AS MARPOSEQ, 'AAA' AS marhtno, 9 AS marhqty, 1 AS marbqty
UNION ALL
SELECT 'PO-131210-RM-01' AS MARPONO, '2' AS MARPOSEQ, 'BBB' AS marhtno, 3 AS marhqty, 1 AS marbqty
UNION ALL
SELECT 'PO-131210-RM-01' AS MARPONO, '2' AS MARPOSEQ, 'CCC' AS marhtno, 7 AS marhqty, 1 AS marbqty
UNION ALL
SELECT 'PO-131210-RM-01' AS MARPONO, '3' AS MARPOSEQ, 'DDD' AS marhtno, 18 AS marhqty, 0 AS marbqty
)
SELECT (CASE WHEN Num != 1 THEN '' ELSE maqpono END) AS maqpono
, (CASE WHEN Num != 1 THEN '' ELSE maqposeq END) AS maqposeq
, (CASE WHEN Num != 1 THEN '' ELSE maqhqty END) AS maqhqty
, (CASE WHEN Num != 1 THEN '' ELSE maqbqty END) AS maqbqty
, marhtno, marhqty, marbqty
FROM
(
SELECT maqpono, maqposeq, maqhqty, maqbqty, marhtno, marhqty, marbqty
, ROW_NUMBER() OVER (PARTITION BY maqpono, maqposeq, maqhqty, maqbqty ORDER BY marhqty) AS Num
from MA170MP
JOIN MA180MP ON MAQPONO=MARPONO AND MAQPOSEQ = MARPOSEQ
) AS A