SQL - 如果count result = 0,则运行第二个Query

时间:2016-07-07 06:14:19

标签: sql sql-server

我的SELECT查询都在下面工作,并返回两个单独结果的值。我想要做的是,如果第一个SELECT语句返回0并且有时由于我们工作的性质,我希望第二个忽略第一个返回零的可能。如果第一个返回大于零的结果,那么我不希望第二个运行。

我一直在寻找和尝试不同的IF ELSE等,但由于使用的具体日期/时间公式,我认为这可能是不可能的,或者我可能必须完全重写。有一个目的,但我不会厌烦你。提前谢谢!

Declare @StartDate as DateTime
Declare @EndDate as DateTime
Declare @TodaysDate as DateTime
Declare @Previous as DateTime
Declare @Previous2 as DateTime

set @TodaysDate = GETDATE()
set @Previous = DATEADD(day,-1,@TodaysDate)
set @Previous2 = DATEADD(day,-2,@TodaysDate)

-- SELECT Statetment one starts here

set @StartDate = cast(convert(varchar(4), datepart(yyyy, getdate())) + '-' +
convert(varchar(2), datepart(mm, @Previous)) + '-' +
convert(varchar(2), datepart(dd, @Previous)) + ' ' +
'05:00' as datetime)
set @EndDate = cast(convert(varchar(4), datepart(yyyy, getdate())) + '-' +
convert(varchar(2), datepart(mm, @Previous)) + '-' +
convert(varchar(2), datepart(dd, @Previous)) + ' ' +
'16:59' as datetime)

SELECT Count(*) as FirstShfitPrevious
FROM [TOL_PROD_DB].[dbo].[tblLOT_CTRL_Active_Component] 
WHERE  Close_Time_Stamp between @StartDate and @EndDate

-- Query 2 Starts, Declarations already made at beginning

set @StartDate = cast(convert(varchar(4), datepart(yyyy, getdate())) + '-' +
convert(varchar(2), datepart(mm, @Previous2)) + '-' +
convert(varchar(2), datepart(dd, @Previous2)) + ' ' +
'05:00' as datetime)
set @EndDate = cast(convert(varchar(4), datepart(yyyy, getdate())) + '-' +
convert(varchar(2), datepart(mm, @Previous2)) + '-' +
convert(varchar(2), datepart(dd, @Previous2)) + ' ' +
'16:59' as datetime)

SELECT Count(*) as FirstShfitPrevious2
FROM [TOL_PROD_DB].[dbo].[tblLOT_CTRL_Active_Component] 
WHERE  Close_Time_Stamp between @StartDate and @EndDate

3 个答案:

答案 0 :(得分:0)

您可以尝试将第一个查询结果的值存储在变量中,然后检查值

declare @myVar int

SELECT @myVar = Count(*) as FirstShfitPrevious
FROM [TOL_PROD_DB].[dbo].[tblLOT_CTRL_Active_Component] 
WHERE  Close_Time_Stamp between @StartDate and @EndDate)

if(@myVar = 0)
//Here your second query logic
begin
set @StartDate = cast(convert(varchar(4), datepart(yyyy, getdate())) + '-' +
convert(varchar(2), datepart(mm, @Previous2)) + '-' +
convert(varchar(2), datepart(dd, @Previous2)) + ' ' +
'05:00' as datetime)
set @EndDate = cast(convert(varchar(4), datepart(yyyy, getdate())) + '-' +
convert(varchar(2), datepart(mm, @Previous2)) + '-' +
convert(varchar(2), datepart(dd, @Previous2)) + ' ' +
'16:59' as datetime)

SELECT Count(*) as FirstShfitPrevious2
FROM [TOL_PROD_DB].[dbo].[tblLOT_CTRL_Active_Component] 
WHERE  Close_Time_Stamp between @StartDate and @EndDate
end

答案 1 :(得分:0)

使用NOT存在..

if NOT Exists
(Select 1  as FirstShfitPrevious--first query
FROM [TOL_PROD_DB].[dbo].[tblLOT_CTRL_Active_Component] 
WHERE  Close_Time_Stamp between @StartDate and @EndDate
)
Begin
SELECT Count(*) as FirstShfitPrevious2--second query
FROM [TOL_PROD_DB].[dbo].[tblLOT_CTRL_Active_Component] 
WHERE  Close_Time_Stamp between @StartDate and @EndDate
END
Else 
Begin
--First query
End

答案 2 :(得分:0)

甚至那样:

SELECT TOP 1 Count(*) as FirstShfitPrevious2
FROM [TOL_PROD_DB].[dbo].[tblLOT_CTRL_Active_Component] 
WHERE  Close_Time_Stamp between @StartDate and @EndDate
GROUP BY CAST(Close_Time_Stamp AS DATE)
ORDER BY CAST(Close_Time_Stamp AS DATE) DESC

期间涵盖两个日期范围。