样本数据:
+===========================================================================+
|NoBoxes | Carrier | ProcessDateTime | Errored | Voided | TrackingNumber |
+===========================================================================+
| 2 | UPS | 5/22/2013 8:14 | 0 | 0 | 1Z1234567891234567|
+---------------------------------------------------------------------------+
| | UPS | | 0 | 1 | 1Z1234567891234567|
+---------------------------------------------------------------------------+
| 5 | UPS | 5/22/2013 8:22 | 1 | 0 | |
+---------------------------------------------------------------------------+
| 7 | UPS | 5/22/2013 8:14 | 0 | 0 | 1Z9876543210987654|
+---------------------------------------------------------------------------+
| 1 | UPS | 5/22/2013 8:22 | 0 | 0 | 1Z1472583691472583|
+---------------------------------------------------------------------------+
| 1 | FedEx | 5/22/2013 8:14 | 0 | 0 | xxxxxxxxxxxxxxxx |
+---------------------------------------------------------------------------+
| 8 | FedEx | 5/22/2013 8:22 | 0 | 0 | yyyyyyyyyyyyyyyy |
+---------------------------------------------------------------------------+
| 3 | USPS | 5/22/2013 8:14 | 0 | 0 | zzzzzzzzzzzzzzzz |
+---------------------------------------------------------------------------+
| 4 | USPS | 5/22/2013 8:22 | 0 | 0 | aaaaaaaaaaaaaaaa |
+---------------------------------------------------------------------------+
| 7 | UPS | 5/22/2013 8:14 | 0 | 0 | 1Z9638527411012396|
+---------------------------------------------------------------------------+
| 9 | UPS | 5/22/2013 8:22 | 0 | 0 | 1Z4561591981655445|
+---------------------------------------------------------------------------+
现在有了这样的表,我怎样才能得到NoBoxes
Where Carrier
= UPS、ProcessDateTime
= Today、Errored
= 0 和TrackingNumber
Have Count = 1 的总和?
重复的追踪编号代表已作废的货件。我不想合计Errored
发货,因为那些没有发货。
我已经尝试了大约 10 种不同的说法,但似乎没有什么能让我到达我需要的地方。
我相信的问题是无效行不包含ProcessDateTime
.
所以当我使用类似的东西时:
SELECT Sum(NoBoxes)
FROM Info
WHERE (Carrier='UPS') AND (ProcessDateTime>{ts '2013-05-23 00:00:00'} And
ProcessDateTime<{ts '2013-05-24 00:00:00'}) AND (Errored=0)
GROUP BY TrackingNumber
HAVING (Count(*)=1)
它仍然返回TrackingNumber
已作废,因为查询不包含任何带有 null 的行ProcessDateTime
所以我尝试了:
SELECT Sum(NoBoxes), ProcessDateTime
FROM Info
WHERE ((Carrier='UPS') AND (Errored=0))
OR
((Carrier='UPS') AND (ProcessDateTime Is Null) AND (Errored=0))
GROUP BY ProcessDateTime, TrackingNumber
HAVING (Count(*)=1)
但这仍然不能完成它只会返回所有内容的工作。
也尝试过HAVING (Count(TrackingNumber)=1)
,但似乎没有做任何事情。
我只是不知道如何摆脱所有重复的跟踪号码,然后返回所有的总和,UPS
并带有一个ProcessDateTime
符合条件的值。因为似乎摆脱重复项的唯一方法是根本不使用ProcessDateTime
,但是我无法告诉它是什么ProcessDateTime
,我需要知道才能过滤特定日期。
我有点明白我可能不得不做类似的事情:
(Select TrackingNumber
From Info
Where Carrier = 'UPS' And Errored = 0
Group By TrackingNumber HAVING Count(*) = 1) As A
然后按照以下方式做一些事情:
Select Sum(NoBoxes) As Total
From Info
Join A On Info.TrackingNumber = A.TrackingNumber
Where Info.ProcessDateTime>{ts '2013-05-23 00:00:00'} And
Info.ProcessDateTime<{ts '2013-05-24 00:00:00'}
但是我根本不知道如何在这样的查询上获得所有正确的顺序和语法。
鉴于提供的表,我希望返回一个带有单个列的单行,其值为24