Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 使用附加条件确定最早消息的SQL查询_Sql Server - Fatal编程技术网

Sql server 使用附加条件确定最早消息的SQL查询

Sql server 使用附加条件确定最早消息的SQL查询,sql-server,Sql Server,我有一个相当复杂的问题,表面上听起来很简单,但事实证明比我预想的要麻烦得多 我有一个名为inboundMessages的表,它充当一种FIFO队列,但有点扭曲。我需要获取状态为1或3的最旧未处理邮件(状态为0) 示例: Id messageID Status ReceivedDateTime ------------------------------------------------------- 112 1234 1 201

我有一个相当复杂的问题,表面上听起来很简单,但事实证明比我预想的要麻烦得多

我有一个名为
inboundMessages
的表,它充当一种FIFO队列,但有点扭曲。我需要获取状态为
1
3
的最旧未处理邮件(状态为
0

示例:

Id     messageID     Status     ReceivedDateTime
-------------------------------------------------------
112    1234          1          2018-08-07 14:52:48.657
113    1234          3          2018-08-08 14:52:48.657
114    1234          0          2018-08-07 14:52:48.657
119    1235          2          2018-08-07 16:05:10.870
120    1235          0          2018-08-08 10:37:09.980
121    1237          0          2018-08-09 05:43:08.824
想要的结果

120    1235          0          2018-08-08 10:37:09.980
ID       Status
0        Not picked up, not processed
1        Picked up - not processed 
2        Processed Successfully 
3        Error during processing 
4        Errored - Refiled
5        Errored - Ignored, unresolvable. 
状态

120    1235          0          2018-08-08 10:37:09.980
ID       Status
0        Not picked up, not processed
1        Picked up - not processed 
2        Processed Successfully 
3        Error during processing 
4        Errored - Refiled
5        Errored - Ignored, unresolvable. 
ID 114无效,因为其他行具有相同的messageID,并且状态为1或3意味着它们将 需要先解决,然后才能提取

ID121几乎是有效的,因为没有MessageID为1237的早期消息,但它不是最早的消息

ID 120是有效的,因为它有一个较早的消息1235,但它的状态是2,这意味着它已成功提交

我尝试过使用status查询并尝试使用having子句,但它似乎不正确,因为我没有得到任何结果

我的问题是:

SELECT [Id]
       ,[MessageID]
      ,[Status]
      ,[ReceivedDateTime]
       ,COUNT(case when  status = 2  or status = 3  then 1 END) as  'Count'
  FROM inboundMessages

 group by [Id]
       ,[MessageID]
      ,[Status]
      ,[ReceivedDateTime]

  having Status=0 and COUNT(case when  status = 1 or status = 3 then 1 END) >=1

我想这应该可以

select 
--select only 1 record
top 1 *
--from your base table
from inboundMessages
--where the messageID has a status of 0 and 2
where messageID in (select messageID from inboundMessages where Status not in (1,3)  group by messageID having count(distinct Status) > 1)
--of those messageID's that meet the where criteria, bring back only the row with Status = 0
and Status = 0
--make sure it's the oldest record (to only get one, and the correct one)
order by ReveivedDateTime desc

我使用了一个两步过程,首先获取有效的行(Status=0),然后获取集合中没有另一行的行(1或3)

CREATE TABLE #tmp(Id int, messageID int, Status int,ReceivedDateTime datetime)

INSERT INTO #tmp VALUES(112,1234,1,'2018-08-07 14:52:48.657')
INSERT INTO #tmp VALUES(113,1234,3,'2018-08-08 14:52:48.657')
INSERT INTO #tmp VALUES(114,1234,0,'2018-08-07 14:52:48.657')
INSERT INTO #tmp VALUES(119,1235,2,'2018-08-07 16:05:10.870')
INSERT INTO #tmp VALUES(120,1235,0,'2018-08-08 10:37:09.980')
INSERT INTO #tmp VALUES(121,1237,0,'2018-08-09 05:43:08.824')

;WITH CTE as 
    (
    SELECT Id,messageID,ReceivedDateTime from #tmp t1 where Status = 0
    )
SELECT  TOP 1 * from CTE
 where messageID not in(select messageID from #tmp where Status in (1,3))
 ORDER BY ReceivedDateTime

我想这可能对你有帮助

SELECT
    Id, messageID, Status, ReceivedDateTime
FROM inboundMessages
WHERE messageID IN (
    SELECT
        messageID
    FROM inboundMessages
    WHERE Status NOT IN (1, 3)
    GROUP BY messageID
    HAVING COUNT(messageID) > 1
)
    AND Status = 0 
GROUP BY Id, messageID, Status, ReceivedDateTime

谢谢你的帮助。我试图运行它,但我看到“inboundMessages.MessageID”列的消息在选择列表中无效,因为它不包含在聚合函数或GROUP BY子句中。我在查询中添加了一个GROUP BY,现在没有收到任何结果。非常好。你能解释一下它是怎么工作的吗?我想我明白了,我只是想确认一下。请慢慢来,现在我有一些工作可以做,但如果我需要向其他人解释,我将不胜感激。添加了一些评论@RJohnson@scsimon如果您需要更多澄清,请告诉我。我做了一些测试,查询似乎忽略了只有一个条目的行。我删除了前1个*以便能够看到整个有效条目的“列表”,我只得到ID为120的条目。我还将它的接收日期调整为121的日期之后,它仍然只返回120。在那种情况下,我希望看到121个。有什么建议吗?但你说121几乎是有效的,没有包括在预期的结果中。为什么现在就要把它包括进来?这就是我们在与同事协商后得出的结论。非常感谢。