Hello All please let me know how can i achieve this
drop table #mytab
create table #mytab (acctNo NUMERIC , phoneType varchar(10), PhoneNo NUMERIC )
INsert into #mytab VALUES (123456789, 'HOME', 9023232211)
INsert into #mytab VALUES (123456789, 'WORK', 7871231234)
INsert into #mytab VALUES (123456789, 'MISC', 8583232211)
INsert into #mytab VALUES (345678912, 'HOME', 4847894565)
INsert into #mytab VALUES (345678912, 'WORK', 2342344222)
INsert into #mytab VALUES (345678912, 'MISC', 8665554411)
INsert into #mytab VALUES (678967891, 'HOME', 5554441234)
INsert into #mytab VALUES (678967891, 'HOME', 5554441234)
INsert into #mytab VALUES (555444123, 'WORK', 1234441234)
INsert into #mytab VALUES (555444123, 'WORK', 1234441234)
INsert into #mytab VALUES (777444123, 'HOME', 5432154321)
INsert into #mytab VALUES (777444123, 'WORK', 6789678900)
INsert into #mytab VALUES (345967891, 'HOME', 5554441234)
INsert into #mytab VALUES (345967891, 'HOME', 5554441234)
INsert into #mytab VALUES (345967891, 'MISC', 1234441234)
when No MISC for particular AcctNo then
more than 1 home, then add the extra home to MISC.
when no extra home, and more than 1 work then add extra work to MISC.
expecting outcome
Select 123456789, 'HOME', 9023232211 UNION
Select 123456789, 'WORK', 7871231234 UNION
Select 123456789, 'MISC', 8583232211 UNION
Select 345678912, 'HOME', 4847894565 UNION
Select 345678912, 'WORK', 2342344222 UNION
Select 345678912, 'MISC', 8665554411 UNION
Select 678967891, 'HOME', 5554441234 UNION
Select 678967891, 'MISC', 5554441234 UNION
Select 555444123, 'WORK', 1234441234 UNION
Select 555444123, 'MISC', 1234441234 UNION
SELECT 777444123, 'HOME', 5432154321 UNION
SELECT 777444123, 'WORK', 6789678900 UNION
SELECT 345967891, 'HOME', 5554441234 UNION
SELECT 345967891, 'HOME', 5554441234 UNION
SELECT 345967891, 'MISC', 1234441234
Thank you
Asitta