0

我有一个客户数据库,我正在寻找运行查询以查找在一天中使用多次的任何条码并仅输出条码...例如,如果条码 #1 在特定日期被扫描 3 次它将在数据库中显示 3 笔交易,全部带有条形码 #1... 这是我现在按日期搜索的基本查询...

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')

我不想搜索条形码 1,而是想知道在 2012 年 12 月 10 日多次扫描的任何条形码 (sBarcode)。

4

4 回答 4

1

从 table_name 中选择条形码,count(barcode) as cnt 其中 date = desired_date group by barcode HAVING (COUNT(*) > 1)

于 2012-12-11T13:29:29.147 回答
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) 
于 2012-12-11T13:34:19.563 回答
1

只需删除最后一个 AND 语句?

编辑:好的。您必须按条形码分组,每天汇总。就这样。就像是:

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
于 2012-12-11T13:25:17.390 回答
0

我会这样做:

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

如果您想要计数,那么您将包括, count(*)在内select

于 2012-12-11T16:27:04.020 回答