计算在SQL中满足多个和可变标准的记录,如countifs

时间:2016-06-09 08:47:11

标签: sql-server count sum aggregate-functions

我想计算符合某些条件的记录,这些条件的标准来自行中的特定值,这些值的变化很大。

下表摘录自50K +记录表;

BatchNumber     |  Number  | LabId | StateId |  Week   | OccurrenceId
----------------+----------+-------+---------+---------+-------------
101347-1M-37    |  101347  |    1  |    100  |  201610 |    
101347-1-1546L  |  101347  |    1  |    100  |  201611 |    
101347-1M-41    |  101347  |    1  |    100  |  201614 |    
101347-1M-1545  |  101347  |    1  |    100  |  201618 |    
101347-1-37     |  101347  |    1  |    101  |  201607 |    
101347-1-1541   |  101347  |    1  |    101  |  201611 |    
101347-1M-37    |  101347  |    1  |    101  |  201616 |    
101347-1-1546L  |  101347  |    1  |    101  |  201617 |    
101347-1M-41    |  101347  |    1  |    101  |  201620 |    

我想在Excel中使用countifs函数计算记录的出现次数。

sum()count()函数与case语句相结合我不会这样做,因为条件会根据行中的值而有所不同。

到目前为止,我在声明中获得的eventId是:

sum(case when v.number = v.number 
          and b.LabId = b.LabId
          and po.StateId = po.StateId
          and po.Week < po.Week
         then 1 
         else 0 
     end) as occurenceid

此语句导致每条记录为0。

但是省略and po.Week < po.Week部分会返回这个(这对于我的生活我不明白,但它似乎在某处);

BatchNumber     |  Number  | LabId | StateId |  Week   | OccurrenceId
----------------+----------+-------+---------+---------+-------------
101347-1M-37    |  101347  |    1  |    100  |  201610 |    1
101347-1-1546L  |  101347  |    1  |    100  |  201611 |    1
101347-1M-41    |  101347  |    1  |    100  |  201614 |    1
101347-1M-1545  |  101347  |    1  |    100  |  201618 |    1
101347-1-37     |  101347  |    1  |    101  |  201607 |    5
101347-1-1541   |  101347  |    1  |    101  |  201611 |    2
101347-1M-37    |  101347  |    1  |    101  |  201616 |    4
101347-1-1546L  |  101347  |    1  |    101  |  201617 |    1
101347-1M-41    |  101347  |    1  |    101  |  201620 |    1

我不能简单地写v.number = 101347 and b.LabId = 1 and po.StateId = 100,因为我需要计算所有记录,并且适用许多不同的数字,LabIds,StateIds等。

为了清楚起见,我需要实现的结果如下:

BatchNumber     |  Number  | LabId | StateId |  Week   | OccurrenceId
----------------+----------+-------+---------+---------+-------------
101347-1M-37    |  101347  |    1  |    100  |  201610 |    1
101347-1-1546L  |  101347  |    1  |    100  |  201611 |    2
101347-1M-41    |  101347  |    1  |    100  |  201614 |    3
101347-1M-1545  |  101347  |    1  |    100  |  201618 |    4
101347-1-37     |  101347  |    1  |    101  |  201607 |    1
101347-1-1541   |  101347  |    1  |    101  |  201611 |    2
101347-1M-37    |  101347  |    1  |    101  |  201616 |    3
101347-1-1546L  |  101347  |    1  |    101  |  201617 |    4
101347-1M-41    |  101347  |    1  |    101  |  201620 |    5

1 个答案:

答案 0 :(得分:0)

知道了!使用具有多个内部连接条件的子查询。

select v.Number, b.LabId, po.StateId, po.Week, count( sub.Week ) as occurrenceid

from Batch b

inner join ProductionOrder po on po.BatchId = b.BatchId
inner join Variety v on b.VarietyId = v.VarietyId
inner join 
    (select v.Number, b.LabId, po.StateId, po.Week

    from Batch b

    inner join ProductionOrder po on po.BatchId = b.BatchId
    inner join Variety v on b.VarietyId = v.VarietyId

    ) sub on v.number = sub.Number and b.LabId = sub.LabId and po.StateId = sub.StateId and po.Week >= sub.Week

where v.Number = 101347 and po.StateId > 99

group by v.Number, b.LabId, po.StateId, po.Week

它可能不是最优雅的解决方案,因此仍然渴望看到更好的答案。