2

这是我目前拥有的:

在此处输入图像描述

我的目标是搜索次数如下......

在此处输入图像描述

我的实际表有一个 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

非常感谢任何帮助!

4

1 回答 1

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 |
于 2013-08-02T08:34:30.983 回答