如何查询列中的多个条目

时间:2012-12-11 13:21:45

标签: sql sql-server-2008

我有一个客户数据库,我希望运行一个查询来查找一天中多次使用的条形码并仅输出条形码...例如,如果条形码#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,而是想知道在12/10/12多次扫描的任何条形码(sBarcode)。

4 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

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

答案 2 :(得分: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) 

答案 3 :(得分:0)

我会这样做:

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

如果您想要计算,那么您可以在, count(*)行添加select