## 3 replies

Last post Sep 18, 2017 08:35 AM by Eric Du

• mike4u

Contributor

2564 Points

1915 Posts

### top 5 district name

Sep 15, 2017 07:04 AM|mike4u|LINK

here is table structure:

SELECT  [DistrictId]
,[DistrictName]
,[DistrictHName]
,[Status]
,[CDate]
,[CBy]
FROM [landrecord].[dbo].[tblDistrict]
SELECT
,[r_date]
,[r_month]
,[DistrictId]
,[cell_02]
,[cell_03]
,[cell_04]
,[cell_05]
,[cell_06]
,[createddate]
,[status]
FROM [landrecord].[dbo].[praptra76]

I have to get  top 3 District data with District name in sql query with the help of below query:

select  sum(cell_04) as scell_04, sum(cell_05) as scell_05 from praptra76 where month(r_date)='7' and year(r_date)='2017'

kindly suggest

• markfitzme

All-Star

26021 Points

5870 Posts

### Re: top 5 district name

Sep 15, 2017 01:35 PM|markfitzme|LINK

Lots of ways to do this. The following hasn't been optimized but should get the job done.

Select
TOP 3
D.DistrictName
, S.scell_04
, s.scell_05
FROM
tblDistrict as D
INNER JOIN
(
select DistrictId, sum(cell_04) as scell_04, sum(cell_05) as scell_05 from praptra76 where month(r_date)='7' and year(r_date)='2017'
GROUP BY DistrictId
) as S
ON D.DistrictId = S.DistrictId
ORDER BY
scell_04 DESC, scell_05 desc

Don't forget to mark useful responses as Answer if they helped you towards a solution.
• mike4u

Contributor

2564 Points

1915 Posts

### Re: top 5 district name

Sep 16, 2017 01:24 PM|mike4u|LINK

### markfitzme

how to get lowest 5 district name kindly suggest.

• Eric Du

Contributor

6670 Points

2717 Posts

### Re: top 5 district name

Sep 18, 2017 08:35 AM|Eric Du|LINK

Hi mike4u,

According to your description and needs, please check the following sample code:

Sample Code:

Select
TOP 5
D.DistrictName
, S.scell_04
, s.scell_05
FROM
tblDistrict as D
INNER JOIN
(
select DistrictId, sum(cell_04) as scell_04, sum(cell_05) as scell_05 from praptra76 where month(r_date)='7' and year(r_date)='2017'
GROUP BY DistrictId
) as S
ON D.DistrictId = S.DistrictId
ORDER BY
scell_04 asc, scell_05 asc

Best Regards,

Eric Du

MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.