如何计算客户在搜索过程中每天访问的次数

时间:2013-02-01 18:07:20

标签: sql sql-server sql-server-2005

我有一个查询,搜索每天多次访问过的客户。如果我在1个特定日期运行此查询,则可以正常运行。我想做的是能够查询一系列日期。例如,我想搜索12/1/12 - 1/31/13并让它每天检查多次访问,并显示客户多次访问的日期。以下是我的查询无效:

SELECT        Customers.sBarcode, COUNT(Customers.sBarcode) AS [Number of Scans], Tickets.dtCreated
FROM            Tickets INNER JOIN
                         Customers ON Tickets.lCustomerID = Customers.lCustomerID
WHERE        (Tickets.dtCreated BETWEEN CONVERT(DATETIME, '2012-12-11 00:00:00', 102) AND CONVERT(DATETIME, '2012-12-12 00:00:00', 102)) AND (Tickets.dblTotal <= 0)
GROUP BY Customers.sBarcode, Tickets.dtCreated
HAVING        (COUNT(*) > 1)

2 个答案:

答案 0 :(得分:1)

尝试这样的事情:

SELECT  Customers.sBarcode ,
        CAST(FLOOR(CAST(Tickets.dtCreated AS FLOAT)) AS DATETIME) AS dtCreatedDate ,
        COUNT(Customers.sBarcode) AS [Number of Scans]
FROM    Tickets
        INNER JOIN Customers ON Tickets.lCustomerID = Customers.lCustomerID
WHERE   ( Tickets.dtCreated BETWEEN CONVERT(DATETIME, '2012-12-01 00:00:00', 102)
                            AND     CONVERT(DATETIME, '2013-01-31 23:59:59', 102) )
        AND ( Tickets.dblTotal <= 0 )
GROUP BY Customers.sBarcode ,
        CAST(FLOOR(CAST(Tickets.dtCreated AS FLOAT)) AS DATETIME)
HAVING  ( COUNT(*) > 1 )

假设您的dtCreated字段有时间信息,则演员/地板/演员会丢弃它,然后您可以对您的日期进行分组并扩展您的日期范围。

答案 1 :(得分:0)

尝试:

SELECT      Customers.sBarcode, 
            COUNT(Customers.sBarcode) AS [Number of Scans], 
            convert(char(10),Tickets.dtCreated,102)
FROM        Tickets 
INNER JOIN  Customers ON Tickets.lCustomerID = Customers.lCustomerID
WHERE       Tickets.dtCreated >=CONVERT(DATETIME, '2012-12-11 00:00:00', 102) AND 
            Tickets.dtCreated < CONVERT(DATETIME, '2012-12-18 00:00:00', 102) AND 
            Tickets.dblTotal <= 0
GROUP BY    Customers.sBarcode, convert(char(10),Tickets.dtCreated,102)
HAVING      COUNT(*) > 1