当日期位于同一个表的不同行中时,如何查找日期差异?

发布于 2024-09-18 18:47:29 字数 859 浏览 4 评论 0原文

我有一个表::

ItemID  VersionNo  CreatedDate
-------------------------------
1       3          7/9/2010
1       2          7/3/2010
1       1          5/3/2010
1       0          3/3/2010
2       0          4/4/2010
3       1          4/5/2010
3       0          3/4/2010

...其中版本 0 表示 .. 它是新生产的项目。这里我需要找到时间(两个版本之间的时间差距)并添加一列作为处理时间。 就像::

ItemID  VersionNo  CreatedDate  ProcessTime
-------------------------------------------
1       3          7/9/2010     6Days or 6*24Hrs
1       2          7/3/2010     60Days 
1       1          5/3/2010     2Days
1       0          3/3/2010     ''
2       0          4/4/2010     '' 
3       1          4/5/2010     31Days
3       0          3/4/2010     ''

版本号不是固定的..意味着随着时间的推移,它可能会增加...如何在 MS Access 或 SQL-Server 中实现所需的结果。

预先感谢您的所有真诚努力。 谢谢

I have a table::

ItemID  VersionNo  CreatedDate
-------------------------------
1       3          7/9/2010
1       2          7/3/2010
1       1          5/3/2010
1       0          3/3/2010
2       0          4/4/2010
3       1          4/5/2010
3       0          3/4/2010

...where Version 0 means .. its a newly produced item. Here I need to find time,(time gap between two versions) and add a column as process time.
like::

ItemID  VersionNo  CreatedDate  ProcessTime
-------------------------------------------
1       3          7/9/2010     6Days or 6*24Hrs
1       2          7/3/2010     60Days 
1       1          5/3/2010     2Days
1       0          3/3/2010     ''
2       0          4/4/2010     '' 
3       1          4/5/2010     31Days
3       0          3/4/2010     ''

VersionNo's are not Fixed..means with time, it could increase... How to acheive the desire result in MS Access or in SQL-Server.

Thanks in advance for all your sincere efforts.
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

趴在窗边数星星i 2024-09-25 18:47:29

怎么样(访问):

SELECT t.ItemID, 
       t.VersionNo, 
       t.CreatedDate, (
          SELECT Top 1 
          CreatedDate 
          FROM Versions v 
          WHERE v.ItemID=t.ItemID 
          And v.VersionNo<t.VersionNo 
          ORDER BY VersionNo DESC) AS LastDate, 
       DateDiff("h",[LastDate],[CreatedDate]) AS DiffHrs,
       DateDiff("d",[LastDate],[CreatedDate]) AS DiffDays
FROM Versions t

How about (Access):

SELECT t.ItemID, 
       t.VersionNo, 
       t.CreatedDate, (
          SELECT Top 1 
          CreatedDate 
          FROM Versions v 
          WHERE v.ItemID=t.ItemID 
          And v.VersionNo<t.VersionNo 
          ORDER BY VersionNo DESC) AS LastDate, 
       DateDiff("h",[LastDate],[CreatedDate]) AS DiffHrs,
       DateDiff("d",[LastDate],[CreatedDate]) AS DiffDays
FROM Versions t
简单气质女生网名 2024-09-25 18:47:29

将表与其自身连接起来,如下所示 (SQL Server):

-- create the table and your data
create table #x (ItemID int, VersionNo int, CreatedDate datetime)
go
insert into #x
select 1,       3          ,'7/9/2010'
union all select  1       ,2          ,'7/3/2010'
union all select  1       ,1          ,'5/3/2010'
union all select  1       ,0          ,'3/3/2010'
union all select  2       ,0          ,'4/4/2010'
union all select  3       ,1          ,'4/5/2010'
union all select  3       ,0          ,'3/4/2010'
go

-- The query
select v2.ItemID, v2.VersionNo, datediff(dd, v1.CreatedDate, v2.CreatedDate)
from #x v1, #x v2
where v1.ItemID = v2.ItemID and v1.VersionNo + 1 = v2.VersionNo

Join the table with itself, like this (SQL Server):

-- create the table and your data
create table #x (ItemID int, VersionNo int, CreatedDate datetime)
go
insert into #x
select 1,       3          ,'7/9/2010'
union all select  1       ,2          ,'7/3/2010'
union all select  1       ,1          ,'5/3/2010'
union all select  1       ,0          ,'3/3/2010'
union all select  2       ,0          ,'4/4/2010'
union all select  3       ,1          ,'4/5/2010'
union all select  3       ,0          ,'3/4/2010'
go

-- The query
select v2.ItemID, v2.VersionNo, datediff(dd, v1.CreatedDate, v2.CreatedDate)
from #x v1, #x v2
where v1.ItemID = v2.ItemID and v1.VersionNo + 1 = v2.VersionNo
2024-09-25 18:47:29

这是在 Access SQL 中,使用 3 个查询,每个步骤一个。

查询1,在itemID上进行自连接,其中版本号较小:

    SELECT t1.itemID, t1.versionNo, t1.created, t2.versionNo AS t2Version
FROM Table1 AS t1 INNER JOIN Table1 AS t2 ON t1.itemID = t2.itemID
WHERE (t2.versionNo)<[t1].[versionNo];

查询2,限制较小版本号的最大值:

    SELECT q1.itemID, q1.versionNo, q1.created, Max(q1.t2Version) AS MaxOft2Version
FROM Query1 AS q1
GROUP BY q1.itemID, q1.versionNo, q1.created;

查询3,现在执行datediff:

SELECT q2.itemID, q2.versionNo, q2.created, q2.MaxOft2Version, t1.created, 
    DateDiff("d",[t1].[created],[Q2].[created]) AS daysdiff
FROM Query2 AS q2 INNER JOIN Table1 AS t1 
    ON (q2.MaxOft2Version = t1.versionNo) 
    AND (q2.itemID = t1.itemID);

Here it is in Access SQL, using 3 queries, one for each step.

Query1, self-join on itemID where versionNo is smaller:

    SELECT t1.itemID, t1.versionNo, t1.created, t2.versionNo AS t2Version
FROM Table1 AS t1 INNER JOIN Table1 AS t2 ON t1.itemID = t2.itemID
WHERE (t2.versionNo)<[t1].[versionNo];

Query2, limit to max of smaller versionNos:

    SELECT q1.itemID, q1.versionNo, q1.created, Max(q1.t2Version) AS MaxOft2Version
FROM Query1 AS q1
GROUP BY q1.itemID, q1.versionNo, q1.created;

Query3, now do datediff:

SELECT q2.itemID, q2.versionNo, q2.created, q2.MaxOft2Version, t1.created, 
    DateDiff("d",[t1].[created],[Q2].[created]) AS daysdiff
FROM Query2 AS q2 INNER JOIN Table1 AS t1 
    ON (q2.MaxOft2Version = t1.versionNo) 
    AND (q2.itemID = t1.itemID);
轻许诺言 2024-09-25 18:47:29

SQL Server 2005,用于处理 VersionNo 存在间隙的情况。

-- Declare a query that extends your table with a new column 
-- that is the sequentially numbered representation of VersionNo.
-- This could be a view, but I used a CTE.  I am going to use this
-- query twice below.
WITH Sequential AS (select *, 
    RANK() over (partition by ItemId order by VersionNo) as SequentialVersionNo
from #T as x
)
select 
    v.ItemID, v.VersionNo, v.SequentialVersionNo, v.CreatedDate, 
    DATEDIFF(day, vPrior.CreatedDate, v.CreatedDate) as ProcessTime
from Sequential as v
left outer join Sequential as vPrior 
on v.ItemID=vPrior.ItemID 
and v.SequentialVersionNo = vPrior.SequentialVersionNo+1;

SQL Server 2005, to handle the case where there are gaps in VersionNo.

-- Declare a query that extends your table with a new column 
-- that is the sequentially numbered representation of VersionNo.
-- This could be a view, but I used a CTE.  I am going to use this
-- query twice below.
WITH Sequential AS (select *, 
    RANK() over (partition by ItemId order by VersionNo) as SequentialVersionNo
from #T as x
)
select 
    v.ItemID, v.VersionNo, v.SequentialVersionNo, v.CreatedDate, 
    DATEDIFF(day, vPrior.CreatedDate, v.CreatedDate) as ProcessTime
from Sequential as v
left outer join Sequential as vPrior 
on v.ItemID=vPrior.ItemID 
and v.SequentialVersionNo = vPrior.SequentialVersionNo+1;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文