SQL Server显示连续失败的计数

时间:2017-05-25 17:43:34

标签: sql-server

我需要能够根据此示例表检索每个区域和频道的连续失败次数:

<nav class="navbar navbar-default navbar-fixed-top menu">
      <div class="container menu">
           <a class="navbar-brand menuButton" href="#">ABOUT</a>
           <a class="navbar-brand menuButton" href="#">SKILLS</a>
           <a class="navbar-brand menuButton" href="#">PORTFOLIO</a>
           <a class="navbar-brand menuButton" href="#">EXPERIENCE</a>
      </div>
    </nav>
    <div class="content-box content">

         <section >
          <div>

                 <div class="circletag" id="nay">
                    <img src="img/defaultAvatars.png">
                 </div>

                  <h1 class="heading">SHRUTI NAIR</h1>
                  <span>Hybrid Mobile App developer</span>
            </div>
         </section>
    </div>

每次收到False,计数器都会重置。使用这个存储过程,我能够显示每个区域的连续失败次数,失败的通道1,但我无法弄清楚如何在结果中包含fail2。

id   reportdate             region  channel fail1   fail2
1   2017-05-22 00:00:00.000 canada  A   True    False
3   2017-05-23 00:00:00.000 canada  A   True    False
4   2017-05-24 00:00:00.000 canada  A   False   True
5   2017-05-25 00:00:00.000 canada  A   True    True
6   2017-05-25 00:00:00.000 canada  B   True    True

我当前的结果集是这个,我需要它来显示两种类型的错误计数:

BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
SELECT
    region, channel, MAX(reportDate),
    COUNT(*) AS consecutive_errors
FROM
    dfc_test t1
WHERE
    NOT EXISTS
    (
        SELECT * FROM dfc_test t2
        WHERE t2.region = t1.region
        AND t2.channel = t1.channel
        AND t2.reportDate >= t1.reportDate
        AND t2.fail1 = 0

    )
GROUP BY
    region, channel
HAVING
    MAX(reportDate) = MAX(CASE WHEN fail1 = 1 THEN reportDate END)

需要结果如下:

region  channel (No column name)    consecutive_errors
canada  A   2017-05-25 00:00:00.000 1
canada  B   2017-05-25 00:00:00.000 1

0 个答案:

没有答案