I am trying to pull data from our address file which has ~400K records.
The below image shows part for one street in that table, MinSt/MaxSt are the address number. The street in this example goes from one city to the other multiple times.
![]()
I can group the data as such but that is the Min and Max Street number for the WHOLE street (Grouped by City), this is not what I need:
![]()
The output I desire would be as such, so there would be a record for each instance as it goes in and out of a city. This would be taking the data from the first image above and presenting it like so:
![]()
I have been trying to use a CTE
CREATE TABLE #tmpTable
(
[StNumberMin] [int] NULL,
[StNumberMax] [int] NULL,
[PreMod] [nvarchar](20) NULL,
[PreDir] [nvarchar](20) NULL,
[PreType] [nvarchar](50) NULL,
[Separator] [nvarchar](50) NULL,
[Name] [nvarchar](75) NULL,
[PostType] [nvarchar](50) NULL,
[PostDir] [nvarchar](20) NULL,
[PostMod] [nvarchar](20) NULL,
[City] [nvarchar](50) NULL,
[State] [nvarchar](2) NULL,
[Zip] [nvarchar](5) NULL
);
WITH Addresses_ALL
(
[StNumberMin]
,[StNumberMax]
,[PreMod]
,[PreDir]
,[PreType]
,[Separator]
,[Name]
,[PostType]
,[PostDir]
,[PostMod]
,[City]
,[Zip]
)
AS
(
SELECT
[StNumber]
,[StNumber]
,[PreMod]
,[PreDir]
,[PreType]
,[Separator]
,[Name]
,[PostType]
,[PostDir]
,[PostMod]
,[City]
,[Zip]
FROM [pubgis].[dbo].[TRANS_ADDRESSPOINTS]
GROUP BY PreMod, PreDir, Pretype, Separator, Name, PostType, PostDir, PostMod, StNumber, [City], [Zip]
)
INSERT INTO #tmpTable
(
[StNumberMin]
,[StNumberMax]
,[PreDir]
,[PreType]
,[Name]
,[PostType]
,[PostDir]
,[PostMod]
,[City]
,[Zip]
)
SELECT
MIN([StNumberMin])
,MAX([StNumberMax])
,[PreDir]
,[PreType]
,[Name]
,[PostType]
,[PostDir]
,[PostMod]
,[City]
,[Zip]
FROM Addresses_ALL
GROUP BY PreDir, PreType, Name, PostType, PostMod, PostDir, City, Zip
ORDER BY Name, PostType, City
SELECT * FROM #tmpTable
DROP TABLE #tmpTable
Would a CTE be the way to go with this ? If not, what would the preferred method be ?
Thank you !