【发布时间】:2016-10-28 09:14:50
【问题描述】:
我的 sql 查询可以正常工作,但是当我找到具有相同小时和分钟的日期时,我无法使查询正常工作。例如:
在“trans_date”列上,我无法使用我的查询,因为使用 max(trans_date) 我没有得到任何结果,不知何故 sql 忽略了秒数。
这是我完整的sql语句:
SELECT till.code,art.description
FROM [TCPOS4].[dbo].[transactions] as tra,
TCPOS4.dbo.articles as art,[TCPOS4].[dbo].[trans_articles] as tro,
[TCPOS4].[dbo].[tills] as till,[TCPOS4].[dbo].[shops] as shop
where tra.till_id=till.id and shop.id=till.shop_id and tro.transaction_id=tra.id and
art.id=tro.article_id and tra.trans_date =(select max(trans_date)
from tcpos4.dbo.transactions as t2 where t2.till_id=tra.till_id and trans_date > '2016-10-26 00:00:0.000' and trans_date< '2016-10-27 00:00:00.000' )
group by till.code,art.description
通过此查询,我从 2016 年 10 月 26 日到 2016 年 10 月 27 日的每个“代码”都获得了最大交易日期,但我没有从代码“5446”中获得任何信息。我应该得到“TABLE CHOCOLECHE-CONGUITOS”,因为它是该范围内的最大 trans_date。
【问题讨论】:
-
是 '2016-10-26 00:00:0.000' 到 '2016-10-27 00:00:00.000' 范围内的 trans_date 吗?你没有得到的那一行是什么日期?
-
我猜问题出在
INNER JOINs 附近。倾向于使用显式连接语法而不是老式连接“通过逗号和谓词在哪里”。 -
该行的日期是图片的第一个,与另一个几乎相同的日期。我认为问题就在那里,因为我一直在试验并且出现错误我有相同的小时和分钟。好吧,我没有错误,查询只是忽略代码'5446'
标签: sql-server datetime group-by max