我有三个表Employee
,Loan
和LoanInstallment
。
Employee
表与Loan
有一对多关系,Loan
有一个与LoanInstallment
许多人Employee
。
Loan
(EmpId,Name,IsOnProbation)SELECT
e.EmployeeID, E.FullName,l.EndDate,
(CASE
WHEN DATEDIFF(YEAR, max(l.EndDate), GETDATE()) < 0
THEN 'Eligible'
ELSE 'Not Eligible'
END) as Status
FROM
Employee e
LEFT JOIN
Loan l ON e.EmployeeID = l.EmployeeID
GROUP BY
e.EmployeeID, e.FullName, l.EndDate
(LoanId,EmpId,StartDate,EndDate)。现在我需要编写一个查询来获取以下输出中的employee记录。
输出记录(EmpId,名称,状态,原因)
规则
如果员工从未获得贷款,那么其状态应该是合格的,并且理由是未获得贷款。
如果员工在一年内获得贷款(即EndDate少于一年),那么其状态应该是不合格的,并且已经采取理由贷款。
如果员工处于试用期,那么状态应该是不合格的和缓刑原因
如果员工已经在1年前服用了laon,那么状态应该是1年前的合格和原因贷款。
我编写了一个简单的查询但我无法理解如何包含所有四个规则并在此单个查询中包含Reason列。
$("#image1").on('change', function(event) {
$('#myform').trigger('submit');
alert("button clicked"); // this is submitted
$("#myform").submit(function(e) { // this is not happening
event.preventDefault();
alert('form clicked');
//var formId=("#myform").submit();
$.ajax({
type: 'GET', // GET or POST or PUT or DELETE
// verb
url: "/bin/mr/controller?q=iechange",
data: $("#myform").serialize(),
// type
// sent
// to
// server
dataType: 'json', // Expected data format from
// server
processdata: true, // True or False
success: function(data) { // On Successfull
alert('call success');
console.log(data);
},
error: function(msg) { // When Service call
alert('call fail'); // fails
}
});
});
});
答案 0 :(得分:1)
您可以将其余条件添加到案例陈述中。
对于Reason列,您的case语句将基本相同,而不是您需要设置原因的状态。
此外,case when DATEDIFF(YEAR, max(l.EndDate), GETDATE()) < 0
错误,因为结果永远不会小于0.
这应该这样做:
select e.EmployeeID, E.FullName,l.EndDate,
(case when l.EmployeeID is null then 'Eligible'
when DATEDIFF(month, max(l.EndDate), GETDATE()) > 12 then 'Eligible'
when DATEDIFF(month, max(l.EndDate), GETDATE()) =< 12 then 'Not Eligible'
when l.IsOnProbation = 1 then 'Not Eligible'
else 'Not Eligible'
end) as Status,
(case when l.EmployeeID is null then 'Loan not taken'
when DATEDIFF(month, max(l.EndDate), GETDATE()) > 12 then 'Loan taken over 1 year ago'
when DATEDIFF(month, max(l.EndDate), GETDATE()) <= 12 then 'Loan already taken'
when l.IsOnProbation = 1 then 'On probation'
else 'Not Eligible'
end) as Reason
FROM Employee e
left join Loan l on e.EmployeeID = l.EmployeeID
group by e.EmployeeID, e.FullName, l.EndDate