这就是我目前所拥有的:
我的目标是搜索次数如下......
我的实际表有一个DatetimeSelected DataTime字段,我只需要计算日期。我使用' 转换'现在想按日期计算每个搜索次数 到目前为止,这是我的代码......
SELECT
CONVERT(VARCHAR(10), DateTimeSelected, 103) AS [Book Date]
,Bathrooms
,Count (CONVERT(VARCHAR(10), DateTimeSelected, 103)) AS 'No of Searches'
FROM dbo.Stats_Bathrooms WITH (NOLOCK)
WHERE (DateTimeSelected BETWEEN GETDATE() - 7 AND GETDATE())
GROUP BY ID, DateTimeSelected, Bathrooms, SessionID
任何帮助非常非常感谢!
答案 0 :(得分:6)
您应该只GROUP BY CONVERT(VARCHAR(10), DateTimeSelected, 103), Bathrooms
:
SELECT
CONVERT(VARCHAR(10), DateTimeSelected, 103) AS [Book Date]
,Bathrooms
,Count (CONVERT(VARCHAR(10), DateTimeSelected, 103)) AS 'No of Searches'
FROM dbo.Stats_Bathrooms WITH (NOLOCK)
WHERE (DateTimeSelected BETWEEN GETDATE() - 7 AND GETDATE())
GROUP BY CONVERT(VARCHAR(10), DateTimeSelected, 103),
Bathrooms;
这会给你:
| BOOK DATE | BATHROOMS | NO OF SEARCHES |
-------------------------------------------
| 26/07/2013 | 1 | 3 |
| 26/07/2013 | 2 | 4 |
| 26/07/2013 | 3 | 1 |
| 27/07/2013 | 1 | 2 |