SQL DISTINCT/GROUP BY 无效

SQL DISTINCT/GROUP BY not working

请多多包涵,我是 SQL 的新手。我写出了以下查询,但 DISTINCT 基本上被我的 WHERE 语句否定了。这是因为 table 中的日期后跟一个时间戳,因此如果订单项的放置时间有任何差异,它会生成一个新行。

让它完全忽略日期之后的所有内容的最佳方法是什么,这样就不会因为时间戳而生成新行?根据我的发现,我认为使用 GROUP BY 可以更好地实现这一点,但接缝总是会产生错误(很可能我不知道将聚合函数放在哪里)。

如有任何帮助,我们将不胜感激。谢谢!

Select DISTINCT
 so.num AS Ref
,  so.shiptoname AS Recipient_Full_Name
, so.shiptoaddress AS Address_1
, so.shiptocity AS City
, stateconst.name AS State
, so.shiptozip AS Zip
, so.billtoname AS Buyer_Name
, qbclass.name AS Class
, soitem.datescheduledfulfillment AS Fulfillment_Date
From SO
JOIN stateconst
ON so.shiptostateid=stateconst.id
JOIN qbclass
ON so.qbclassid=qbclass.id
JOIN soitem
ON so.id=soitem.soid
WHERE DATESCHEDULEDFULFILLMENT LIKE '2016-05-10%'

使用 CAST()datetime 数据类型更改为 date 类型:

Select DISTINCT
 so.num AS Ref
,  so.shiptoname AS Recipient_Full_Name
, so.shiptoaddress AS Address_1
, so.shiptocity AS City
, stateconst.name AS State
, so.shiptozip AS Zip
, so.billtoname AS Buyer_Name
, qbclass.name AS Class
, CAST(soitem.datescheduledfulfillment as date) AS Fulfillment_Date
From SO
JOIN stateconst
ON so.shiptostateid=stateconst.id
JOIN qbclass
ON so.qbclassid=qbclass.id
JOIN soitem
ON so.id=soitem.soid
WHERE CAST(soitem.datescheduledfulfillment as date) = '2016-05-10'

您真的需要查询输出中的日期吗?因为省略它会解决你的 'problem':

Select DISTINCT
 so.num AS Ref
,  so.shiptoname AS Recipient_Full_Name
, so.shiptoaddress AS Address_1
, so.shiptocity AS City
, stateconst.name AS State
, so.shiptozip AS Zip
, so.billtoname AS Buyer_Name
, qbclass.name AS Class

From SO
JOIN stateconst
ON so.shiptostateid=stateconst.id
JOIN qbclass
ON so.qbclassid=qbclass.id
JOIN soitem
ON so.id=soitem.soid
WHERE DATESCHEDULEDFULFILLMENT LIKE '2016-05-10%'