CREATE VIEW pal_vCampaignHistory
AS
SELECT	M.recId
		,C.campaignTitle
		,C.campaignBody
		,T.campaignType
		,C.isRecurring
		,C.interval
		,CASE C.messageType WHEN 1 THEN 'Email' WHEN 2 THEN 'SMS' END messageType
		,C.lastSent
		,C.nextSend
		,C.createdBy
		,C.dateSaved
		,C.dateUpdated
		,C.isActive
		,C.isDraft
		,C.contactSourceSurface
		,C.contactNumberField
		,C.contactNameField
		,C.filterField
		,C.filterValue
		,M.email
		,M.mobile
		,M.dateSent
		,M.status
		,M.statusCode
		,M.statusDisplay
		,I.recId [surfaceItemId]
FROM	pal_Campaign C
		JOIN pal_CampaignMessage M	
			ON M.campaignId = C.recId
		JOIN pal_CampaignRecipient R	
			ON R.campaignId = C.recId
			AND R.isIncluded = 1
			AND R.recipientNumber = M.mobile
		JOIN pal_SurfaceItem I
			ON I.recId = R.surfaceItemId
		JOIN pal_CampaignType T
			ON T.recId = C.campaignType
