USE [DD_ECM]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE OR ALTER VIEW [dbo].[VWREC_RESULT]
AS
SELECT TOP (100) PERCENT
            [OUT_RESULT].[GUID]             as 'RESULT_GUID',
            [OUT_RESULT].[ACTION_ID]        as 'ACTION_ID',
            [CFG_ACTION].[PROFILE_ID]       as 'PROFILE_ID',
            [CFG_PROFILE].[PROFILE_NAME]    as 'PROFILE_NAME',
            [OUT_RESULT].[STATUS_ID]        as 'STATUS_ID',
            CASE
				-- None HTTP codes
                WHEN [OUT_RESULT].[STATUS_ID] = 0   THEN 'OK'
				WHEN [OUT_RESULT].[STATUS_ID] = 999 THEN 'Error'
				-- 1xx Informational
                WHEN [OUT_RESULT].[STATUS_ID] = 100 THEN 'Continue'
                WHEN [OUT_RESULT].[STATUS_ID] = 101 THEN 'Switching Protocols'
                WHEN [OUT_RESULT].[STATUS_ID] = 103 THEN 'Early Hints'
                -- 2xx Success
                WHEN [OUT_RESULT].[STATUS_ID] = 200 THEN 'OK'
                WHEN [OUT_RESULT].[STATUS_ID] = 201 THEN 'Created'
                WHEN [OUT_RESULT].[STATUS_ID] = 202 THEN 'Accepted'
                WHEN [OUT_RESULT].[STATUS_ID] = 204 THEN 'No Content'
                WHEN [OUT_RESULT].[STATUS_ID] = 206 THEN 'Partial Content'
                -- 3xx Redirection
                WHEN [OUT_RESULT].[STATUS_ID] = 301 THEN 'Moved Permanently'
                WHEN [OUT_RESULT].[STATUS_ID] = 302 THEN 'Found'
                WHEN [OUT_RESULT].[STATUS_ID] = 304 THEN 'Not Modified'
                WHEN [OUT_RESULT].[STATUS_ID] = 307 THEN 'Temporary Redirect'
                WHEN [OUT_RESULT].[STATUS_ID] = 308 THEN 'Permanent Redirect'
                -- 4xx Client Error
                WHEN [OUT_RESULT].[STATUS_ID] = 400 THEN 'Bad Request'
                WHEN [OUT_RESULT].[STATUS_ID] = 401 THEN 'Unauthorized'
                WHEN [OUT_RESULT].[STATUS_ID] = 403 THEN 'Forbidden'
                WHEN [OUT_RESULT].[STATUS_ID] = 404 THEN 'Not Found'
                WHEN [OUT_RESULT].[STATUS_ID] = 405 THEN 'Method Not Allowed'
                WHEN [OUT_RESULT].[STATUS_ID] = 408 THEN 'Request Timeout'
                WHEN [OUT_RESULT].[STATUS_ID] = 429 THEN 'Too Many Requests'
                WHEN [OUT_RESULT].[STATUS_ID] = 451 THEN 'Unavailable For Legal Reasons'
                -- 5xx Server Error
                WHEN [OUT_RESULT].[STATUS_ID] = 500 THEN 'Internal Server Error'
                WHEN [OUT_RESULT].[STATUS_ID] = 501 THEN 'Not Implemented'
                WHEN [OUT_RESULT].[STATUS_ID] = 502 THEN 'Bad Gateway'
                WHEN [OUT_RESULT].[STATUS_ID] = 503 THEN 'Service Unavailable'
                WHEN [OUT_RESULT].[STATUS_ID] = 504 THEN 'Gateway Timeout'
                WHEN [OUT_RESULT].[STATUS_ID] = 511 THEN 'Network Authentication Required'
                ELSE 'UNKNOWN STATUS'
            END                             as 'STATUS',
            [OUT_RESULT].[RESULT_TYPE_ID]   as 'RESULT_TYPE_ID',
            CASE
                WHEN [OUT_RESULT].[RESULT_TYPE_ID] = 1 THEN 'PRE'
                WHEN [OUT_RESULT].[RESULT_TYPE_ID] = 2 THEN 'MAIN'
                WHEN [OUT_RESULT].[RESULT_TYPE_ID] = 3 THEN 'POST'
                ELSE 'INVALID'
            END                             as 'RESULT_TYPE',
            [OUT_RESULT].[RESULT_HEADER]    as 'RESULT_HEADER',
            [OUT_RESULT].[RESULT_BODY]      as 'RESULT_BODY',
            [OUT_RESULT].[RESULT_INFO]      as 'RESULT_INFO',
            [OUT_RESULT].[RESULT_ERROR]     as 'RESULT_ERROR',
            [OUT_RESULT].[ADDED_WHO]        as 'ADDED_WHO',
            [OUT_RESULT].[ADDED_WHEN]       as 'ADDED_WHEN',
            [OUT_RESULT].[CHANGED_WHO]      as 'CHANGED_WHO',
            [OUT_RESULT].[CHANGED_WHEN]     as 'CHANGED_WHEN'

FROM        [dbo].[TBREC_OUT_RESULT]  as [OUT_RESULT]  WITH (NOLOCK)
LEFT JOIN   [dbo].[TBREC_CFG_ACTION]  as [CFG_ACTION]  WITH (NOLOCK) ON [OUT_RESULT].[ACTION_ID]  = [CFG_ACTION].[GUID]
LEFT JOIN   [dbo].[TBREC_CFG_PROFILE] as [CFG_PROFILE] WITH (NOLOCK) ON [CFG_ACTION].[PROFILE_ID] = [CFG_PROFILE].[GUID]

ORDER BY    [OUT_RESULT].[GUID];

GO