I have a table like this:
| D | S |
|---|---|
| 2 | 1 |
| 2 | 3 |
| 4 | 2 |
| 4 | 3 |
| 4 | 5 |
| 6 | 1 |
in which the code of symptoms(S) of three diseases(D) are shown. I want to rearrange this table (D-S) such that the diseases with more symptoms come up i.e. order it by decreasing the numbers of symptoms as below:
| D | S |
|---|---|
| 4 | 2 |
| 4 | 3 |
| 4 | 5 |
| 2 | 1 |
| 2 | 3 |
| 6 | 1 |
Can anyone help me to write a SQL code for it in SQL server?
I had tried to do this as the following but this doesn't work:
SELECT *
FROM (
select D, Count(S) cnt
from [D-S]
group by D
) Q
order by Q.cnt desc