分组和排序后,MS Access从表中获得前2行

时间:2017-01-18 12:53:52

标签: sql ms-access

在Microsoft Access中,我有一个医疗程序表,我希望能够使用选择查询按医生对程序进行分组(即哪位医生最多执行了该程序)但仅限制每个程序2名医生。

我的数据表的结构如下:

| CLINICAL_ID | CLINICAL_DESC     | DOCTOR | PRICE   | COST   |
|-------------|-------------------|--------|---------|--------|
| PROCEDURE1  | Hip Replacement   | X01    | 10000   | 8000   |
| PROCEDURE1  | Hip Replacement   | X01    | 10000   | 8500   |
| PROCEDURE1  | Hip Replacement   | X01    | 10000   | 8300   |
| PROCEDURE1  | Hip Replacement   | X01    | 10000   | 7200   |
| PROCEDURE1  | Hip Replacement   | X02    | 10000   | 8100   |
| PROCEDURE1  | Hip Replacement   | X03    | 10000   | 11000  |
| PROCEDURE1  | Hip Replacement   | X03    | 10000   | 9800   |
| PROCEDURE1  | Hip Replacement   | X04    | 10000   | 8000   |
| PROCEDURE2  | Hip Revision      | X01    | 14000   | 12500  |
| PROCEDURE2  | Hip Revision      | X02    | 14000   | 11000  |
| PROCEDURE2  | Hip Revision      | X03    | 14000   | 13000  |
| PROCEDURE2  | Hip Revision      | X03    | 14000   | 13000  |
| PROCEDURE2  | Hip Revision      | X03    | 14000   | 11000  |
| PROCEDURE2  | Hip Revision      | X04    | 14000   | 11000  |
| PROCEDURE2  | Hip Revision      | X04    | 14000   | 14500  |
| PROCEDURE2  | Hip Revision      | X04    | 14000   | 13000  |

我希望结果如下:

| CLINICAL_ID | CLINICAL_DESC     | DOCTOR | COUNT | AVG_PRICE | AVG_COST |
|-------------|-------------------|--------|-------|-----------|----------|
| PROCEDURE1  | Hip Replacement   | X01    | 4     | 10000     | 8000     |
| PROCEDURE1  | Hip Replacement   | X03    | 2     | 10000     | 10400    |
| PROCEDURE2  | Hip Replacement   | X04    | 3     | 14000     | 12833    |
| PROCEDURE2  | Hip Replacement   | X03    | 3     | 14000     | 12333    |

到目前为止,我的查询似乎只显示了所有的医生:

SELECT
  Clinical_id,
  Clinical_desc,
  Doctor,
  COUNT(Doctor) AS Count,
  AVG(Price) AS Avg_price,
  AVG(Cost) AS Avg_cost
FROM (SELECT
  Clinical_id,
  Clinical_desc,
  Doctor,
  Price,
  Cost
FROM Summary_Data t
WHERE Doctor IN (SELECT TOP 2
  Doctor
FROM Summary_Data
WHERE Doctor = t.Doctor
GROUP BY Doctor
ORDER BY COUNT(Doctor) DESC, SUM(Cost) DESC
)) q
GROUP BY Clinical_id, Clinical_desc, Doctor
ORDER BY COUNT(Doctor) DESC, SUM(Cost) DESC;

看起来Top 2元素无法正常工作,但在我的生活中,我无法锻炼我需要做的其他事情。

2 个答案:

答案 0 :(得分:0)

您不需要子查询,但我认为您的查询应该有效。

这是一个稍微简单的版本:

SELECT Clinical_id, Clinical_desc, Doctor,
       COUNT(Doctor) AS Count,
       AVG(Price) AS Avg_price,
       AVG(Cost) AS Avg_cost
FROM Summary_Data as sd
WHERE sd.Doctor IN (SELECT TOP 2 sd2.Doctor
                    FROM Summary_Data as sd2
                    WHERE sd2.Clinical_id = sd.Clinical_id
                    GROUP BY sd2.Doctor
                    ORDER BY COUNT(sd2.Doctor) DESC, SUM(sd2.Cost) DESC,
                             sd2.Doctor  -- Just added to keep the ordering unique so 2 rows are always returned
                   )
GROUP BY sd.Clinical_id, sd.Clinical_desc, sd.Doctor
ORDER BY COUNT(Doctor) DESC, SUM(Cost) DESC;

答案 1 :(得分:0)

考虑一个相关计数子查询来计算行数,然后您可以过滤前2个排名。首先,将聚合查询保存为自己的存储查询(以避免主查询中的重度嵌套仍然可行):

汇总查询(以下称为ClinicalDoctorAgg)

SELECT Clinical_id, Clinical_desc, Doctor, AVG(Price) AS Avg_Price,
       Round(AVG(Cost),0) AS Avg_Cost, Count(*) AS Count_Procedures
FROM Summary_Data
GROUP BY Clinical_id, Clinical_desc, Doctor;

最终查询(可以使用WHERE子句修改顶部##)

SELECT main.*
FROM 
 (SELECT agg.*, (SELECT Count(*) 
                 FROM ClinicalDoctorAgg sub 
                 WHERE sub.Count_Procedures >= agg.Count_Procedures 
                 AND sub.Clinical_id = agg.Clinical_id 
                 AND sub.Clinical_desc = agg.Clinical_desc) As RowCount
  FROM ClinicalDoctorAgg As agg)  AS main
WHERE main.RowCount <= 2;