Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 SQL select help - filtering conditions

Author  Topic 

Drak
Starting Member

1 Post

Posted - 2015-03-11 : 05:40:50
Hi guys,

I am quite new to the SQL, so if somebody is interested to help, I will be really glad. I need prepare some daily statistics from the database about running jobs. It is just some filtering / limitation conditions (I hope)


all records I need to filter are just from one table named ho.async_task

there are many columns in it, however I need just following to be displayed:

name I need / name of the column in db from ho.async_task

Job ID = ID (example: 1050711323)
Job Name = SCHEDULER_TEMPLATE_CODE (example: JOB_NAMEDA)
Start Time = START_DATE (example: 19.09.2014 3:00:00)
End Time = END_DATE (example: 19.09.2014 3:00:12)
Run time = should be difference between START_DATE and END_DATE in seconds. (example: 12)
Status = STATUS (example: F)
Error message = ERROR_MESSAGE (example: Error description)


-- Run Time field is not in db, should be difference between start / end date in seconds

-- I need track only these SCHEDULER_TEMPLATE_CODE
JOB_NAMEDA,JOB_NAMEDANOTHER, JOB_NAMELESS, JOB_ANOTHER_EXAMPLE

-- because some jobs (Scheduler_template_code) could have more records daily, in such an example I need display just last record

-- whole SQL select should be somehow easy filterable by particular day (or possibility to display records X day back when changing some parm inside the script)


so the output should be like this for given day

Job ID--Job Name--Start Time--End Time--RUN time--Status--Error
111--JOB_NAMEDA---3:00:00--3:00:12------12--------F-------blank
213--JOB_NAMEDANOTHER--etc...
898--JOB_NAMELESS-- etc...
875--JOB_ANOTHER_EXAMPLE--etc...

thanks for any help

Ifor
Aged Yak Warrior

700 Posts

Posted - 2015-03-11 : 08:20:48
When posting a question, you should really include some consumable test data that illustrates your problem.
With the information provided something like:

-- *** Test Data ***
CREATE TABLE #t
(
Id int NOT NULL
,SCHEDULER_TEMPLATE_CODE varchar(20) NOT NULL
,[START_DATE] datetime NOT NULL
,END_DATE datetime NOT NULL
,[STATUS] char(1) NOT NULL
,[ERROR_MESSAGE] varchar(20) NOT NULL
);
INSERT INTO #t
-- dates in ISO format!
VALUES (1050711323, 'JOB_NAMEDA', '20140919 3:00:00', '20140919 3:00:12', 'F', 'Error description');
-- *** End Test Data ***

This data does not really illustrate what you want to do.

Try playing with something like:

WITH JobOrder
AS
(
SELECT *
,ROW_NUMBER() OVER (PARTITION BY DATEADD(day, DATEDIFF(day, 0, [START_DATE]), 0) ORDER BY [START_DATE] DESC) AS rn
FROM #t
WHERE SCHEDULER_TEMPLATE_CODE IN ('JOB_NAMEDA', 'JOB_NAMEDANOTHER', 'JOB_NAMELESS', 'JOB_ANOTHER_EXAMPLE')
)
SELECT DATEADD(day, DATEDIFF(day, 0, [START_DATE]), 0) AS StartDay
,Id AS JobID
,SCHEDULER_TEMPLATE_CODE AS JobName
,CAST([START_DATE] AS time) AS StartTime
,CAST(END_DATE AS time) AS EndTime
,DATEDIFF(s, [START_DATE], END_DATE) AS RunTime
,[STATUS]
,[ERROR_MESSAGE]
FROM JobOrder
WHERE rn = 1;
Go to Top of Page
   

- Advertisement -