结合选择语句

时间:2017-08-18 17:45:41

标签: asp.net sql-server stored-procedures

我正在构建一个仪表板屏幕,需要多个sql语句才能返回数据。但是,所有这些数据库调用都在增加性能方面。

现在代码调用在线意味着我正在我的代码中进行数据库调用。我想将所有数据库调用移动到存储过程,但我不知道如何将所有数据库调用组合到一个存储过程中以返回多个值,因为我必须单独显示每个值。

有人可以告诉我如何将这些select语句组合成一个并返回多个值,以便在我的代码中使用。

/*Pending Count */
Select Count(*)
FROM Issue
WHERE Issue.WorkerKeyFk =  lngWorkerKey
      And Issue.ClosedDate is Null

/* Get Today's Issue Count */             
Select Count(*)
FROM Issue
WHERE Issue.WorkerKeyFk =  lngWorkerKey
      And Issue.IssueDate =  FormatDateTime(dateTimes, DateFormat.ShortDate)

/* Over Due Issue Count */
Select Count(*) 
FROM Issue
WHERE Issue.WorkerKeyFk =  lngWorkerKey
      And Issue.DueDate <  FormatDateTime(dateTimes, DateFormat.ShortDate)
      And Issue.ClosedDate is null       

/*Get Closed Issue Count */
Select Count(*)
FROM Issue
WHERE Issue.WorkerKeyFk = lngWorkerKey
      And Issue.ClosedDate is not null 

2 个答案:

答案 0 :(得分:0)

我将如何做到这一点:

SELECT
    Sum(Iif(Issue.IssueDate = FormatDateTime(dateTimes, DateFormat.ShortDate), 1, 0) as Today_Issue_Count
    , Sum(Iif(Issue.DueDate < FormatDateTime(dateTimes, DateFormat.ShortDate) And Issue.ClosedDate is null, 1, 0) as Overdue_Issue_Count
    , Sum(Iif(Issue.ClosedDate is not null, 1, 0) as Closed_Issue_Count
FROM Issue
    WHERE Issue.WorkerKeyFk = lngWorkerKey

这样你只需要处理一个结果集,服务器上的执行速度应该很快。

答案 1 :(得分:0)

Select Count(*), 'Pending' as Count_Label
FROM Issue
WHERE Issue.WorkerKeyFk =  lngWorkerKey
      And Issue.ClosedDate is Null

Union All           
Select Count(*), 'Today' as Count_Label
FROM Issue
WHERE Issue.WorkerKeyFk =  lngWorkerKey
      And Issue.IssueDate =  FormatDateTime(dateTimes, DateFormat.ShortDate)

union all
Select Count(*), 'Overdue' as Count_Label
FROM Issue
WHERE Issue.WorkerKeyFk =  lngWorkerKey
      And Issue.DueDate <  FormatDateTime(dateTimes, DateFormat.ShortDate)
      And Issue.ClosedDate is null       

union all
Select Count(*), 'Closed' as Count_Label
FROM Issue
WHERE Issue.WorkerKeyFk = lngWorkerKey
      And Issue.ClosedDate is not null