如何仅获得具有最大update_count值的所有订单?

时间:2019-02-18 15:20:58

标签: sql-server

我有实验室订单系统,例如,这些订单可以多次更新

order_id     test_id    update_count
10              1            1
10              1            2 
10              1            3 
11              2            1
11              5            1
12              3            1 
12              3            2 

我要选择所有不重复的订单,并选择更新次数最大的订单

我尝试了很多,我检查了WITH客户端,并进行了自我连接,但总是select语句返回具有所有更新计数的所有行,不仅是每个order_id的最大更新计数。

这是我的Select语句:

SELECT LAB_RESULTS.ORDER_ID as 'Order Number'
      ,LAB_RESULTS.PATIENT_NO as 'Patient No'
      ,Patients.Patient_Name as 'Patient Name'
      ,Patients.Age as 'Patient Age'
      ,LabTests.TestName as 'Test Name'
      ,LAB_RESULTS.RESULT_NUMBER as 'Result'
      ,LAB_RESULTS.RESULT_REPORT as 'Text Result'
      ,LAB_RESULTS.APPROVED_DATE as 'Approved_Date'
      ,LAB_RESULTS.REQ_FORM_NO as 'Request Form Number'
      ,LAB_RESULTS.CUSTID as 'Customer Id'
      ,Machines.Machine_name as 'Machine Name'
      ,LAB_RESULTS.SAMPLE_ID as 'Sample Id'
      ,LAB_RESULTS.packageid as 'package id'
      ,LAB_RESULTS.GROUPID as 'group id'
      ,LAB_RESULTS.EXAMINED_BY as 'Examined By'
      ,LAB_RESULTS.EXAMINED_DATE as 'Examined Date'
      ,LAB_RESULTS.APPROVED_BY as 'Approved By'
      ,LAB_RESULTS.update_count


FROM LAB_RESULTS 

  inner join patients on LAB_RESULTS.patient_no = Patients.Patient_No
  inner join labtests on  LabTests.TestId = LAB_RESULTS.TESTID
  inner join Machines on Machines.Machine_id = LAB_RESULTS.machine_id

where LAB_RESULTS.APPROVED_BY is not null 
and LAB_RESULTS.SAMPLE_STATUS = 6
and LAB_RESULTS.update_count in (select max(update_count) from LAB_RESULTS where LAB_RESULTS.SAMPLE_STATUS = 6 and LAB_RESULTS.deptid = 2 )
and LAB_RESULTS.deptid = 2

我希望得到以下结果:

order_id     test_id    update_count
10              1            3 
11              2            1
11              5            1
12              3            2 

然后我添加了此条件,但是此操作仅返回最大更新计数,而不是按每个order_id分组,仅返回所有订单的最大计数。

and LAB_RESULTS.update_count in (select max(update_count) from LAB_RESULTS where LAB_RESULTS.SAMPLE_STATUS = 6 and LAB_RESULTS.deptid = 2 )

我该怎么做?

3 个答案:

答案 0 :(得分:1)

  

然后我添加了此条件,但这仅返回最大更新   不按每个order_id分组,仅按所有订单的最大值进行计数。

update_count in()无效的唯一原因是因为您没有关联子查询。

代替此:

and LAB_RESULTS.update_count in (
  select max(update_count) 
  from LAB_RESULTS 
  where LAB_RESULTS.SAMPLE_STATUS = 6 
  and LAB_RESULTS.deptid = 2 
)

您需要这个:

and LAB_RESULTS.update_count in (
  select max(update_count) 
  from LAB_RESULTS l2
  where l2.SAMPLE_STATUS = 6 
  and l2.deptid = 2 
  AND l2.order_id=LAB_RESULTS.order_id   --correlate to outer query
)

尽管我也建议在主查询中为表加上别名,并在子查询中同时使用这两个别名。

答案 1 :(得分:0)

将此添加到您的选择语句中:

,DENSE_RANK() OVER   
    (PARTITION BY LAB_RESULTS.ORDER_ID ORDER BY LAB_RESULTS.update_count DESC) AS rank  

然后,您将把结果包装在WITH子句中,然后选择并过滤以执行以下操作:

WITH base_query as (
the current query you have with the extra column I suggested before) Select order_id, test_id, update_count where rank=1

答案 2 :(得分:0)

CREATE TABLE t1 
(order_id INT, test_id INT, update_count INT)

INSERT INTO t1 VALUES
(10,1,1),
(10,1,2),
(10,1,3),
(11,2,1),
(11,5,1),
(12,3,1),
(12,3,2)

SELECT * FROM dbo.t1

SELECT order_id, test_id, MAX(update_count)
FROM dbo.t1
GROUP BY order_id, test_id
ORDER BY order_id, test_id

DROP TABLE dbo.t1