sql server—从两个查询的结果返回0或1作为一条记录的sql查询

vu8f3i0k  于 2021-07-27  发布在  Java
关注(0)|答案(1)|浏览(298)

我有一个runhistory表,其中记录了进程运行结果。进程正在通过调度程序运行,在调度程序中它以数字形式登录runinstance,但是如果手动运行,那么它将以“手动”方式登录我需要一个查询来检查进程今天是否运行(getdate()),而不管它是手动还是通过调度程序运行。如果它运行了,那么查询应该返回1或者0作为一条记录。我已经创建了临时表和联合查询来演示这个问题。

create table #RunHistory
    (
    [RunId] [int] IDENTITY(1,1) NOT NULL,
    [ReportDate] [date] NOT NULL,
    [RunInstance] [varchar](6) NOT NULL,
    [RunStartTime] [datetime] NOT NULL,
    [RunEndTime] [datetime] NULL,
    )

INSERT INTO #RunHistory
           ([ReportDate]
           ,[RunInstance]
           ,[RunStartTime]
           ,[RunEndTime])
     VALUES
           ('2020-07-29'
           ,'1200'
           ,'2020-07-29 12:44:13.340'
           ,'2020-07-29 12:44:25.313')
    INSERT INTO #RunHistory
           ([ReportDate]
           ,[RunInstance]
           ,[RunStartTime]
           ,[RunEndTime])
     VALUES
           ('2020-07-29'
           ,'MANUAL'
           ,'2020-07-29 12:36:51.117'
           ,'2020-07-29 12:41:10.720')

           --if both ran returing 1 then it works fine
    SELECT RESULT 
    FROM  
    (
    SELECT CASE WHEN  COUNT(*) > 0 THEN 1 ELSE 0 END AS RESULT
    FROM #RunHistory
    WHERE RunEndTime IS NOT NULL AND RunInstance IS NOT NULL
        AND (ISNUMERIC(RunInstance) > 0)
        AND CONVERT(TINYINT,LEFT(RunInstance,2)) >= 8 
        AND CONVERT(DATE,#RunHistory.ReportDate) =  CONVERT(DATE,Getdate())
    UNION
    SELECT CASE WHEN  COUNT(*) > 0 THEN 1 ELSE 0 END AS RESULT
    FROM #RunHistory
    WHERE RunEndTime IS NOT NULL AND RunInstance IS NOT NULL
        AND RunInstance ='MANUAL'
        AND CONVERT(DATE,#RunHistory.ReportDate) =  CONVERT(DATE,Getdate())
    ) as z

    --if one of then returns 0 as result, then two records are returned. I just want if one of the records is 1, then return 1
    SELECT RESULT 
    FROM  
    (
    SELECT 0 AS RESULT
    FROM #RunHistory
    WHERE RunEndTime IS NOT NULL AND RunInstance IS NOT NULL
        AND (ISNUMERIC(RunInstance) > 0)
        AND CONVERT(TINYINT,LEFT(RunInstance,2)) >= 8 
        AND CONVERT(DATE,#RunHistory.ReportDate) =  CONVERT(DATE,Getdate())
    UNION
    SELECT CASE WHEN  COUNT(*) > 0 THEN 1 ELSE 0 END AS RESULT
    FROM #RunHistory
    WHERE RunEndTime IS NOT NULL AND RunInstance IS NOT NULL
        AND RunInstance ='MANUAL'
        AND CONVERT(DATE,#RunHistory.ReportDate) =  CONVERT(DATE,Getdate())
    ) as z

我不知道我是否真的需要一个联合,或者两个条件可以组合在一个查询中,或者需要一个外部查询。

velaa5lx

velaa5lx1#

你不应该受到限制 RunInstance 如果不管它是不是手动的:

select case when count(1) > 0 then 1 else 0 end result
from #RunHistory
where RunEndTime is not null
and   RunInstance is not null
and   convert(date,ReportDate) = convert(date,getdate())

如果确实需要合并多个查询,则可以获得最大结果:

select max(result)
from (
    SELECT 0 AS RESULT
    FROM #RunHistory
    WHERE RunEndTime IS NOT NULL AND RunInstance IS NOT NULL
        AND (ISNUMERIC(RunInstance) > 0)
        AND CONVERT(TINYINT,LEFT(RunInstance,2)) >= 8 
        AND CONVERT(DATE,ReportDate) =  CONVERT(DATE,Getdate())
    UNION
    SELECT CASE WHEN  COUNT(*) > 0 THEN 1 ELSE 0 END AS RESULT
    FROM #RunHistory
    WHERE RunEndTime IS NOT NULL AND RunInstance IS NOT NULL
        AND RunInstance ='MANUAL'
        AND CONVERT(DATE,ReportDate) =  CONVERT(DATE,Getdate())
) as z

相关问题