SQL - 使用 datediff 作为查询正在运行?

发布于 2024-08-31 10:14:43 字数 1072 浏览 4 评论 0 原文

这是我的第一篇文章,我对 MSSQL 真的很生疏,所以要温柔:-)

我有一个表,我试图在其中使用 datediff。我认为如果我先发布查询和结果,这将是最简单的。

select mh.agetime, mh.whatid

from mailhistory mh
inner join mail m
 on mh.mailid=m.myid
where (mh.whatid=17 or mh.whatid=11 or mh.whatid=0) and maincontactid=287816 and mailid=276086

order by agetime

实际上,maincontactid 和 mailid 目前只是在那里限制我进行查询时的结果。

结果如下...

AGETIME                    WHATID
1899-12-30 00:00:00.000 0
1899-12-30 00:48:10.000 11
1899-12-31 02:16:49.000 17
1899-12-31 06:29:08.000 11
1900-01-18 15:31:40.000 17
1900-02-11 14:56:59.000 11

我试图在查询运行时创建第三列,这将使第三列显示日期(以天为单位)的差异...WHATID 为 11 和 17 的项目之间。 。所以我正在寻找这样的结果:

AGETIME                    WHATID    DIFFERENCE
1899-12-30 00:00:00.000 0         NULL
1899-12-30 00:48:10.000 11        0
1899-12-31 02:16:49.000 17        1
1899-12-31 06:29:08.000 11        0
1900-01-18 15:31:40.000 17        18
1900-02-11 14:56:59.000 11        22

类似的东西... 那么,有没有办法将我的查询转换为这样的运行 datediff ?

非常感谢!

克里斯

Its my first post and I'm really rusty on MSSQL so be gentle :-)

I have a table in which I am trying to use datediff. I think it will be easiest if I post the query and results first

select mh.agetime, mh.whatid

from mailhistory mh
inner join mail m
 on mh.mailid=m.myid
where (mh.whatid=17 or mh.whatid=11 or mh.whatid=0) and maincontactid=287816 and mailid=276086

order by agetime

Really, the maincontactid and mailid are currently just in there to limit the results while i make the query.

The results are as follows...

AGETIME                    WHATID
1899-12-30 00:00:00.000 0
1899-12-30 00:48:10.000 11
1899-12-31 02:16:49.000 17
1899-12-31 06:29:08.000 11
1900-01-18 15:31:40.000 17
1900-02-11 14:56:59.000 11

I am trying to make a third column as the query runs that will make a third column showing the difference in the dates (in days)... between items with a WHATID of 11 and 17... so I'm after results like this:

AGETIME                    WHATID    DIFFERENCE
1899-12-30 00:00:00.000 0         NULL
1899-12-30 00:48:10.000 11        0
1899-12-31 02:16:49.000 17        1
1899-12-31 06:29:08.000 11        0
1900-01-18 15:31:40.000 17        18
1900-02-11 14:56:59.000 11        22

Something like that... So, is there a way to convert my query to do the running datediff like that?

Many thanks in advance!

Chris

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

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

发布评论

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

评论(1

终止放荡 2024-09-07 10:14:43

如果使用 SQL Server 2005 或更高版本,您可以创建 CTE 并将 row_number 分配给当前结果集。然后,您可以使用 Current.Row_Num = Previous.Row_Num -1 在 CTE 上进行左自连接,然后可以获得日期差。方法与此链接中所示类似:

http://www.kodyaz.com/articles/sql-select-previous-and-next-rows-with-current-row-in-tsql.aspx

对于 SQL Server 2000

假设结果集中没有分组:

我可能会创建一个临时表变量,而不是带有身份字段的 CTE(它将充当行号),然后将应用与上述 2005 年相同的逻辑。只有重大更改才会我们将使用临时表变量来代替 CTE,并且我们将使用临时表中的标识来代替行号。

If using SQL Server 2005 or above, you can make a CTE and assign row_number to your current resultset. Then you can have a left self join on CTE with Current.Row_Num = Previous.Row_Num -1 and then can get the date difference. Approach will be similar as show in this link:

http://www.kodyaz.com/articles/sql-select-previous-and-next-rows-with-current-row-in-tsql.aspx

For SQL Server 2000

Assuming there is no grouping in result set:

I would probably create a temp table variable instead of CTE with an Identity Filed( Which will act as a rownumber) and then will apply the same logic as described above for 2005. Only major change will be instead of CTE we will use Temp Table variable and instead of rownumber we will use identity in temp table.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文