如何按照执行的手术次数显示医生

时间:2014-01-04 04:07:50

标签: asp.net sql sql-server

我有表[Surgery_By][Surgery][Doctor]我正在使用ASP.NET和SQL Server:

[Surgery_By]包含以下列:

1-ID (PK)
2-Surgery ID (FK)
3-Doctor ID (FK)

如何按手术次数显示医生?

2 个答案:

答案 0 :(得分:0)

以这种方式试试

SELECT d.id, d.fullname, COUNT(s.id) total_surgeries
  FROM doctor d LEFT JOIN surgery_by s
    ON d.id = s.doctor_id
 GROUP BY d.id, d.fullname
 ORDER BY total_surgeries DESC

示例输出:

| ID |   FULLNAME | TOTAL_SURGERIES |
|----|------------|-----------------|
|  1 |   John Doe |               3 |
|  2 |   Jane Doe |               1 |
|  3 | Mark Smith |               0 |

这是 SQLFiddle 演示

答案 1 :(得分:0)

这是在黑暗中刺伤。

Select Doctor.ID As DoctorID
    ,Count(*) As Count
From Doctor
    Join Surgery_By
    On Doctor.ID = Surgery_By.DoctorID 
Group By Doctor.DoctorID
Order By Count(*)

我不确定您是否希望合并表Surgery(但如果您这样做,则加入将非常简单 - 只需确保将所选列添加到Group By语句中。)< / p>

从ASP.NET,您可以select this data from a SQL Command