0

我有一个类似于以下内容的 SQL 查询:

SELECT CONVERT(varchar, DATEPART(month, titlStreaming)) + '/' + CONVERT(varchar, DATEPART(day, titlStreaming)) + '/' + CONVERT(varchar, DATEPART(year, titlStreaming)) AS [Day], 
COUNT(titlTitleID) AS Total 
FROM Title
GROUP BY DATEPART(year, titlStreaming), DATEPART(month, titlStreaming), DATEPART(day, titlStreaming)
ORDER BY DATEPART(year, titlStreaming) DESC, DATEPART(month, titlStreaming) DESC, DATEPART(day, titlStreaming) DESC

这通常会返回一个表格,如:

Day       |   Total
--------------------
4/23/2013 |       2
         ...
NULL      |   14234

我想做的是过滤掉NULL返回值的行。因为Day显然是一个计算列,所以我不能简单地做一个WHERE Day IS NOT NULL. 我承认我对 SQL 的了解很少,所以任何帮助或建议都将不胜感激。

4

2 回答 2

1

您可以使用子查询:

select *
from (SELECT CONVERT(varchar, DATEPART(month, titlStreaming)) + '/' + CONVERT(varchar, DATEPART(day, titlStreaming)) + '/' + CONVERT(varchar, DATEPART(year, titlStreaming)) AS [Day], 
             COUNT(titlTitleID) AS Total 
     FROM Title
     GROUP BY DATEPART(year, titlStreaming), DATEPART(month, titlStreaming), DATEPART(day, titlStreaming)
    ) t
where [day] is not null
ORDER BY [day] DESC
于 2013-04-24T15:29:45.813 回答
1

就所有日期部分都依赖于 titleStreaming 而言,titleStreaming 的 where 条件应该足够了。

....
From Title
where titlStreaming is not null
Group by ....
于 2013-04-24T15:32:49.960 回答