• Hi

    For inserting the data into the text file, I did a striaght select into the text file using sql query.

    For removing Duplicates I used the below query but I'm sure there is a better option.

    --select all dupicates into a temp table

    select pho_phone_number,count(*) cc

    into #temp

    from XD_HM_Extract with (nolock)

    where data_type = 'XD'

    group by pho_phone_number

    having count(*) > 1

    --select accounts with no duplicate into a temp table

    select acc_account_code,

    ped_initials,

    ped_name_1,

    ped_surname,

    adr_line_1,

    adr_line_2,

    adr_line_3,

    adr_line_4,

    adr_post_code,

    que_code,

    que_description,

    bck_id,

    pho_phone_number,

    data_type,

    status

    into #main

    from XD_HM_Extract xd with (nolock)

    where not exists (select *

    from #temp t

    where xd.pho_phone_number = t.pho_phone_number)

    and data_type = 'XD'

    --select all duplicate accounts

    select acc_account_code,

    ped_initials,

    ped_name_1,

    ped_surname,

    adr_line_1,

    adr_line_2,

    adr_line_3,

    adr_line_4,

    adr_post_code,

    que_code,

    que_description,

    bck_id,

    xd.pho_phone_number,

    data_type,

    status

    into #dup

    from XD_HM_Extract xd with (nolock)

    join #temp t with (nolock)

    on (xd.pho_phone_number = t.pho_phone_number)

    where data_type = 'XD'

    --select distinct accounts where phone number is duplicate

    select *

    into #main1

    from #dup xd

    where xd.acc_account_code = (select top 1 t.acc_account_code

    from #dup t

    where xd.pho_phone_number = t.pho_phone_number

    order by acc_account_code desc)

    --merging my two temp tables

    insert into #main

    select *

    from #main1

    /*

    drop table #main

    drop table #main1

    drop table #temp

    drop table #dup*/

    select *

    from #main