DISTINCT 与 CAST 和 GROUP BY

DISTINCT with CAST and GROUP BY

我正在尝试获取 FeedbackDT 列的 DISTINCT,但我似乎不明白为什么它不起作用..

SQL 查询:

SELECT COUNT(FeedbackID) as FeedbackID,
       (SELECT DISTINCT CAST(feedbackDateTime AS DATE)) as FeedbackDT
FROM Feedback
WHERE feedBackDateTime <= GETDATE()
GROUP BY (feedbackDateTime)

执行查询的结果

我上下搜索都没有结果..

感谢任何帮助,谢谢..

因为您当前的查询没有多大意义。当您使用 GROUP BY 时,您将获得分组依据的列的不同值(或者列的组合,如果您使用多个列)。不需要您正在使用的 SELECT DISTINCT 子查询。

在我看来你需要使用一个简单的 GROUP BY:

SELECT CAST(feedbackDateTime AS DATE) FeedbackDT,
       COUNT(FeedbackID) as FeedbackID
FROM Feedback
WHERE feedBackDateTime <= GETDATE()
GROUP BY CAST(feedbackDateTime AS DATE)
;