带有 TIMESTAMP 的 SQL Server 中的表连接问题
Posted
技术标签:
【中文标题】带有 TIMESTAMP 的 SQL Server 中的表连接问题【英文标题】:Table joining issue in SQL Server with TIMESTAMP 【发布时间】:2019-05-27 06:58:40 【问题描述】:我想从 SQL Server 中的三个表中获取当天的详细信息。我无法使用TIMESTAMP
加入表格,每个表格中的TIMESTAMP
都不同。连接这些表没有共同的因素。有什么办法可以加入这些表。而且我无法修改表格,这些表格来自第三方服务。
查询:
SELECT *
FROM table1
WHERE CAST(TIMESTAMP AS DATE) = CAST(GETDATE() AS DATE)
ORDER BY CAST(TIMESTAMP AS DATETIME2(0))
SELECT *
FROM table2
WHERE CAST(TIMESTAMP AS DATE) = CAST(GETDATE() AS DATE)
ORDER BY CAST(TIMESTAMP AS DATETIME2(0))
SELECT *
FROM table3
WHERE CAST(TIMESTAMP AS DATE) = CAST(GETDATE() AS DATE)
ORDER BY CAST(TIMESTAMP AS DATETIME2(0))
SELECT
CAST(one.TIMESTAMP AS DATETIME2(0)) AS DATE,
CONVERT(DECIMAL(10, 2), one.value) AS v1,
CONVERT(DECIMAL(10, 2), two.VALUE) AS v2,
CONVERT(DECIMAL(10, 2), three.VALUE) AS v3
FROM
table1 one
LEFT JOIN
table2 two ON CAST(one.TIMESTAMP AS DATETIME2(0)) = CAST(two.TIMESTAMP AS DATETIME2(0))
LEFT JOIN
table3 three ON CAST(one.TIMESTAMP AS DATETIME2(0)) = CAST(three.TIMESTAMP AS DATETIME2(0))
WHERE
CAST(one.TIMESTAMP AS DATE) = CAST(GETDATE() AS DATE)
ORDER BY
CAST(one.TIMESTAMP AS DATETIME2(0))
结果:
我没有得到第 2 表和第 3 表的数据。例如:2018-12-30 00:00:15.657
这次是从第三张表的第一行开始。结果中没有显示。我想要所有数据。
希望你能理解。你能帮我解决这个问题吗?
【问题讨论】:
具体是什么问题?首先,它sez“无法加入”,然后“加入”完美运行。请说明预期结果,以及这与加入有何不同。然后,我们可以重现这一点,删除 jpeg 或 png 输入,并将它们作为副本替换为 headers/paste,然后用大括号框住每一个以保持间距。 @donPablo 我刚刚编辑了我的问题。 加入row_number? @P.Salmon which row_number?你是说身份证? 我假设您希望加入每个表上的第一个条目,每个表上的第二个条目等。您不能加入 id,因为它们不一样,但您可以根据每个表生成 row_number id 并加入。 docs.microsoft.com/en-us/sql/t-sql/functions/… 和 google sqlserver row_number 了解更多信息。 【参考方案1】:或者,只需修改 WHERE
WHERE
CAST(one.TIMESTAMP AS DATE) = CAST(GETDATE() AS DATE)
OR CAST(two.TIMESTAMP AS DATE) = CAST(GETDATE() AS DATE)
OR CAST(three.TIMESTAMP AS DATE) = CAST(GETDATE() AS DATE)
【讨论】:
这个步骤我试过了,但是不能正常工作。【参考方案2】:你可能想要full join
:
SELECT COALESCE(CAST(one.TIMESTAMP AS DATETIME2(0)),
CAST(two.TIMESTAMP AS DATETIME2(0)),
CAST(three.TIMESTAMP AS DATETIME2(0))
) as dt,
CONVERT(DECIMAL(10, 2), one.value) AS v1,
CONVERT(DECIMAL(10, 2), two.VALUE) AS v2,
CONVERT(DECIMAL(10, 2), three.VALUE) AS v3
FROM (SELECT *
FROM table1 one
WHERE CAST(one.TIMESTAMP AS DATE) = CAST(GETDATE() AS DATE)
) one FULL JOIN
(SELECT *
FROM table2 two
WHERE CAST(two.TIMESTAMP AS DATE) = CAST(GETDATE() AS DATE)
) two
ON CAST(two.TIMESTAMP AS DATETIME2(0)) = CAST(on.TIMESTAMP AS DATETIME2(0)) FULL JOIN
(SELECT *
FROM table3 three
WHERE CAST(three.TIMESTAMP AS DATE) = CAST(GETDATE() AS DATE)
) three
ON CAST(three.TIMESTAMP AS DATETIME2(0)) = COALESCE(CAST(one.TIMESTAMP AS DATETIME2(0)), CAST(one.TIMESTAMP AS DATETIME2(0)))
ORDER BY dt;
FULL JOIN
s 过滤起来很棘手,这就是它使用子查询的原因。
【讨论】:
以上是关于带有 TIMESTAMP 的 SQL Server 中的表连接问题的主要内容,如果未能解决你的问题,请参考以下文章
JSTL - 使用带有 java.sql.Timestamp 的 formatDate
在 SQL Server 2008 R2 中将 varchar 转换为时间戳