简体   繁体   English

使用计数选择最小值和最大值

[英]Select min and max with count

I have a table called 'Medical Report' I need to select min date,max date in prescriptionwritten and corresponding active status for particular account number,code我有一个名为“医疗报告”的表格,我需要选择最小日期、处方中的最大日期以及特定帐号、代码的相应活动状态

Accountnumber code   prescriptionwritten.  Active      
101            201          2019-10-21         0          
101            201          2020-03-24         0     
101            201          2020-05-25         1     
101            202          2019-10-21         0     
101            202          2020-06-20         1

Please help with sql query.请帮助进行sql查询。

The output:输出:

101 201 2019-10-21 0  
101 201 2020-05-25 1  
101 202 2019-10-21 0  
101 202 2020-06-20 1

You can try using row_number()您可以尝试使用row_number()

select * from
(
select *,row_number() over(partition by code order by prescriptionwritten) as minrn,
row_number() over(partition by code order by prescriptionwritten desc) as maxrn
from tablename
)A where minrn=1 or maxrn=1

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM