안녕하세요.
pivot 관련해서 쿼리 작성중인데 어떻게 해야 하는지 문의 드립니다..
select *
from (
select a.setnum
,a.ProductNum
,a.ProductName
,a.ProductCode
,a.Yield
,b.OutputYear
,b.Outputmonth
,b.OutputQty
,SUM(b.OutputQty) OVER(partition by b.outputProductNum) AS SUM_Product
from MPVProduct A, MPVData B
where a.SetNum = b.OutputSetNum
and a.ProductNum = b.OutputProductNum
) As result
Pivot
(
sum(OutputQty) for Outputmonth in ([Jan],[Feb],[Mar],[Apr],[May],[Jun],[Jul],[Aug],[Sep],[Oct],[Nov],[Dec])
) as pivot_result
order by OutputYear
연도와 월 데이터는 계속 쌓이는 형태입니다.
위의 결과에서 아래처럼 연도_월 컬럼으로 변경해서 옆으로 쭉 출력이 되게 가능할까요?
알려주시면 감사하겠습니다!!
Comment 6
-
지영아빠
2023.03.21 11:58
-
Just
2023.03.21 14:41
테이블 생성쿼리와 샘플 데이터 입력 쿼리입니다.
CREATE TABLE [dbo].[MPVData](
[OutputSetNum] [int] NOT NULL,
[OutputProductNum] [int] NOT NULL,
[OutputYear] [varchar](4) NOT NULL,
[OutputMonth] [varchar](10) NOT NULL,
[OutputQty] [int] NOT NULL,
[RegUser] [nvarchar](50) NULL,
[RegDate] [datetime] NOT NULL,
[UpdDate] [datetime] NULL
) ON [PRIMARY]
GOCREATE TABLE [dbo].[MPVProduct](
[SetNum] [int] NOT NULL,
[ProductNum] [int] NOT NULL,
[ProductName] [nvarchar](100) NULL,
[ProductCode] [nvarchar](100) NULL,
[Yield] [int] NULL,
[RegUser] [nvarchar](50) NULL,
[RegDate] [datetime] NOT NULL,
[UpdDate] [datetime] NULL,
CONSTRAINT [PK_OutputQtyProduct] PRIMARY KEY CLUSTERED
(
[SetNum] ASC,
[ProductNum] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GOinsert into MPVData(OutputSetNum, OutputProductNum, OutputYear, OutputMonth, OutputQty, reguser, regdate)
values
(1, 1 ,'2020', 'May', 23 ,'tester', '2023-03-16 10:33:28.027')
,(1, 2 ,'2020', 'May', 22 ,'tester', '2023-03-16 10:33:28.027')
,(1, 3 ,'2020', 'May', 23 ,'tester', '2023-03-16 10:33:28.027')
,(1, 4 ,'2020', 'May', 23 ,'tester', '2023-03-16 10:33:28.027')
,(1, 5 ,'2020', 'May', 23 ,'tester', '2023-03-16 10:33:28.027')
,(1, 6 ,'2020', 'May', 23 ,'tester', '2023-03-16 10:33:28.027')
,(1, 7 ,'2020', 'May', 8 ,'tester', '2023-03-16 10:33:28.027')
,(1, 8 ,'2020', 'May', 5 ,'tester', '2023-03-16 10:33:28.027')
,(1, 9 ,'2020', 'May', 5 ,'tester', '2023-03-16 10:33:28.027')
,(1, 10 ,'2020', 'May', 5 ,'tester', '2023-03-16 10:33:28.027')
,(1, 1 ,'2021', 'Jan', 10 ,'tester', '2023-03-16 10:33:28.027')
,(1, 2 ,'2021', 'Jan', 7 ,'tester', '2023-03-16 10:33:28.027')
,(1, 3 ,'2021', 'Jan', 135 ,'tester', '2023-03-16 10:33:28.027')
,(1, 4 ,'2021', 'Jan', 70 ,'tester', '2023-03-16 10:33:28.027')
,(1, 5 ,'2021', 'Jan', 70 ,'tester', '2023-03-16 10:33:28.027')
,(1, 6 ,'2021', 'Jan', 71 ,'tester', '2023-03-16 10:33:28.027')
,(1, 7 ,'2021', 'Jan', 71 ,'tester', '2023-03-16 10:33:28.027')
,(1, 8 ,'2021', 'Jan', 59 ,'tester', '2023-03-16 10:33:28.027')
,(1, 9 ,'2021', 'Jan', 59 ,'tester', '2023-03-16 10:33:28.027')
,(1, 10 ,'2021', 'Jan', 59 ,'tester', '2023-03-16 10:33:28.027')insert into MPVProduct(setnum, productnum, productname, productcode, yield, reguser, regdate)
values
(1 ,1 ,'Costner-SPR', 'C11CG05506', NULL ,'tester', '2023-03-16 09:55:41.580')
,(1 ,2 ,'Costner-FPR', 'C11CG02506', NULL ,'tester', '2023-03-16 09:55:49.563')
,(1 ,3 ,'BK XLarge', 'C13T950100', 10000 ,'tester', '2023-03-16 09:58:02.630')
,(1 ,4 ,'C Large', 'C13T949200', 5000 ,'tester', '2023-03-16 09:58:02.630')
,(1 ,5 ,'M Large', 'C13T949300', 5000 ,'tester', '2023-03-16 09:58:02.630')
,(1 ,6 ,'Y Large', 'C13T949400', 5000 ,'tester', '2023-03-16 09:58:02.630')
,(1 ,7 ,'BK Std', 'C13T948100', 3000 ,'tester', '2023-03-16 09:58:02.630')
,(1 ,8 ,'C Std', 'C13T948200', 3000 ,'tester', '2023-03-16 09:58:02.630')
,(1 ,9 ,'M Std', 'C13T948300', 3000 ,'tester', '2023-03-16 09:58:02.630')
,(1 ,10 ,'Y Std', 'C13T948400', 3000 ,'tester', '2023-03-16 09:58:02.630')cmd_comment_vote_user Upvote0 Downvote0 Comment Update Delete
-
지영아빠
2023.03.21 15:31
칼럼을 동적으로 한다면 해당년의 년월일을 distinct하게 구성한다음 pivot의 동적 칼럼으로구성하시면 될것 같습니다.
select
distinct outputYear + '_' + outputMonth as outputYYMM
from MPVData
select *
from (
select a.setnum
,a.ProductNum
,a.ProductName
,a.ProductCode
,a.Yield
,b.OutputQty
,b.outputYYMM
,SUM(b.OutputQty) OVER(partition by b.outputProductNum) AS SUM_Product
from MPVProduct A,
(select
outputSetNum, OutputProductNum, outputYear + '_' + outputMonth as outputYYMM, outputQty, regUser, regDAte
from MPVData) B
where a.SetNum = b.OutputSetNum
and a.ProductNum = b.OutputProductNum
) As result
Pivot
(
sum(OutputQty) for outputYYMM in (
[2020_May], [2021_Jan] -- 여기 동적 칼럼을 넣으면 될듯요
)
) as pivot_result
-
Just
2023.03.21 15:52
도움주셔서 감사합니다^^
[2020_May], [2021_Jan] -- 여기 동적 칼럼을 넣으면 될듯요
위 부분에 날짜 데이터가 계속 쌓이게 되면 배열로 만들어서 호출하면 될까요?
cmd_comment_vote_user Upvote0 Downvote0 Comment Update Delete
-
지영아빠
2023.03.21 16:20
declare @colstring varchar(4000) = '' select @colstring = STRING_AGG( '[' + outputYYMM + ']' , ',' ) from ( select distinct outputYear + '_' + outputMonth as outputYYMM from MPVData ) x declare @out_head varchar(4000) = ' select * from ( select a.setnum ,a.ProductNum ,a.ProductName ,a.ProductCode ,a.Yield ,b.OutputQty ,b.outputYYMM ,SUM(b.OutputQty) OVER(partition by b.outputProductNum) AS SUM_Product from MPVProduct A, (select outputSetNum, OutputProductNum, outputYear + ''_'' + outputMonth as outputYYMM, outputQty, regUser, regDAte from MPVData) B where a.SetNum = b.OutputSetNum and a.ProductNum = b.OutputProductNum ) As result Pivot ( sum(OutputQty) for outputYYMM in (' declare @out_tail varchar(4000) = ') ) as pivot_result' exec(@out_head + @colstring + @out_tail)
-
Just
2023.03.21 16:38
너무너무 감사드립니다^^ 많이 배웠습니다!
cmd_comment_vote_user Upvote0 Downvote0 Comment Update Delete
MPVPRoduct, MPVData 샘플데이터를 각각 줘보시겠어요?