﻿USE [FabApprovalSystem]
GO
	/****** Object:  StoredProcedure [dbo].[CCGetMeetingDecisionSummaryList]    Script Date: 11/21/2024 11:29:04 AM ******/
SET
	ANSI_NULLS ON
GO
SET
	QUOTED_IDENTIFIER ON
GO
	-- =============================================
	-- Author:		<Author,,Name>
	-- Create date: <Create Date,,>
	-- Description:	<Description,,>
	-- =============================================
	CREATE PROCEDURE [dbo].[CCGetMeetingDecisionSummaryList] -- Add the parameters for the stored procedure here
	@PlanNumber INT AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
SET
	NOCOUNT ON;

-- Insert statements for procedure here
/****** Script for SelectTopNRows command from SSMS  ******/
SELECT
	DISTINCT P.PCRValues AS PCRB,
	M.MeetingDate,
	CCDecisionNotes.DecisionNotes,
	CCECNLink.ECNs AS ECNLinks,
	CCLDLink.LDs AS LotDispoLinks
FROM
	CCChangeControl C
	INNER JOIN CCMeeting M ON C.PlanNumber = M.PlanNumber
	LEFT JOIN vCCMeetingPCRs P ON M.MeetingID = P.MeetingID
	LEFT JOIN CCDecisionSummary D ON M.MeetingID = D.MeetingID
	LEFT JOIN (
		SELECT
			DISTINCT MM.MeetingID,
			STUFF (
				(
					SELECT
						'~' + LTRIM(RTRIM(CAST(DecisionNotes AS VARCHAR(500))))
					FROM
						CCDecisionSummary CCD
					WHERE
						CCD.MeetingID = MM.MeetingID FOR XML PATH('')
				),
				1,
				1,
				''
			) AS DecisionNotes
		FROM
			CCMeeting MM
	) AS CCDecisionNotes ON M.MeetingID = CCDecisionNotes.MeetingID
	LEFT JOIN (
		SELECT
			DISTINCT MM.MeetingID,
			STUFF (
				(
					SELECT
						'~ ' + LTRIM(RTRIM(CAST(ECNLinks AS VARCHAR(100))))
					FROM
						CCDecisionSummary CCD
					WHERE
						CCD.MeetingID = MM.MeetingID FOR XML PATH('')
				),
				1,
				1,
				''
			) AS ECNs
		FROM
			CCMeeting MM
	) AS CCECNLink ON M.MeetingID = CCECNLink.MeetingID
	LEFT JOIN (
		SELECT
			DISTINCT MM.MeetingID,
			STUFF (
				(
					SELECT
						'~ ' + LTRIM(RTRIM(CAST(LotDispoLinks AS VARCHAR(100))))
					FROM
						CCDecisionSummary CCD
					WHERE
						CCD.MeetingID = MM.MeetingID FOR XML PATH('')
				),
				1,
				1,
				''
			) AS LDs
		FROM
			CCMeeting MM
	) AS CCLDLink ON M.MeetingID = CCLDLink.MeetingID
WHERE
	C.PlanNumber = @PlanNumber
END
GO