0

I have a customer database and i'm looking to run a query to find any barcode that was used more than once in a day and output just the barcode... So for example if barcode #1 was scanned 3 times on a specific date it will show 3 transactions in the database all with the barcode #1... Here is a basic query I have now searching by date...

Select *
FROM            Customers
WHERE        (dtCreated BETWEEN CONVERT(DATETIME, '2012-12-10 00:00:00', 102) AND CONVERT(DATETIME, '2012-12-11 00:00:00', 102)) AND (sBarcode = '1')

Instead of searching for barcode 1, i'd like to know ANY barcodes (sBarcode) that was scanned more than once on 12/10/12.

0

4 Answers 4

1

select barcode, count(barcode) as cnt from table_name where date = desired_date group by barcode HAVING (COUNT(*) > 1)

Sign up to request clarification or add additional context in comments.

2 Comments

This is close, however it says invalid column name for 'cnt'
HAVING (COUNT(*) > 1) and it's correct. If you edit your post I can give you credit for the answer.
1

Just remove the last AND statement?

EDIT: ok. You have to group by barcode, agregating on each day. That’s all. Something like:

SELECT *
WHERE (dtCreated BETWEEN CONVERT(DATETIME, '2012-12-10 00:00:00', 102) AND CONVERT(DATETIME, '2012-12-11 00:00:00', 102))
GROUP BY dtID, dtCreated
HAVING COUNT(*) > 1

2 Comments

No, that will return ALL customers created on the 10th. I'm looking for duplicate barcode creations.
I stole the HAVING (COUNT(*) > 1) from yours and used TwTw and Rohit query. Thanks!
1
Select Count(barcode) counter
FROM  Customers
WHERE  (dtCreated BETWEEN CONVERT(DATETIME, '2012-12-10 00:00:00', 102) AND CONVERT(DATETIME, '2012-12-11 00:00:00', 102)) AND (sBarcode = '1')
group by barcode
having (COUNT(barcode) >= 1) 

Comments

0

I would do this:

select sbarcode
from customers
where cast(dtCreated as date) = '2012-12-10'
group by sbarcode
having count(*) > 1

If you wanted the count, then you would include , count(*) on the select line.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.