Where 子句不 select o 计入 SQL 服务器 CE

Where clause not select o count in in SQL Server CE

我在 select 使用 SQL CE table 的所有计数值列表中遇到问题。

SELECT  Time AS Hour,Cast (COUNT(ReceivedTime)*1.0/(10)as decimal(16,2)) AS HourlyTotal 
    FROM tblTime 
    LEFT OUTER JOIN tblMessageReceived ON Time = ReceivedTime  
    WHERE ReceivedDateTime >= '2016-01-01'  AND ReceivedDateTime <= '2016-07-07' 
    GROUP BY Time

但它 select 只有非零计数。结果table如下。

在这里,我想 select 所有小时时间的计数值为 0。

您的 WHERE 子句有效地将您的 LEFT JOIN 变成了 INNER JOIN。解决方案是将它们放在 ON 子句中:

SELECT  
    t.Time AS Hour,
    CAST(COUNT(t.ReceivedTime)*1.0/(10) AS DECIMAL(16, 2)) AS HourlyTotal 
FROM tblTime t
LEFT OUTER JOIN tblMessageReceived tmr
    ON t.Time = tmr.ReceivedTime  
    AND tmr.ReceivedDateTime >= '2016-01-01'
    AND tmr.ReceivedDateTime <= '2016-07-07' 
GROUP BY t.Time

注:

  • 使用有意义的别名以提高可读性。