Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 Update Query

Author  Topic 

thugs
Starting Member

13 Posts

Posted - 2015-02-09 : 07:45:00
I have 4 columns, ID, Dept.Number, Address and Grouping

ID Dept Address GroupID
165683 32 P O BOX 602 2
165925 32 PO BOX 562 2
165696 50 P O BOX 602 4
164356 103 PO BOX 175 6
164356 103 PO BOX 175 6
164368 203 PO BOX 72 19
165856 264 PO BOX 562 27
164374 302 PO BOX 39 32
164375 303 PO BOX 368 33
164375 303 PO BOX 368 33
164375 303 PO BOX 368 33
164375 303 PO BOX 368 33
165934 303 PO BOX 175 33
165934 303 PO BOX 175 33
164382 406 PO BOX 72 47
164421 905 PO BOX 39 57
165616 4310 PO BOX 368 87
165616 4310 PO BOX 368 87
165616 4310 PO BOX 368 87
165616 4310 PO BOX 368 87
166077 4913 ROUTE 3 89
165768 5434 PO BOX 368 91
164905 5434 PO BOX 368 91
165768 5434 PO BOX 368 91
164905 5434 PO BOX 368 91
165768 5434 PO BOX 368 91
164905 5434 PO BOX 368 91
164934 5803 PO BOX 368 92
164934 5803 PO BOX 368 92
164934 5803 PO BOX 368 92
164934 5803 PO BOX 368 92
166095 8040 ROUTE 3 109
165294 9311 PO BOX 175 115
165294 9311 PO BOX 175 115

I need to update the newgroupID field with the group ID based on the
Address and Dept columns.

First if address column matches update the newgruopID, With min GroupID Value.

Second, If the Address is diff and Dept is same update the new groupid with the group id of min Dept.

I have a hugs table and, I need to do it recursively.

Sample OUTPUT:
ID Dept Address GroupID New GroupID
165683 32 POBOX602 2 2
165925 32 POBOX562 2 2
165696 50 POBOX602 4 2
164356 103 POBOX175 6 6
164356 103 POBOX175 6 6
164368 203 POBOX72 19 19
165856 264 POBOX562 27 2
164374 302 POBOX39 32 32
164375 303 POBOX368 33 6
164375 303 POBOX368 33 6
164375 303 POBOX368 33 6
164375 303 POBOX368 33 6
165934 303 POBOX175 33 6
165934 303 POBOX175 33 6
164382 406 POBOX72 47 19
164421 905 POBOX39 57 32
165616 4310 POBOX368 87 6
165616 4310 POBOX368 87 6
165616 4310 POBOX368 87 6
165616 4310 POBOX368 87 6
166077 4913 ROUTE3 89 89
165768 5434 POBOX368 91 6
164905 5434 POBOX368 91 6
165768 5434 POBOX368 91 6
164905 5434 POBOX368 91 6
165768 5434 POBOX368 91 6
164905 5434 POBOX368 91 6
164934 5803 POBOX368 92 6
164934 5803 POBOX368 92 6
164934 5803 POBOX368 92 6
164934 5803 POBOX368 92 6
166095 8040 ROUTE3 109 89
165294 9311 POBOX175 115 6
165294 9311 POBOX175 115 6



Can anyone please pls help me on this.

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2015-02-09 : 08:56:42
Please clarify:

"First if address column matches update the newgruopID, With min GroupID Value."

Matches what?

"Second, If the Address is diff and Dept is same update the new groupid with the group id of min Dept."

different from what?

Go to Top of Page

Ifor
Aged Yak Warrior

700 Posts

Posted - 2015-02-09 : 09:15:50
The best I could come up with is iteration:

-- *** Test Data ***
-- Please supply in consumable format
CREATE TABLE #t
(
ID int NOT NULL
,Dept int NOT NULL
,[Address] varchar(30) NOT NULL
,GroupID int NOT NULL
,NewGroupID int NULL
)
INSERT INTO #t (ID, Dept, [Address], GroupID)
VALUES (165683, 32, 'PO BOX 602', 2),(165925, 32, 'PO BOX 562', 2),(165696, 50, 'PO BOX 602', 4)
,(164356, 103, 'PO BOX 175', 6),(164356, 103, 'PO BOX 175', 6),(164368, 203, 'PO BOX 72', 19)
,(165856, 264, 'PO BOX 562', 27),(164374, 302, 'PO BOX 39', 32),(164375, 303, 'PO BOX 368', 33)
,(164375, 303, 'PO BOX 368', 33),(164375, 303, 'PO BOX 368', 33),(164375, 303, 'PO BOX 368', 33)
,(165934, 303, 'PO BOX 175', 33),(165934, 303, 'PO BOX 175', 33),(164382, 406, 'PO BOX 72', 47)
,(164421, 905, 'PO BOX 39', 57),(165616, 4310, 'PO BOX 368', 87),(165616, 4310, 'PO BOX 368', 87)
,(165616, 4310, 'PO BOX 368', 87),(165616, 4310, 'PO BOX 368', 87),(166077, 4913, 'ROUTE 3', 89)
,(165768, 5434, 'PO BOX 368', 91),(164905, 5434, 'PO BOX 368', 91),(165768, 5434, 'PO BOX 368', 91)
,(164905, 5434, 'PO BOX 368', 91),(165768, 5434, 'PO BOX 368', 91),(164905, 5434, 'PO BOX 368', 91)
,(164934, 5803, 'PO BOX 368', 92),(164934, 5803, 'PO BOX 368', 92),(164934, 5803, 'PO BOX 368', 92)
,(164934, 5803, 'PO BOX 368', 92),(166095, 8040, 'ROUTE 3', 109),(165294, 9311, 'PO BOX 175', 115)
,(165294, 9311, 'PO BOX 175', 115);
-- *** End Test Data ***

WHILE 1=1
BEGIN;
WITH AddrGrps
AS
(
SELECT ID, Dept, [Address], GroupID, NewGroupID
,MIN(COALESCE(NewGroupID, GroupID)) OVER (PARTITION BY [Address]) AS MinGroupID
FROM #t
)
,DeptGrps
AS
(
SELECT ID, Dept, [Address], GroupID, NewGroupID
,MIN(MinGroupID) OVER (PARTITION BY Dept) AS MinGroupID
FROM AddrGrps
)
UPDATE DeptGrps
SET NewGroupID = MinGroupID
WHERE NewGroupID IS NULL
OR NewGroupID <> MinGroupID

IF @@ROWCOUNT = 0
BREAK;
END;

select * from #t;
Go to Top of Page

thugs
Starting Member

13 Posts

Posted - 2015-02-09 : 11:52:04
Thanks Ifor. Its working.

Go to Top of Page
   

- Advertisement -