Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
sqlserver2000中的秩查询_Sql_Sql Server_Database_Sql Server 2000_Ranking - Fatal编程技术网

sqlserver2000中的秩查询

sqlserver2000中的秩查询,sql,sql-server,database,sql-server-2000,ranking,Sql,Sql Server,Database,Sql Server 2000,Ranking,我正在尝试创建一个查询,该查询将显示作业ID的最后状态。在SQL Server 2008中,使用“按分区排序”函数很简单,但在SQL Server 2000中,此函数不可用。 我在临时表job_history中通过job_ID升序和DATETIME descening将job_history按顺序排列-注意,pk_ID是在临时表中使用:IDENTITYint,1,1生成的,但这是我遇到的问题,我不知道如何对该表中的记录进行排序 SELECT h1.pk_ID, h1.Job_ID,

我正在尝试创建一个查询,该查询将显示作业ID的最后状态。在SQL Server 2008中,使用“按分区排序”函数很简单,但在SQL Server 2000中,此函数不可用。 我在临时表job_history中通过job_ID升序和DATETIME descening将job_history按顺序排列-注意,pk_ID是在临时表中使用:IDENTITYint,1,1生成的,但这是我遇到的问题,我不知道如何对该表中的记录进行排序

SELECT 
  h1.pk_ID,
  h1.Job_ID,
  h1.Status,
  h1.DATETIME
FROM #JOB_HISTORY h1
ORDER BY h1.pk_ID ASC;
作业历史记录结果:

期望输出:


SQL Server可以自由地按照它想要的顺序插入记录,所以我不相信Identitynt,1,1总能为您提供正确的顺序。不过,它在某些版本中可能会起作用

您可以内部连接一个子查询,从中获取最大日期时间或最小主键ID以获取第一行/最后一行。下面是获取SQL Server 2000中每个作业的最后结果的工作示例:

use msdb;

select
    sysjobhistory.job_id                            as  job_id
    --  https://msdn.microsoft.com/en-us/library/ms174997.aspx
,   case    sysjobhistory.run_status
        when    0   then    'Failed'
        when    1   then    'Succeeded'
        when    2   then    'Retry'
        when    3   then    'Canceled'
        else                cast(sysjobhistory.run_status as varchar(10))
    end                                             as  run_status
,   jobLastHistory.lastDateTime                     as  execution_time
FROM    dbo.sysjobhistory   as  sysjobhistory
    inner join (
        --last datetime for each job_id
        SELECT
            history.job_id
        ,   max(                                        --newest
                CONVERT(DATETIME, STUFF(STUFF(STUFF(    --yyyymmddhhmiss --> datetime
                    cast(history.run_date as varchar(8))--yyyymmdd
                    +right('000000'+cast(history.run_time as varchar(8)),6) --hhmiss
                ,13,0,':'),11,0,':'),9,0,' '))  
            )                                           AS  lastDateTime
        FROM    dbo.sysjobhistory   as  history
        where   history.step_id =   0   --job outcome
        group by
            history.job_id
    )   as  jobLastHistory
        on  jobLastHistory.job_id   
            =   sysjobhistory.job_id    --same job
        and convert(varchar(8),jobLastHistory.lastDateTime,112)
            =   sysjobhistory.run_date  --latest date (yyyymmdd)
        and replace(convert(varchar(8),jobLastHistory.lastDateTime,108),':','') 
            =   sysjobhistory.run_time  --latest time (hhmiss)
where   sysjobhistory.step_id   =   0   --job outcome
;

您可以通过自连接来实现这一点,但它确实很糟糕,而且效率极低,随着表的增大,成本/持续时间呈指数级增长。像2000这样一个残废的、不受支持的版本,你还要运行多久?我们没有升级的预算。使用SQL Server的前端程序不支持较新的SQL Server技术:我尝试了这个查询,但它不起作用,您将如何进行选择?选择h1.pk_ID,h1.Job_ID,h1.STATUS,h1.DATETIME FROM Job_hyst h1,Job_hyst h2,其中h2.DATETIMEJob_ID STATUS DATETIME 1234 Succeded. 2015-03-30 12:10 5678 Failed. 2015-04-02 04:00
use msdb;

select
    sysjobhistory.job_id                            as  job_id
    --  https://msdn.microsoft.com/en-us/library/ms174997.aspx
,   case    sysjobhistory.run_status
        when    0   then    'Failed'
        when    1   then    'Succeeded'
        when    2   then    'Retry'
        when    3   then    'Canceled'
        else                cast(sysjobhistory.run_status as varchar(10))
    end                                             as  run_status
,   jobLastHistory.lastDateTime                     as  execution_time
FROM    dbo.sysjobhistory   as  sysjobhistory
    inner join (
        --last datetime for each job_id
        SELECT
            history.job_id
        ,   max(                                        --newest
                CONVERT(DATETIME, STUFF(STUFF(STUFF(    --yyyymmddhhmiss --> datetime
                    cast(history.run_date as varchar(8))--yyyymmdd
                    +right('000000'+cast(history.run_time as varchar(8)),6) --hhmiss
                ,13,0,':'),11,0,':'),9,0,' '))  
            )                                           AS  lastDateTime
        FROM    dbo.sysjobhistory   as  history
        where   history.step_id =   0   --job outcome
        group by
            history.job_id
    )   as  jobLastHistory
        on  jobLastHistory.job_id   
            =   sysjobhistory.job_id    --same job
        and convert(varchar(8),jobLastHistory.lastDateTime,112)
            =   sysjobhistory.run_date  --latest date (yyyymmdd)
        and replace(convert(varchar(8),jobLastHistory.lastDateTime,108),':','') 
            =   sysjobhistory.run_time  --latest time (hhmiss)
where   sysjobhistory.step_id   =   0   --job outcome
;