SELECT
A.First_Name, b.First_Name, A.DOB, B.DOB, A.Token, B.Token,
DATEDIFF(YEAR, A.DOB, B.DOB) -
(CASE
WHEN DATEADD(YY, DATEDIFF(YEAR, A.DOB, B.DOB), A.DOB) > Getdate()
THEN 1
ELSE 0 END) 'Age Difference'
FROM
Boys_details A , Girls_details B
WHERE
a.Cast = 'brahmin'
AND A.City = 'pune'
AND a.Height >= B.Height
AND a.Salary >= B.salary
输出:
Nilesh Ruchita 1973-01-21 1964-12-01 9 89 -9
Nilesh Smitha 1973-01-21 1976-07-30 9 77 3
Nilesh Richa 1973-01-21 1974-04-21 9 2 1
我不想要输出中的减去值,但仍然是它给出的高度和薪水之类的条件令人满意,但是如果年龄他们失败了。所以一个失败的人不想在o / p中显示它们请帮助我。
答案 0 :(得分:1)
在子查询中怎么样?
SELECT *
FROM
(
SELECT A.First_Name,
b.First_Name,
A.DOB,
B.DOB,
A.Token,
B.Token,
DATEDIFF(YEAR,A.DOB,B.DOB) -
(CASE WHEN DATEADD(YY,DATEDIFF(YEAR,A.DOB,B.DOB),A.DOB)> Getdate()
THEN 1
ELSE 0
END) AS [Age Difference]
from Boys_details A ,
Girls_details B
where a.Cast='brahmin' and
A.City='pune' and
a.Height >= B.Height and
a.Salary>=B.salary
) s
WHERE [Age Difference] >= 0
答案 1 :(得分:0)
只需将case语句添加到where子句:
SELECT
A.First_Name, b.First_Name, A.DOB, B.DOB, A.Token, B.Token,
DATEDIFF(YEAR, A.DOB, B.DOB) -
(CASE
WHEN DATEADD(YY, DATEDIFF(YEAR, A.DOB, B.DOB), A.DOB) > Getdate()
THEN 1
ELSE 0 END) 'Age Difference'
FROM
Boys_details A , Girls_details B
WHERE
a.Cast = 'brahmin'
AND A.City = 'pune'
AND a.Height >= B.Height
AND a.Salary >= B.salary
AND (CASE WHEN DATEADD(YY, DATEDIFF(YEAR, A.DOB, B.DOB), A.DOB) > Getdate()
THEN 1
ELSE 0 END) => 0