Hi,
Here is the Logic. I am sure we should Use CURSOR or not but here is logic.
I want to Insert the record into #Table2_Desti table from #Table1_Source
First Step, Group by #Table1_Source.Link_ID if we receive two record and one record Location is EAST and second
record Location is WEST so exclude WEST record from INSERT statement
Second, If Group By #Table1_Source.Link_ID and if we receive only one record doesn't matter the location is EAST OR WEST
insert into #Table2_Desti TABLE.
Below is the sample code SQL
Please let me know if my question is not clear or need more info. Thank You.
CREATE TABLE #Table1_Source
(
id INT IDENTITY(1,1)
,Link_ID INT
,City VARCHAR(20)
,Phone VARCHAR(20)
,Location VARCHAR(20)
)
CREATE TABLE #Table2_Desti
(
id INT IDENTITY(1,1)
,City VARCHAR(20)
,Phone VARCHAR(20)
,Link_ID INT
)
------ Insert sample data
INSERT INTO #Table1_Source ( Link_ID, City, Phone, Location )
SELECT 1234, 'Chicago', '687-234-9980','East'
UNION all
SELECT 1234, 'New York', '454-233-9077', 'West'
UNION all
SELECT 7868, 'Los Angles', '21-556-0909','East'
UNION all
SELECT 9877,'Central','234-000-9878','West'
SELECT * FROM #Table1_Source
SELECT * FROM #Table2_Desti
Here IS the END RESULT that I want in #Table2_Desti
ID,City,Phone,Link_ID
1,Chicago,687-234-9980,East
3,Los Angles,21-556-0909,East
4,Central,234-000-9878,West