简体   繁体   English

如何选择未超过值的记录

[英]How do I select records where a value has not been exceeded

I wish to extract from a database a list of job Ids where the status for each job has not exceeded a given criteria. 我希望从数据库中提取一份工作ID列表,其中每个工作的状态未超过给定条件。

For this example table I want to display all the jobid that have not exceeded status 200 and display only the latest status. 对于此示例表,我想显示所有未超过状态200的Jobid,并仅显示最新状态。

Job-Progress table 作业进度表

Jobid Status Date      Time
1234  100    20131001  080000
1234  200    20131001  100000
1234  300    20131001  140000
9876  100    20131014  110000
5555  100    20131015  100000
5555  200    20131016  080000

The result i am looking for is 我正在寻找的结果是

Jobid Status Date      Time
9876  100    20131014  110000
5555  200    20131016  080000

Database is on AS400 数据库在AS400上

This is a good use of window/analytic functions. 这是窗口/分析功能的良好用法。

You can use row_number() to get the most recent status. 您可以使用row_number()获取最新状态。 You can use sum() over to count the number of times that status exceeds 200. 您可以使用sum() over来计算状态超过200的次数。

select jobid, Status, Date, Time
from (select jp.*,
             row_number() over (partition by jobid order by date desc, time desc) as seqnum,
             sum(case when status >= 200 then 1 else 0 end) over (partition by jobid) as status_200
      from JobProgress jp
     ) jp
where status_200 = 0 and seqnum = 1;

The where clause then filters to the rows you are looking for. 然后where子句将过滤到要查找的行。

select t1.* 
from your_table t1
inner join 
(
  select Jobid, max(date*1000000+time) as maxdt
  from your_table
  group by jobid
  having sum(case when status > 200 then 1 else 0 end) = 0
) t2 on t1.jobid = t2.jobid 
     and t1.date*100000+t1.time = maxdt

In SQL server you can use ROW_NUMBER : 在SQL Server中,您可以使用ROW_NUMBER

SELECT *
FROM (SELECT
            Jobid,
            status,
            ROW_NUMBER() OVER(PARTITION BY Jobid ORDER BY Date DESC) AS rn
      FROM
            Job-Progress
      WHERE Status < 200) A
WHERE RowNum = 1

This is the simplest approach I can think of: 这是我能想到的最简单的方法:

SELECT t.* FROM t
INNER JOIN (
  SELECT jobid, max(date) date FROM t
  GROUP BY jobid
  HAVING COUNT(CASE WHEN status > 200 THEN 1 END) = 0
) s
ON t.jobid = s.jobid AND t.date = s.date

Fiddle here . 在这里摆弄。

A common table expression will allow you to employ the ROW_NUMBER() function to mark the latest row of each job as 1. Then all you need to do is pick that row whenever its status is acceptable. 一个通用的表表达式将允许您使用ROW_NUMBER()函数将每个作业的最新行标记为1。然后,您所要做的就是在状态允许的情况下选择该行。

With x as
(SELECT *,
        ROW_NUMBER() OVER(PARTITION BY Jobid ORDER BY Date, time DESC) AS pick
      FROM JobProgress
)
SELECT jobid, status, date, time
  FROM x
  WHERE Status <= 200
    And pick = 1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何选择未超过不同列的多个值的记录 - How do I select records where multiple values of different columns have not been exceeded 如何选择在给定日期尚未更新的MySQL表值? - How do I SELECT a MySQL Table value that has not been updated on a given date? 我怎样才能 select 具有相同值的记录? - How can I select records which has same value? 我如何 select 多行,其中一行在列中具有特定值? - How do I select multiple rows where one row has a specific value in a column? 如何选择字段具有特定值的所有记录,直到显示具有不同值的记录? - How to select all records where a field is of a certain value until a record shows up that has a different value? 选择相关表中没有值的记录的所有行 - Select all rows where related table has no records with value 如何选择任何列具有特定值的sql数据库表中的所有记录 - How to select all records in sql database tables where any column has a particular value 如何使用SQL / mySQL选择2列具有相同值的多个且1列具有不同值的行? - How do I use SQL/mySQL to select rows where 2 columns have multiple of the same value and 1 column has a distinct value? 如何从汽车停止的数据库位置中进行选择? - How to select from database positions where car has been stopped? 如何通过比较列A中具有相同值的记录中的列B,C中的值来选择记录? - How do I select records by comparing values in columns B, C among records with same value in column A?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM