I have a table
tblDiseaseTrack TrackID DiseaseID PostalCode 1 3 111 2 3 111 3 2 111 4 1 222 5 2 222 6 4 111 7 1 222 8 5 333 9 5 333 10 5 333
I want to write a query to display the disease id and the postal code of the maximum repeated DiseaseId for each postalcode as follows,
DiseaseID PostalCode 3 111 1 222 5 333
PLEASE any help would be much appreciated. i tried evrything and couldnt find any help..Thank you again 🙂
This returns exactly what you wanted:
EDIT:
Same query like above, but with a JOIN to get the city name from a second table:
Note that I used the same column name
PostalCodein the city table as well, so I have to prefix the PostalCode in the outmost query:T2.postalcodeinstead ofpostalcode.Otherwise the query would crash with the message:
…because SQL Server wouldn’t know which of the two PostalCodes I want.