Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9248679
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:59:50+00:00 2026-06-18T09:59:50+00:00

I have a query that checks a database to see if a customer has

  • 0

I have a query that checks a database to see if a customer has visited multiple times a day. If they have it counts the number of visits, and then tells me what times they visited. The problem is it throws “Tickets.lcustomerid” into the group by clause, causing me to miss 5 records (Customers without barcodes). How can I change the below query to remove “tickets.lcustomerid” from the group by clause… If I remove it I get an error telling me “Tickets.lCustomerID” is not a valid select because it’s not part of an aggregate or groupby clause.

The Query that works:

SELECT        Customers.sBarcode, CAST(FLOOR(CAST(Tickets.dtCreated AS FLOAT)) AS DATETIME) AS dtCreatedDate, COUNT(Customers.sBarcode) AS [Number of Scans], 
                         MAX(Customers.sLastName) AS LastName
FROM            Tickets INNER JOIN
                         Customers ON Tickets.lCustomerID = Customers.lCustomerID
WHERE        (Tickets.dtCreated BETWEEN @startdate AND @enddate) AND (Tickets.dblTotal <= 0)
GROUP BY Customers.sBarcode, CAST(FLOOR(CAST(Tickets.dtCreated AS FLOAT)) AS DATETIME)
HAVING        (COUNT(*) > 1)
ORDER BY dtCreatedDate

The Output is:

sBarcode   dtcreated Date      Number of Scans  slastname    
1234     1/4/2013 12:00:00 AM         2          Jimbo         
         1/5/2013 12:00:00 AM         3          Jimbo2       
1578     1/6/2013 12:00:00 AM         3          Jimbo3        

My current Query with the subquery

SELECT customers.sbarcode, 
       Max(customers.slastname)                                  AS LastName, 
       Cast(Floor(Cast(tickets.dtcreated AS FLOAT)) AS DATETIME) AS 
       dtCreatedDate, 
       Count(customers.sbarcode)                                 AS 
       [Number of Scans], 
       Stuff ((SELECT ', ' 
                      + RIGHT(CONVERT(VARCHAR, dtcreated, 100), 7) AS [text()] 
               FROM   tickets AS sub 
               WHERE  ( lcustomerid = tickets.lcustomerid ) 
                      AND ( dtcreated BETWEEN Cast(Floor(Cast(tickets.dtcreated 
                                                              AS 
                                                              FLOAT)) AS 
                                                   DATETIME 
                                              ) 
                                              AND 
Cast(Floor(Cast(tickets.dtcreated 
AS FLOAT 
)) AS 
DATETIME 
) 
+ '23:59:59' ) 
AND ( dbltotal <= '0' ) 
FOR xml path('')), 1, 1, '')                      AS [Times Scanned] 
FROM   tickets 
       INNER JOIN customers 
               ON tickets.lcustomerid = customers.lcustomerid 
WHERE  ( tickets.dtcreated BETWEEN @startdate AND @enddate ) 
       AND ( tickets.dbltotal <= 0 ) 
GROUP  BY customers.sbarcode, 
          Cast(Floor(Cast(tickets.dtcreated AS FLOAT)) AS DATETIME), 
          tickets.lcustomerid 
HAVING ( Count(*) > 1 ) 
ORDER  BY dtcreateddate 

The Current output (notice the record without a barcode is missing) is:

sBarcode   dtcreated Date      Number of Scans  slastname    Times Scanned
1234     1/4/2013 12:00:00 AM         2          Jimbo         12:00PM, 1:00PM
1578     1/6/2013 12:00:00 AM         3          Jimbo3        03:05PM, 1:34PM
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T09:59:52+00:00Added an answer on June 18, 2026 at 9:59 am

    UPDATE: Based on our “chat” it seems that customerid is not the unique field but barcode is, even though customer id is the primary key.

    Therefore, in order to not GROUP BY customer id in the subquery you need to join to a second customers table in there in order to actually join on barcode.

    Try this:

    SELECT customers.sbarcode, 
           Max(customers.slastname)                                  AS LastName, 
           Cast(Floor(Cast(tickets.dtcreated AS FLOAT)) AS DATETIME) AS 
           dtCreatedDate, 
           Count(customers.sbarcode)                                 AS 
           [Number of Scans], 
           Stuff ((SELECT ', ' 
                          + RIGHT(CONVERT(VARCHAR, dtcreated, 100), 7) AS [text()] 
                   FROM   tickets AS subticket
                   inner join
                   customers as subcustomers
                   on
                   subcustomers.lcustomerid = subticket.lcustomerid
                   WHERE  ( subcustomers.sbarcode = customers.sbarcode ) 
                          AND ( subticket.dtcreated BETWEEN Cast(Floor(Cast(tickets.dtcreated 
                                                                  AS 
                                                                  FLOAT)) AS 
                                                       DATETIME 
                                                  ) 
                                                  AND 
    Cast(Floor(Cast(tickets.dtcreated 
    AS FLOAT 
    )) AS 
    DATETIME 
    ) 
    + '23:59:59' ) 
    AND ( dbltotal <= '0' ) 
    FOR xml path('')), 1, 1, '')                      AS [Times Scanned] 
    FROM   tickets 
           INNER JOIN customers 
                   ON tickets.lcustomerid = customers.lcustomerid 
    WHERE  ( tickets.dtcreated BETWEEN @startdate AND @enddate ) 
           AND ( tickets.dbltotal <= 0 ) 
    GROUP  BY customers.sbarcode, 
              Cast(Floor(Cast(tickets.dtcreated AS FLOAT)) AS DATETIME)
    HAVING ( Count(*) > 1 ) 
    ORDER  BY dtcreateddate 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a database that has multiple tables to track customers and our interactions
My main issue is the number of times I query the database (see below).
Quick question... I have a query that checks for duplicates that looks like this:
I have a function that first checks to see a row exists and if
I have this php code. As you can see i query a mysql database
I have a PHP code, that will run a Select query to check if
I am using in C# MYsql .I have query that works if I run
I have a query that successfully grabs the unique products from my products table
I have a query that basically combines tables of actions and selects from them
I have have this query that i want to execute. SELECT warehouse.expiry_date, pharmacy.expiry_date, drugs.active_substance,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.