Sql计算的计数大于列值

时间:2017-09-22 14:44:55

标签: sql sql-server-2008

我有一个表,其中保存了deleieverddate,一些传递的日期为空,其中一些不是我想要计算delievered日期为空的计数,并跳过那些在两者之间错过的日期。

deleievered date

NULL
NULL
NULL

计数应为3。

NULL
NULL
9/22/2017
NULL
10/22/2017
NULL

计数应为1 这里跳过所有以前deievered日期不为空的

我试过这个但是成本太高了。

DECLARE @myTable TABLE
    (
      MessageId BIGINT ,
      ReceiverID VARCHAR(100)
    )

DECLARE @mySecondTable TABLE
    (
      MessageId BIGINT ,
      ReceiverID VARCHAR(100),
      DeliveredDate DATETIME
    )


DECLARE @myLastTable TABLE
    (
      MessageId BIGINT ,
      ReceiverID VARCHAR(100)
    )





INSERT  INTO @myTable
        ( MessageId ,
          ReceiverID

        )
        SELECT  MAX(MessageID) ,
                ReceiverID
        FROM    dbo.CM_MessageStatus
        WHERE   ReceiverID IN ( SELECT  *
                                FROM    string_split(@UserID, ',') )
                AND DeliveredDate IS NOT NULL
        GROUP BY ReceiverID






INSERT  INTO @mySecondTable
        ( MessageId ,
          ReceiverID,
          DeliveredDate

        )

SELECT  CM_MessageStatus.MessageID,
        dbo.CM_MessageStatus.ReceiverID,
        DeliveredDate
FROM    dbo.CM_MessageStatus
WHERE   DeliveredDate IS NULL AND

         ReceiverID IN ( SELECT  *
                            FROM    string_split(@UserID, ',') )
--GROUP BY dbo.CM_MessageStatus.ReceiverID,DeliveredDate




--Now check each userid
declare @ReceiverID NVARCHAR(MAX)
while exists (select * from @mySecondTable)
BEGIN

            select top 1 @ReceiverID = ReceiverID
            from @mySecondTable
            order by ReceiverID ASC 





        IF EXISTS( SELECT * FROM @myTable WHERE ReceiverID=@ReceiverID)
        BEGIN
                            INSERT  INTO @myLastTable
                                    ( MessageId ,
                                      ReceiverID

                                    )


                                    SELECT  MessageID,
                                            @ReceiverID
                                    FROM    @mySecondTable
                                    WHERE   DeliveredDate IS NULL
                                            AND MessageId > ( SELECT
                                                      MessageId
                                                      FROM
                                                      @myTable
                                                      WHERE
                                                      ReceiverID = @ReceiverID
                                                      )
                                                      AND ReceiverID=@ReceiverID
                                                      --GROUP BY ReceiverID
        END


        ELSE
        BEGIN
                INSERT  INTO @myLastTable
                                    ( MessageId ,
                                      ReceiverID

                                    )
                                   SELECT  MessageID ,
                                            ReceiverID
                                    FROM    @mySecondTable
                                    WHERE   DeliveredDate IS NULL
                                    AND ReceiverID=@ReceiverID
        END   


        delete @mySecondTable
        where ReceiverID = @ReceiverID
END 

    SELECT COUNT(MessageId) AS MessageId,ReceiverID FROM @myLastTable
    GROUP BY ReceiverID

耗时35秒,耗资8万,这太贵了,我怎么能在一个选择中做到这一点。努力是问题所在。

1 个答案:

答案 0 :(得分:0)

嗯,你必须对数据有一些订单。也就是说,ID列,或者表示插入行的时间与所有其他行相比。在这个例子中,我使用MessageID。每次插入一行时,它都会获得一个比前一个大1的新MessageID。没有SOME列(插入日期)或PrimaryKey / Auto Increment列,这是不可能的。所以,如果你有一个,这就是你可以做到的。

declare @table table (MessageId  int identity(1,1), dt date)
insert into @table

values
(NULL),
(NULL),
('9/22/2017'),
(NULL),
('10/22/2017'),
(NULL)



select
    count(*)
from 
    @table
where 
    --limits rows to the last one where data is not null
    MessageID > (
    select max(MessageID)
    from @table
    where dt is not null)
or
    --if all dates are null
    (
    select max(dt)
    from @table) is null