样本数据:
+===========================================================================+
|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
的总和,其中Carrier
= UPS,ProcessDateTime
= Today,Errored
= 0,以及TrackingNumber
具有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
,因为查询不包含任何空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
值的单列的单行 最佳答案
Select Sum( NoBoxes )
From Info As I
Where Carrier = 'UPS'
And ProcessDateTime >= '2013-05-22 00:00:00'
And ProcessDateTime < '2013-05-23 00:00:00'
And Errored = 0
And Voided = 0
And Not Exists (
Select 1
From Info As I1
Where I1.TrackingNumber = I.TrackingNumber
And Voided <> 0
)
我注意到的一项是,示例查询中的日期范围与您提供的示例数据不重叠。下面是SQL Fiddle版本的链接。您没有指定数据库和版本,所以我使用了SQL Server,但是上述解决方案几乎可以在所有数据库产品中使用。
SQL Fiddle version