我有一个查询,可以在一天内检查数据库中的多个客户条形码扫描。该报告效果很好,但是我想在其中添加另一篇文章。我想在输出中包含一列“扫描次数”。但是,由于我使用的是“SUM”,它不会在 1 行上多次列出。例如,如果发现条形码“1234”被扫描两次,我希望它被扫描的次数(tickets.dtcreated)作为一个输出列在“扫描次数”列中。
这是我当前的输出:
Barcode DtCreatedDate Number of Scans
1234 1/1/2013 2
1235 1/1/2013 2
1563 1/2/2013 3
这是我希望我的输出看起来像的样子(请记住,“扫描时间”应该只显示发生多次扫描的当天的时间(DTcreateddate):
Barcode DtCreatedDate Number of Scans Times Scanned
1234 1/1/2013 2 11:15AM, 12:15PM
1235 1/1/2013 2 9:00AM, 4:00PM
1563 1/2/2013 3 8:05AM, 8:08AM, 5:50PM
我当前的查询如下
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 @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