Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/73.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/9.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条件选择与for/while循环_Sql_Oracle_For Loop_Conditional - Fatal编程技术网

SQL条件选择与for/while循环

SQL条件选择与for/while循环,sql,oracle,for-loop,conditional,Sql,Oracle,For Loop,Conditional,这是表格: Order_ID|Priority|Status A |3 |Normal A |4 |Urgent B |6 |Urgent B |7 |Normal C |8 |Normal C |9 |Urgent 如何选择订单ID和状态,其中该ID的行优先级较高?例如,在这种情况下,给定上述数据的查询输出应为: A - Urgent B - N

这是表格:

Order_ID|Priority|Status 
A       |3       |Normal
A       |4       |Urgent
B       |6       |Urgent
B       |7       |Normal 
C       |8       |Normal
C       |9       |Urgent
如何选择订单ID和状态,其中该ID的行优先级较高?例如,在这种情况下,给定上述数据的查询输出应为:

A - Urgent
B - Normal
C - Urgent

一种方法是:

select order_id || ' - ' || status
from (
    select order_id, priority, status,
           rank() over (partition by order_id order by priority desc) ranking
    from table
)
where ranking = 1;

使用SQL执行此操作的另一种方法是简单地获取每个订单ID的
MAX(Priority)
,然后使用SUBSELECT返回到该表:

SELECT  x.ORDER_ID + ' - ' + x.Status AS [Output],
    x.Priority ,
    x.Status
FROM    ( SELECT    Order_ID AS Order_ID ,
                MAX(Priority) AS [Priority]
      FROM      @your_table
      GROUP BY ORDER_ID
    ) t
    JOIN @your_table x ON x.Order_ID = t.Order_ID
                       AND x.[Priority] = t.[Priority];
其中,
@您的_表
是表的名称

这是查询的输出:

Output      Priority    Status
A - Urgent  4           Urgent
B - Normal  7           Normal
C - Urgent  9           Urgent

具有最高优先级的记录-->>没有具有更高优先级的记录


返回

A - Urgent
B - Normal
C - Urgent

这需要在表上进行两次传递,如果业务任务执行的概要文件在总响应时间的贡献者底部显示此查询,这将不是什么大问题。生成的计划取决于查询计划器/生成器和索引的可用性。我非常喜欢窗口功能。他们让这样的事情变得容易。对于OP,还要注意,如果此解决方案(rank())有两个具有相同最高优先级的order_id,则它将返回多行。@Shawn,这就是为什么我建议OP针对这些类型的问题提供可复制+粘贴的DDL和DML语句,以激发需要较少时间创建的更好答案。为了将来的参考,我建议,对于这些类型的问题,您可以提供可复制+粘贴的DDL和DML语句,以激发需要较少时间创建的更好答案。例如,如果存在唯一约束(订单id、优先级),则解决方案可能比存在唯一约束(订单id、优先级、创建日期)(即,如果存在一个创建日期指示优先级更改的时间)或没有唯一约束更容易创建。
select Order_ID || ' - ' || (
    select top 1 Status
    from priorities as p2
    where p1.Order_ID = p2.Order_ID 
    order by p2.Priority desc)
from priorities as p1
group by Order_ID
order by max(Priority)
A - Urgent
B - Normal
C - Urgent