2013-02-01 66 views
0

我有一個查詢,可以搜索每天有多次訪問的客戶。如果我運行這個查詢1特定的一天它的作品。我想要做的是能夠查詢一系列日期。例如,我想搜索2012年12月1日 - 2013年1月31日,並每天檢查多次訪問,並顯示客戶多次訪問的日期。下面是我的查詢不工作:如何統計客戶在搜索中每天訪問的次數

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) 
+1

這怎麼不工作?任何錯誤?沒有預期的結果? –

+0

只有'Tickets.dtCreated'日期,還是包含時間和日期? –

+0

時間也是如此。它沒有顯示任何結果。如果我一次運行查詢1日期,我會得到我期待的結果。 – Shmewnix

回答

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場,投/樓/施放會放棄它,那麼你就可以在你的日期上分組,並擴大你的日期範圍。

+0

我只是補充說'CONVERT'在這裏什麼都不做,可以刪除。 – paul

+1

@Kevin:請注意,Tickets.dtCreated可能具有2013-01-31 23:59:59.9的值。 –

+0

的確,更好的選擇是將時間放在那裏,或者在開始日期使用> =,使用日期後使用<。 –

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 
相關問題