I am trying to create a Group ID where either value from two columns in a table matches the either value from the two columns for a different row. For example:
Table:
+---------+-------------------+
| ogr_fid | comapared_ogr_fid |
+---------+-------------------+
| 8 | 10 |
| 6 | 8 |
| 10 | 4 |
| 5 | 3 |
| 5 | 2 |
| 12 | 15 |
+---------+-------------------+
Ideal Output:
+---------+-------------------+-------+
| ogr_fid | comapared_ogr_fid | SP_ID |
+---------+-------------------+-------+
| 8 | 10 | 1 |
| 6 | 8 | 1 |
| 10 | 4 | 1 |
| 5 | 3 | 2 |
| 5 | 2 | 2 |
| 12 | 15 | 3 |
+---------+-------------------+-------+
Any thoughts on how I could accomplish this? I've been trying to use a cursor. Below is my code. It seems to work, but returns duplicates. The commented out code does not work, it returns the error:
Warning: Null value is eliminated by an aggregate or other SET operation.
IF OBJECT_ID('SpatialAggregation.dbo.zzzInitialStack') IS NOT NULL -- Drop temp table if exists
DROP TABLE dbo.zzzInitialStack;
GO
CREATE TABLE dbo.zzzInitialStack
(
SP_ID int,
ogr_fid int,
compared_ogr_fid int
)
GO
IF OBJECT_ID('tempdb.dbo.#Stack') IS NOT NULL
DROP TABLE #Stack
;
SELECT ogr_fid
,compared_ogr_fid
INTO #Stack
FROM dbo.zzzCentroidCalc
WHERE CentroidDistance < 20 AND AvgScore >= 0.70 AND ogr_fid IS NOT NULL AND compared_ogr_fid IS NOT NULL
-- Declare Initial Variables
DECLARE @ogr_fid int, @compared_ogr_fid int, @sp_id int, @sp int, @in_grp int
-- set SP ID group to 1
SET @sp_id = 1
-- create Cursor
DECLARE sp_cursor CURSOR FOR
SELECT ogr_fid
,compared_ogr_fid
FROM #Stack
ORDER BY ogr_fid, compared_ogr_fid
OPEN sp_cursor
--Select next row from cursor
FETCH NEXT FROM sp_cursor
INTO @ogr_fid, @compared_ogr_fid
WHILE @@FETCH_STATUS = 0
--Start Stacked Parcel grouping
BEGIN
--IF (SELECT COUNT(*) FROM dbo.zzzInitialStack) IS NULL
-- BEGIN
-- INSERT INTO dbo.zzzInitialStack (SP_ID, ogr_fid,compared_ogr_fid)
-- VALUES(@sp_id,@ogr_fid,@compare_ogr_fid)
-- SET @sp_id = @sp_id + 1
-- END
--ELSE IF (SELECT count(*) FROM dbo.zzzInitialStack WHERE @ogr_fid = ogr_fid) IS NOT NULL
-- BEGIN
-- SET @sp = (SELECT MAX(SP_ID) FROM dbo.zzzInitialStack WHERE @ogr_fid = ogr_fid)
-- INSERT INTO dbo.zzzInitialStack(SP_ID, ogr_fid,compared_ogr_fid)
-- VALUES(@sp,@ogr_fid,@compare_ogr_fid)
-- END
--ELSE IF (SELECT COUNT(*) FROM dbo.zzzInitialStack WHERE @compared_ogr_fid = ogr_fid) IS NOT NULL
-- BEGIN
-- SET @sp = (SELECT MAX(SP_ID) FROM dbo.zzzInitialStack WHERE @compared_ogr_fid = ogr_fid)
-- INSERT INTO dbo.zzzInitialStack(SP_ID, ogr_fid,compared_ogr_fid)
-- VALUES(@sp,@ogr_fid,@compared_ogr_fid)
-- END
--ELSE
-- BEGIN
-- INSERT INTO dbo.zzzInitialStack(SP_ID, ogr_fid, compared_ogr_fid)
-- VALUES(@sp_id,@ogr_fid,@compared_ogr_fid)
-- SET @sp_id = @sp_id + 1
-- END
INSERT INTO dbo.zzzInitialStack (SP_ID,ogr_fid,compared_ogr_fid)
SELECT CASE WHEN @ogr_fid IN (SELECT ogr_fid FROM dbo.zzzInitialStack) OR @ogr_fid IN (SELECT ogr_fid FROM dbo.zzzInitialStack)
OR @compared_ogr_fid IN (SELECT ogr_fid FROM dbo.zzzInitialStack) OR @compared_ogr_fid IN (SELECT ogr_fid FROM dbo.zzzInitialStack)
THEN (SELECT TOP 1 SP_ID FROM dbo.zzzInitialStack
WHERE @ogr_fid = ogr_fid OR @ogr_fid = compared_ogr_fid
OR @compared_ogr_fid = ogr_fid OR @compared_ogr_fid = compared_ogr_fid)
ELSE (SELECT CASE WHEN MAX(SP_ID) IS NULL
THEN 1
ELSE CAST(MAX(SP_ID) + 1 AS INT)
END
FROM dbo.zzzInitialStack)
END AS SP_ID
,@ogr_fid AS ogr_fid
,@compared_ogr_fid AS compared_ogr_fid
FETCH NEXT FROM sp_cursor INTO @ogr_fid, @compared_ogr_fid
END;
CLOSE sp_cursor;
DEALLOCATE sp_cursor;
GO
IF OBJECT_ID('dbo.StackedParcels') IS NOT NULL
DROP TABLE dbo.StackedParcels;
WITH cte1 AS (
SELECT DISTINCT SP_ID, ogr_fid
FROM dbo.zzzInitialStack
UNION ALL
SELECT DISTINCT SP_ID, compared_ogr_fid AS ogr_fid
FROM dbo.zzzInitialStack
)
,cte2 AS (
SELECT SP_ID, count(ogr_fid) AS NumParcels
FROM cte1
GROUP BY SP_ID
)
SELECT a.SP_ID
,b.ogr_fid
,a.NumParcels
INTO dbo.StackedParcels
FROM cte2 AS a JOIN cte1 AS b ON a.SP_ID = b.SP_ID