在SQL Server中捕获多种类型的数据

时间:2017-04-12 11:20:31

标签: sql sql-server sql-server-2008

我有一张表(Task),如下所示:

Task Table

我需要这样回答:

TaskResult

我正在做第一个这样的查询:

select 
    StudentID, AdmissionID, EnquiryID, EnquiryDetailsID
from  
    Task
where 
    TaskUser = 0 and BranchID = 1
  • 如果我正在获取studentID,那么我会在循环中创建第二个查询,以搜索学生的名字和姓氏。

  • else如果我收到了EnquiryID,那么我会在循环中创建第二个查询来搜索查询的名字和姓氏。

  • elseif我正在获取AdmissionID然后我在循环中创建第二个查询,用于搜索Admission的名字和姓氏。

  • else如果我收到了EnquiryDetailsID,那么我会在循环中创建第二个查询来搜索EnquiryDetails的名字和姓氏。

因此它在循环中创建循环,并且我在页面上加载了很多时间。

我需要将两个查询合并到一个查询中。因此页面不会加载。

我只有两个元素,即taskUser和BranchID。

请帮帮我!!在此先感谢!!!

2 个答案:

答案 0 :(得分:0)

COALESCE--not tested select StudentID, AdmissionID, EnquiryID, EnquiryDetailsID, COALESCE(s.name, e.name, d.name, ed.name) as name, etc. from Task t left join student s on s.id = t.studentID left join Enquiry e on e.id = t.EnquiryID left join Admission d on d.id = t.AdmissionID left join EnquiryDetails ed on ed.id = t.EnquiryDetailsID where TaskUser=0 and BranchID=1 一起使用,如下所示:

//This is class method
calculateStats (selectedStatus, allStatuses) {

 allStatuses.forEach((productStatus) => {
 if (productStatus.product.id === selectedStatus.product.parentId) {   
      //Some other function calls and small calculation
      //I we ended up here three times
      //Return statement will be called three times
      return this.calculateStats(productStatus, allStatuses);
     }

 });

 return this.wrapper(this.startPoint, this.finalProductStatus);     
}

答案 1 :(得分:0)

所以 - 看起来你有一个奇怪的组织任务表,结果,你将不得不做一些有点奇怪的事情来进行查询。根据您的描述,任务表中的一行包含studentId,admissionId,enquiryId或enquiryDetailId。这不是一个最佳的方法......但我明白,有时你必须得到你所拥有的东西。

因此,要获取名称,您必须加入名称来源......并假设他们遍布整个地方,在相关表格中,您可以执行以下操作:

select 
  t.StudentID,t.AdmissionID,t.EnquiryID,t.EnquiryDetailsID,x.FirstName,x.LastName
from Task t inner join Student s on t.StudentId = s.Id
union all
select 
  t.StudentID,t.AdmissionID,t.EnquiryID,t.EnquiryDetailsID,x.FirstName,x.LastName
from Task t inner join Admission a on t.AdmissionId = a.Id
union all 
select 
  t.StudentID,t.AdmissionID,t.EnquiryID,t.EnquiryDetailsID,x.FirstName,x.LastName
from Task t inner join Enquiry e on t.EnquiryId = e.Id
union all 
select 
  t.StudentID,t.AdmissionID,t.EnquiryID,t.EnquiryDetailsID,x.FirstName,x.LastName
from Task t inner join EnquiryDetail d on t.EnquiryDetailId = d.Id

......或者,你可以从内到外完成同样的事情:

select 
  t.StudentID, 
  t.AdmissionID, 
  t.EnquiryID, 
  t.EnquiryDetailsID,
  x.FirstName,
  x.LastName
from 
  Task t
  inner join
  (
    select 's' source, Id, FirstName, LastName from Student union all
    select 'a' source, Id, FirstName, LastName from Admission union all
    select 'e' source, Id, FirstName, LastName from Enquiry union all
    select 'd' source, Id, FirstName, LastName from EnquiryDetail
  ) as x
  on
    ( t.StudentId  = x.Id and x.source = 's' )
    or
    ( t.AdmissionId = x.Id and x.source = 'a' )
    or
    ( t.EnquiryId = x.Id and x.source = 'e' )
    or 
    ( t.EnquiryDetailId = x.Id and x.source = 'd' )
where 
  t.TaskUser=0 and t.BranchID=1