3

样本数据:

+===========================================================================+
|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|
+---------------------------------------------------------------------------+

现在有了这样的表,我怎样才能得到NoBoxesWhere Carrier= UPS、ProcessDateTime= Today、Errored= 0 和TrackingNumberHave 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

4

4 回答 4

1
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 小提琴版本

于 2013-05-24T15:15:03.463 回答
0

Here is everything. First part is what I created to test. Second part is what you need

DECLARE @Info TABLE
(
    NoBoxes int,
    Carrier varchar(50),
    ProcessDateTime DateTime,
    Errored int,
    Voided int,
    TrackingNumber varchar(100)
)

Insert into @Info (NoBoxes, Carrier, ProcessDateTime, Errored, Voided, TrackingNumber) Values ( 2, 'UPS', '2013-05-22 8:14', 0, 0, '1Z1234567891234567')
Insert into @Info (NoBoxes, Carrier, ProcessDateTime, Errored, Voided, TrackingNumber) Values ( NULL, 'UPS', NULL, 0, 1, '1Z1234567891234567')
Insert into @Info (NoBoxes, Carrier, ProcessDateTime, Errored, Voided, TrackingNumber) Values ( 5, 'UPS', '2013-05-22 8:22', 1, 0, NULL)
Insert into @Info (NoBoxes, Carrier, ProcessDateTime, Errored, Voided, TrackingNumber) Values ( 7, 'UPS', '2013-05-22 8:14', 0, 0, '1Z9876543210987654')
Insert into @Info (NoBoxes, Carrier, ProcessDateTime, Errored, Voided, TrackingNumber) Values ( 1, 'UPS', '2013-05-22 8:22', 0, 0, '1Z1472583691472583')
Insert into @Info (NoBoxes, Carrier, ProcessDateTime, Errored, Voided, TrackingNumber) Values ( 1, 'FedEx', '2013-05-22 8:14', 0, 0, 'xxxxxxxxxxxxxxxx')
Insert into @Info (NoBoxes, Carrier, ProcessDateTime, Errored, Voided, TrackingNumber) Values ( 8, 'FedEx', '2013-05-22 8:22', 0, 0, 'yyyyyyyyyyyyyyyy')
Insert into @Info (NoBoxes, Carrier, ProcessDateTime, Errored, Voided, TrackingNumber) Values ( 3, 'USPS', '2013-05-22 8:14', 0, 0, 'zzzzzzzzzzzzzzzz')
Insert into @Info (NoBoxes, Carrier, ProcessDateTime, Errored, Voided, TrackingNumber) Values ( 4, 'USPS', '2013-05-22 8:22', 0, 0, 'aaaaaaaaaaaaaaaa')
Insert into @Info (NoBoxes, Carrier, ProcessDateTime, Errored, Voided, TrackingNumber) Values ( 7, 'UPS', '2013-05-22 8:14', 0, 0, '1Z9638527411012396')
Insert into @Info (NoBoxes, Carrier, ProcessDateTime, Errored, Voided, TrackingNumber) Values ( 9, 'UPS', '2013-05-22 8:22', 0, 0, '1Z4561591981655445')




;

I would definitely use a CTE

WITH FIRSTCTE (TrackingNumber)
AS
(
    Select TrackingNumber 
    From @Info 
    Where Carrier = 'UPS' And Errored = 0
    Group By TrackingNumber
)
select *
FROM FIRSTCTE as C
    INNER JOIN @Info as I
        ON I.TrackingNumber = C.TrackingNumber
WHERE DATEADD(D, 0, DATEDIFF(D, 0, I.ProcessDateTime)) = '2013-05-22'
于 2013-05-24T15:00:39.927 回答
0

我认为您只想ProcessDateTime = NULL在第一个查询中包含检查:

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'}) or
       ProcessDateTime is NULL
      ) AND
      (IsError=0)
GROUP BY TrackingNumber
HAVING (Count(*)=1)

如果您想要总和,则将其设为子查询并将其中的值相加。

于 2013-05-24T15:00:37.690 回答
0

在阅读了我自己的问题之后,我意识到如何使用我应该使用的完全相同的子查询来做到这一点,这完全符合我的希望。

Select  Sum(NoBoxes) As Total

From Info

Join 
    (Select TrackingNumber 
     From Info 
     Where Carrier = 'UPS' And Errored = 0
     Group By TrackingNumber HAVING Count(*) = 1) As A

On Info.TrackingNumber = A.TrackingNumber

Where Info.ProcessDateTime>{ts '2013-05-22 00:00:00'} And 
      Info.ProcessDateTime<{ts '2013-05-23 00:00:00'}
于 2013-05-24T16:40:09.963 回答