@@ -2000,3 +2000,41 @@ ON [dbo].[tblUsersDistricts] ([UserID],[LocationId])
20002000WHERE ValidityTo is null
20012001GO
20022002
2003+ -- userdistrict index as sugested by ssms
2004+ CREATE NONCLUSTERED INDEX NCI_tblUserDistrict_locationId
2005+ ON [dbo].[tblUsersDistricts] ([LocationId],[ValidityTo])
2006+ INCLUDE ([UserID])
2007+ GO
2008+
2009+ -- uvwLocations without using other view
2010+
2011+ SET ANSI_NULLS ON
2012+ GO
2013+
2014+ SET QUOTED_IDENTIFIER ON
2015+ GO
2016+
2017+
2018+ ALTER VIEW [dbo].[uvwLocations]
2019+ AS
2020+ SELECT 0 LocationId, null VillageId, null VillageName,NULL VillageCode, null WardId, null WardName,NULL WardCode, null DistrictId,null DistrictName, NULL DistrictCode, NULL RegionId, N ' National' RegionName, null RegionCode, 0 ParentLocationId
2021+ UNION ALL
2022+ SELECT lUv .LocationId , lUv .LocationId VillageId, lUv .LocationName VillageName,luv .LocationCode VillageCode, luw .LocationId WardId, luw .LocationName WardName,luw .LocationCode WardCode, lUD .LocationId DistrictId, lUD .LocationName DistrictName, lud .LocationCode DistrictCode, LUR .LocationId RegionId, LUR .LocationName RegionName , lur .LocationCode RegionCode, LUv .ParentLocationId FROM tblLocations LUR
2023+ INNER JOIN tblLocations lud on lUR .LocationId = LUD .ParentLocationId and LUD .ValidityTo is null and LUD .LocationType = ' D'
2024+ INNER JOIN tblLocations luw on lUD .LocationId = LUW .ParentLocationId and LUW .ValidityTo is null and LUW .LocationType = ' W'
2025+ INNER JOIN tblLocations luv on luw .LocationId = LUv .ParentLocationId and LUv .ValidityTo is null and LUv .LocationType = ' V'
2026+ WHERE LUR .ValidityTo is null and LUR .LocationType = ' R'
2027+ UNION ALL
2028+ SELECT lUd .LocationId , null VillageId, null VillageName,NULL VillageCode, luw .LocationId WardId, luw .LocationName WardName,luw .LocationCode WardCode,lUD .LocationId DistrictId, lUD .LocationName DistrictName, lud .LocationCode DistrictCode, LUR .LocationId RegionId, LUR .LocationName RegionName , lur .LocationCode RegionCode, LUw .ParentLocationId FROM tblLocations LUR
2029+ INNER JOIN tblLocations lud on lUR .LocationId = LUD .ParentLocationId and LUD .ValidityTo is null and LUD .LocationType = ' D'
2030+ INNER JOIN tblLocations luw on lUD .LocationId = LUW .ParentLocationId and LUW .ValidityTo is null and LUW .LocationType = ' W'
2031+ WHERE LUR .ValidityTo is null and LUR .LocationType = ' R'
2032+ UNION ALL
2033+ SELECT lUd .LocationId , null VillageId, null VillageName,NULL VillageCode, null WardId, null WardName,NULL WardCode,lUD .LocationId DistrictId, lUD .LocationName DistrictName,lud .LocationCode DistrictCode, LUR .LocationId RegionId, LUR .LocationName RegionName , lur .LocationCode RegionCode, LUD .ParentLocationId FROM tblLocations LUR
2034+ INNER JOIN tblLocations lud on lUR .LocationId = LUD .ParentLocationId and LUD .ValidityTo is null and LUD .LocationType = ' D'
2035+ WHERE LUR .ValidityTo is null and LUR .LocationType = ' R'
2036+ UNION ALL
2037+ SELECT lUr .LocationId , null VillageId, null VillageName,NULL VillageCode, null WardId, null WardName,NULL WardCode, null DistrictId,null DistrictName, NULL DistrictCode, LUR .LocationId RegionId, LUR .LocationName RegionName, lur .LocationCode RegionCode, 0 ParentLocationId FROM tblLocations LUR
2038+ WHERE LUR .ValidityTo is null and LUR .LocationType = ' R'
2039+
2040+ Go
0 commit comments