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 2008 Forums
 Transact-SQL (2008)
 Update Target Table

Author  Topic 

mlawton
Starting Member

35 Posts

Posted - 2015-01-28 : 10:18:34
I am trying to update the target table from the source table for the disruption column. When there is a disruption, the column should update with a 'Y', if not, then it should update with a 'N'.
In the source table for account 300815,
Service started on 1/15/2014 for City, Channel, Street
Removed City and Channel on 4/25/14
Added Channel back on 1/6/15.
This is a disruption of service for Channel because the customer removed Channel on 4/25/14 and added Channel back on 1/6/15.

Here is the data:
SOURCE TABLE
ACCOUNT START_DT END_DT AREA_NAME
300815 1/15/2014 4/23/2014 CHANNEL
300815 1/15/2014 4/24/2014 CHANNEL
300815 1/15/2014 12/31/9999 STREET
300815 1/15/2014 4/24/2014 CITY
300815 4/24/2014 4/24/2014 CHANNEL
300815 12/31/2014 1/5/2015 CHANNEL
300815 12/31/2014 12/31/9999 CHANNEL
400578 1/15/2014 4/23/2014 CHANNEL
400578 1/15/2014 5/10/2014 STREET
400578 4/24/2014 5/10/2014 CHANNEL
500187 12/15/2014 12/31/9999 CHANNEL
500187 12/15/2014 12/31/9999 CHANNEL
500187 12/15/2014 12/31/9999 STREET

TARGET TABLE
ACCOUNT AREA_NUMBER AREA_NAME START_DT ANNIVERSARY_DATE AREA_STATUS ACTIVE DISRUPTION
300815 2180 CITY 1/15/2014 1/15/2015 Inactive N ?
300815 2179 STREET 1/15/2014 1/15/2015 Active Y ?
300815 2181 STREET 1/15/2014 1/15/2015 Active Y ?
300815 2178 CHANNEL 1/15/2014 1/15/2015 Active Y ?
400578 2179 STREET 1/15/2014 1/15/2015 Inactive N ?
400578 2178 CHANNEL 1/15/2014 1/15/2015 Inactive N ?
500187 2178 HSD 12/15/2014 12/15/2015 Active Y ?
500187 2179 Video 12/15/2014 12/15/2015 Active Y ?


RESULTS AFTER UPDATE
ACCOUNT AREA_NUMBER AREA_NAME START_DT ANNIVERSARY_DATE AREA_STATUS ACTIVE DISRUPTION
300815 2180 CITY 1/15/2014 1/15/2015 Inactive N N
300815 2179 STREET 1/15/2014 1/15/2015 Active Y N
300815 2181 STREET 1/15/2014 1/15/2015 Active Y N
300815 2178 CHANNEL 1/15/2014 1/15/2015 Active Y Y
400578 2179 STREET 1/15/2014 1/15/2015 Inactive N N
400578 2178 CHANNEL 1/15/2014 1/15/2015 Inactive N Y
500187 2178 HSD 12/15/2014 12/15/2015 Active Y N
500187 2179 Video 12/15/2014 12/15/2015 Active Y N

Ifor
Aged Yak Warrior

700 Posts

Posted - 2015-01-28 : 12:04:24
I do not quite understand your data as Disruption is 'Y' for 400578/CHANNEL even though 4/23/2014 and 4/24/2014 are consecutive days.
Presumably it is a disruption as there is more than one row in the source table.

You will get quicker and more reliable answers if you provide consumable test data.

-- *** Test Data ***
CREATE TABLE #s
(
Account int NOT NULL
,Start_DT date NOT NULL
,End_DT date NOT NULL
,Area_Name varchar(20) NOT NULL
);
INSERT INTO #s
VALUES (300815, '20140115', '20140423', 'CHANNEL')
,(300815, '20140115', '20140424', 'CHANNEL')
,(300815, '20140115', '99991231', 'STREET')
,(300815, '20140115', '20140424', 'CITY')
,(300815, '20140424', '20140424', 'CHANNEL')
,(300815, '20141231', '20150105', 'CHANNEL')
,(300815, '20141231', '99991231', 'CHANNEL')
,(400578, '20140115', '20140423', 'CHANNEL')
,(400578, '20140115', '20140510', 'STREET')
,(400578, '20140424', '20140510', 'CHANNEL')
,(500187, '20141215', '99991231', 'CHANNEL')
,(500187, '20141215', '99991231', 'CHANNEL')
,(500187, '20141215', '99991231', 'STREET')

SELECT DISTINCT ACCOUNT, AREA_NAME, CAST(NULL AS char(1)) AS Disruption
INTO #t
FROM #s;
-- *** End Test Data ***

select * from #t;

WITH DeDupe
AS
(
SELECT DISTINCT *
FROM #s
)
,Disrupted
AS
(
SELECT Account, Area_Name
FROM DeDupe
GROUP BY Account, Area_Name
HAVING COUNT(*) > 1
)
UPDATE T
SET Disruption =
CASE
WHEN D.Account IS NULL THEN 'N'
ELSE 'Y'
END
FROM #t T
LEFT JOIN Disrupted D
ON T.Account = D.Account
AND T.Area_Name = D.Area_Name;

select * from #t;

Go to Top of Page

mlawton
Starting Member

35 Posts

Posted - 2015-01-28 : 14:32:13
Thanks!
Go to Top of Page
   

- Advertisement -