Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Combining multiple records into a single record (View)

$
0
0

Okay so here is my scenerio:

There is a table I have made that details SQL jobs and the various details about them. What i wan't to do is make a view with specific logic implemented. Fields that will be taken across to this view will be: LastRunDate, LastRunTime and Step_Status. JobName will also be used to define the logic but will not be a column in itself.

What i wan't is an additional column using this logic, called 'DataSet'. For each Dataset there are multiple jobs that are unique to it, so there are multiple records being brought over from the table to view for one particular dataset. For example, for 'ExampleDataSet' there are three jobs, i wan't it so these jobs are combined and named as 'ExampleDataSet' under a calculated column called 'Dataset'. For the following fields i would like it so that LastRunDate and LastRunTime are the time in whichever job was last to be intitiated is the date and time set. For Step_Status, there are three possible results, 'Running', 'Failed' or 'Succeeded'. If any of the three jobs are 'Running', then 'Running'. If neither are running yet one has failed then 'Failed'. Only when all three have succeeded should it then be set to 'Succeeded'.

I hope i have made that clear enough, please feel free to ask any questions if not. I am relatively new to SQL but i thought to at least try least try (I knew it wouldn't work) and it didn't work. But it may give you a better idea of what i wan't:

CREATE VIEW [dbo].[vw_DataAvailabilityAnalysts] AS

SELECT CASE
WHEN JobName in ('Job1','Job2','Job3') and step_status='Running' THEN 'Running'
WHEN JobName in ('Job1','Job2','Job3') and step_status='Failed' THEN 'Failed'
WHEN JobName in ('Job1','Job2','Job3) and step_status='Succeeded' THEN 'Succeeded'
END as StatusCheck,

CASE WHEN JobName in ('Job1','Job2','Job3') THEN 'ExampleDataSet'
END as DATASET,
LastRunDate,
lastruntime,
step_status,
FROM [CDS_Common].[dbo].[DataAvailability]

Many Thanks,

Plain_Clueless


Plain_Clueless


Viewing all articles
Browse latest Browse all 23857

Trending Articles