0

I have a query that searches for customers that have visted multiple times per day. If I run this query for 1 specific day it works. What I'd like to do is be able to query a range of dates. For example I want to search 12/1/12 - 1/31/13 and have it check each day for multiple visits, and display the date that customer visited multiple times. Below is my query that is not working:

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)
3
  • 1
    How this is not working? Any errors? not the desired results?? Feb 1, 2013 at 18:09
  • Is Tickets.dtCreated date only, or does it include time as well as date?
    – user359040
    Feb 1, 2013 at 18:10
  • Time as well. It's not showing any results. If I run the query 1 date at a time i get the results i'm looking for.
    – Shmewnix
    Feb 1, 2013 at 18:11

2 Answers 2

1

Try something like this:

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 )

Assuming you have time information your dtCreated field, the cast/floor/cast would drop it, then you can group on your date and expand your date range.

3
  • I would just add that the CONVERTs are doing nothing here and can be removed.
    – paul
    Feb 1, 2013 at 18:15
  • 1
    @Kevin: Note that a Tickets.dtCreated could conceivably have a value of 2013-01-31 23:59:59.9 .
    – user359040
    Feb 1, 2013 at 18:24
  • True, a better option would be to either drop the times there as well, or use >= with the start date and < with the date after.
    – Kevin Dahl
    Feb 1, 2013 at 19:50
0

Try:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.