SQL:使用多个条件为每个名称选择一个项目

时间:2019-03-20 16:50:42

标签: sql sql-server

我正在尝试根据多个条件在“名称”列中为每个值选择一个项目。

我要使用的条件如下:

  1. 仅包含IsEnabled = 1的结果
  2. 返回优先级最低的单个结果(我们使用1表示“最高优先级”)
  3. 如果出现平局,请返回带有最新时间戳的结果

我还看到了其他几个问题,这些问题要求返回给定值的最新时间戳,我已经能够对其进行调整以返回Priority的最小值-但我不知道该如何过滤掉优先级和时间戳。

Here is the question that's been most helpful in getting me this far.

样本数据:

+------+------------+-----------+----------+
| Name |  Timestamp | IsEnabled | Priority |
+------+------------+-----------+----------+
|  A   | 2018-01-01 |     1     |    1     |
|  A   | 2018-03-01 |     1     |    5     |
|  B   | 2018-01-01 |     1     |    1     |
|  B   | 2018-03-01 |     0     |    1     |
|  C   | 2018-01-01 |     1     |    1     |
|  C   | 2018-03-01 |     1     |    1     |
|  C   | 2018-05-01 |     0     |    1     |
|  C   | 2018-06-01 |     1     |    5     |
+------+------------+-----------+----------+

所需的输出:

+------+------------+-----------+----------+
| Name |  Timestamp | IsEnabled | Priority |
+------+------------+-----------+----------+
|  A   | 2018-01-01 |     1     |    1     |
|  B   | 2018-01-01 |     1     |    1     |
|  C   | 2018-03-01 |     1     |    1     |
+------+------------+-----------+----------+

到目前为止,我一直在尝试什么(这使我仅启用了具有最低优先级的项目,但在出现平局的情况下不会过滤最新的项目):

SELECT DATA.Name, DATA.Timestamp, DATA.IsEnabled, DATA.Priority
From MyData AS DATA
INNER JOIN (
  SELECT MIN(Priority) Priority, Name
  FROM MyData
  GROUP BY Name
) AS Temp ON DATA.Name = Temp.Name AND DATA.Priority = TEMP.Priority
WHERE IsEnabled=1

Here is a SQL fiddle as well.

如何增强此查询以仅返回除现有过滤器之外的最新结果?

2 个答案:

答案 0 :(得分:0)

使用row_number()

select d.*
from (select d.*,
             row_number() over (partition by name order by priority, timestamp) as seqnum
      from mydata d
      where isenabled = 1
     ) d
where seqnum = 1;

答案 1 :(得分:0)

我发现这些问题最有效的方法是使用CTE和import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonRootName; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; public class JsonApp { public static void main(String[] args) throws Exception { String json = "{...}"; ObjectMapper mapper = new ObjectMapper(); mapper.enable(DeserializationFeature.UNWRAP_ROOT_VALUE); ContainerStatus cs = mapper.readValue(json, ContainerStatus.class); System.out.println(cs.getState()); } } @JsonRootName("containerStatuses") class ContainerStatus { @JsonProperty("name") private String name; @JsonProperty("state") private JsonNode state; @JsonProperty("lastState") private JsonNode lastState; @JsonProperty("ready") private Boolean ready; @JsonProperty("restartCount") private Integer restartCount; @JsonProperty("image") private String image; @JsonProperty("imageID") private String imageID; @JsonProperty("containerID") private String containerID; // getters, setters, toString }

{"terminated":{"exitCode":1,"reason":"Error","startedAt":"2019-03-20T15:40:08Z","finishedAt":"2019-03-20T15:40:50Z","containerID":"docker://"}}