1

我有一个具有以下值的表:

job name       start_dt         end_dt
gggggg         4/5/2013        5/5/2013
iiiii            6/5/13          7/8/13

我想从of中减去start_dtof 。ggggggend_dtiiiii

我正在使用这个查询:

select a.job_nm,(b.end_dt-a.start_dt) as difference from JOB a,b
where a.job_nm='ggggg' and b.job_nm='iiiii'.

这没有给我任何输出。

4

2 回答 2

1

如果这是 SQL Server,您可以使用DATEDIFF()

select a.job_nm,
datediff(day, a.start_dt, b.end_dt) as [difference]
from JOB a,b
where a.job_nm='ggggg'
and b.job_nm='iiiii'
于 2013-08-26T10:48:06.137 回答
0

一种方法是条件聚合:

select (max(case when j.job_nm = 'iiiii' then b.end_dt end) - 
        max(case when j.job_nm = 'ggggg' then a.start_dt end)
       ) as difference
from Job j
where j.job_nm in ('ggggg', 'iiiii');

您可以使用该datediff()功能,但datetimes我发现减法更容易。

于 2013-08-26T12:08:19.287 回答