SQL查询确定带有附加条件的最旧消息

时间:2018-08-08 21:12:21

标签: sql-server

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

我有一个名为inboundMessages的表,该表充当一种FIFO队列,但是有点儿混乱。我需要获取状态为01的最旧的未处理消息(状态3)。

示例:

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. 

ID 114无效,因为其他行具有相同的messageID且状态为1或3意味着这些行将 需要解决后才能将其拿起。

ID 121几乎是有效的,因为没有MessageID为1237的较早消息,但是它不是最旧的。

ID 120是有效的,因为它具有1235的较早消息,但其状态为2,表示已成功归档。

我已经尝试过使用具有状态的查询并尝试使用“具有”子句,但是由于我没有得到任何结果,这似乎并不正确。

我的查询:

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

3 个答案:

答案 0 :(得分:1)

我使用了两步过程,首先获取有效的行(状态= 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

答案 1 :(得分:0)

我认为应该这样做。

<!DOCTYPE html>
<html>
<head>
    <meta charset='utf-8' />
    <title>Create a hover effect</title>
    <meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
    <script src='https://api.tiles.mapbox.com/mapbox-gl-js/v0.47.0/mapbox-gl.js'></script>
    <link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.47.0/mapbox-gl.css' rel='stylesheet' />
    <style>
        body { margin:0; padding:0; }
        /* #map { position:absolute; top:0; bottom:0; width:100%; } */
        #map { width:100%; height: 500px; }
    </style>
</head>
<body>

<div id='map'></div>
<script>
mapboxgl.accessToken = 'pk.eyJ1IjoiY29kYm9kIiwiYSI6IjhjbFE1aUUifQ.Gimi98Oh3Uex9WQZlb5Wkw';
var map = new mapboxgl.Map({
    container: 'map',
    style: 'mapbox://styles/mapbox/streets-v9',
    center: [-0.1507524, 51.5140731],
    zoom: 9
});
var hoveredStateId =  null;

map.on('load', function () {
    map.addSource("sites", {
        "type": "geojson",
        "data": {
          "type": "FeatureCollection",
          "features": [
            {
              "type": "Feature",
              "geometry": {
                "type": "Point",
                "coordinates": [
                  -0.1407524,
                  51.5240731
                ]
              },
              "properties": {
                "id": 2,
                "name": "Oxford Street"
              }
            }
          ]
        }
    });

    // The feature-state dependent fill-opacity expression will render the hover effect
    // when a feature's hover state is set to true.
    map.addLayer({
        'id': 'sites-bold',
        'type': 'circle',
        'source': 'sites',
        'layout': {},
        'paint': {
          'circle-radius': 10,
          // 'circle-color': '#ff442b',
          'circle-color': ['case',
            ['boolean', ['feature-state', 'hover'], false],
              'red',
              'blue'
            ],
          'circle-opacity': 0.6,
          'circle-stroke-color': 'cyan',
          'circle-stroke-width': 1
        }
      });


    // When the user moves their mouse over the state-fill layer, we'll update the
    // feature state for the feature under the mouse.
    map.on("mousemove", "sites-bold", function(e) {
      // console.log('in', e.features)
      if (e.features.length > 0) {
          // if (this.hoveredStateId) {
          //     map.setFeatureState({source: 'sites', id: this.hoveredStateId}, { hover: false});
          // }
          this.hoveredStateId = e.features[0].properties.id;
          console.log('in-f-id', this.hoveredStateId)
          map.setFeatureState({source: 'sites', id: this.hoveredStateId}, { hover: true});
          console.log('in-f', e.features[0])
      }
      console.log('in', map.getFeatureState({source: 'sites', id: this.hoveredStateId}))
    });

    // When the mouse leaves the state-fill layer, update the feature state of the
    // previously hovered feature.
    map.on("mouseleave", "sites-bold", function() {
      console.log('out-id', this.hoveredStateId)
      // if (this.hoveredStateId) {
          map.setFeatureState({source: 'sites', id: this.hoveredStateId}, { hover: false});
      // }
      console.log('out', map.getFeatureState({source: 'sites', id: this.hoveredStateId}))
      hoveredStateId =  null;
    });
});
</script>

</body>
</html>

答案 2 :(得分:0)

我认为这可以为您提供帮助。

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