CREATE VIEW [dbo].[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 AS 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 AS surfaceItemId, ISNULL(U.name + ' ' + U.surname, '') AS createdByName
FROM            dbo.pal_Campaign AS C INNER JOIN
                         dbo.pal_CampaignMessage AS M ON M.campaignId = C.recId INNER JOIN
                         dbo.pal_CampaignRecipient AS R ON R.campaignId = C.recId AND R.isIncluded = 1 AND R.recipientNumber = M.mobile INNER JOIN
                         dbo.pal_SurfaceItem AS I ON I.recId = R.surfaceItemId INNER JOIN
                         dbo.pal_CampaignType AS T ON T.recId = C.campaignType LEFT OUTER JOIN
                         dbo.pal_vUserShared AS U ON U.recId = C.createdBy
