SQL - 如何连接相似(不精确)的列

发布于 2024-09-01 08:37:04 字数 365 浏览 2 评论 0原文

我有两个表几乎在同一时间更新 - 我需要加入日期时间列。

我试过这个:

SELECT *
FROM A, B
WHERE ABS(DATEDIFF(second, A.Date_Time, B.Date_Time)) = (
    SELECT MIN(ABS(DATEDIFF(second, A.Date_Time, B2.Date_Time)))
    FROM B AS B2
)

但它告诉我:

在包含外部引用的聚合表达式中指定多个列。如果正在聚合的表达式包含外部引用,则该外部引用必须是表达式中引用的唯一列。

我如何加入这些表?

I have two tables which get updated at almost the exact same time - I need to join on the datetime column.

I've tried this:

SELECT *
FROM A, B
WHERE ABS(DATEDIFF(second, A.Date_Time, B.Date_Time)) = (
    SELECT MIN(ABS(DATEDIFF(second, A.Date_Time, B2.Date_Time)))
    FROM B AS B2
)

But it tells me:

Multiple columns are specified in an aggregated expression containing an outer reference. If an expression being aggregated contains an outer reference, then that outer reference must be the only column referenced in the expression.

How can I join these tables?

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

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

发布评论

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

评论(3

苍白女子 2024-09-08 08:37:04

怎么样(假设 SQL 2005+):

With RankedItems As
    (
    Select A.DateTime As ADateTime, B.DateTime As BDateTime
        , ROW_NUMBER() OVER ( PARTITION BY A.DateTime ORDER BY ABS( DateDiff(s, A.DateTime, B.DateTime) ) ) As ItemRank
    From A
        Cross Join B
    )
Select 
From RankedItems
Where ItemRank = 1

在我的解决方案中,我使用公共表表达式或简称 CTE。在 CTE 中,我使用排名函数 (ROW_NUMBER),它计算 CTE 表达式中的每一行。 ROW_NUMBER 函数将通过按绝对值排序的 PARTITION BY A.DateTime 子句返回为表 A 中的每个 DateTime 值提供一个连续整数A.DateTime 值与 B.DateTime 值的“接近度”。因此,我使用 Abs(DateDiff(s,A.DateTime, B.DateTime) 对“紧密度”进行排名,并为每个值选择最高排名(排名 = 1,也称为“最接近”值) A.DateTime 值是否存在平局并不重要,因为 ROW_NUMBER() 函数将为每个 A.DateTime< 返回一个唯一的数字列表。 /代码> 值。

How about something like (assuming SQL 2005+) :

With RankedItems As
    (
    Select A.DateTime As ADateTime, B.DateTime As BDateTime
        , ROW_NUMBER() OVER ( PARTITION BY A.DateTime ORDER BY ABS( DateDiff(s, A.DateTime, B.DateTime) ) ) As ItemRank
    From A
        Cross Join B
    )
Select 
From RankedItems
Where ItemRank = 1

In my solution, I'm using a common-table expression or CTE for short. In the CTE, I'm using a ranking function (ROW_NUMBER) which calculates for each row in the CTE expression. The ROW_NUMBER function will return provide a sequential integer for each DateTime value in table A via the PARTITION BY A.DateTime clause ordered by the absolute value of the "closeness" A.DateTime value to the B.DateTime value. Thus, I'm ranking the "closeness" using Abs(DateDiff(s,A.DateTime, B.DateTime) and choosing the highest rank (rank = 1, aka "closest" value) for each A.DateTime value. It will not matter if there is a tie as the ROW_NUMBER() function will return a unique list of numbers for each A.DateTime value.

颜漓半夏 2024-09-08 08:37:04

这是你想要的吗?它将检查 1 秒差异

    select * from
A, b --use a join next time
where   A.Date_Time 
between DATEADD(s,-1,B.Date_Time) and DATEADD(s,1,B.Date_Time)

Is this what you want? It will check for 1 second difference

    select * from
A, b --use a join next time
where   A.Date_Time 
between DATEADD(s,-1,B.Date_Time) and DATEADD(s,1,B.Date_Time)
绾颜 2024-09-08 08:37:04

最好首先创建一个视图,其中包含保存计算出的 datediff 的列,然后从中构造一个查询。

It might be better to first create a view that contains a column holding your computed datediff, and then construct a query from that.

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