Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 178879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T14:18:30+00:00 2026-05-11T14:18:30+00:00

I am trying to create an ANI lookup table from 2 separate tables, one

  • 0

I am trying to create an ANI lookup table from 2 separate tables, one a table of stores and the other a list of contacts for those stores.

I am using MS SQL Server 2005, which, unfortunately, does not support the MERGE INTO syntax…

The good stuff: The ANI lookup table has 2 significant columns, StoreID and PhoneNumber. The PhoneNumber column is the (unique) Primary key, as there must be only one StoreID returned for a given PhoneNumber.

Store_Info significant columns:

StoreID   StorePhone   AltPhone   

There is one record for each StoreID, with possible duplicate phone numbers between stores. And yes, AltPhone could be the same as StorePhone…

Store_Contacts significant columns:

StoreID   Phone   

There are multiple entries for StoreID, and possible duplicate phone numbers for one store or across multiple stores.

Sample store data

StoreID   Parent ID StorePhone       AltPhone   1         0         402-123-2300     402-123-2345   2         0         202-321-7800     202-321-7890   3         1         202-302-5600     202-302-5600   

Sample contacts data:

StoreID   Title    Name    Phone   1         Mgr      Bob     402-123-2345   1         IT       Pat     402-123-2346   1         Reg Mgr  Dave    402-321-3213   2         Mgr      Ann     202-231-7890   2         IT       Mary    202-231-7893   2         A/R      Ann     202-231-7890   2         Reg Mgr  Dave    402-321-3213   3         Mgr      Bob     402-123-2345   3         AsstMgr  Pete    402-123-2356   

I want to insert phone numbers in the following priority:

  1. Main/single store StorePhone
  2. Main/single store AltPhone
  3. Branch store StorePhone
  4. Branch store AltPhone
  5. Main/single store contact Phone
  6. Branch store contact phone
    • If a phone number already exists in the destination table, do not add it…

So the resulting dataset should be:

StoreID  Phone   1        402-123-2300  (first pass)   2        202-321-7800   1        402-123-2345  (2nd pass)   2        202-321-7890   3        202-302-5600  (3rd & 4th pass - only add once)   1        402-123-2346  (5th pass - skip dup)   1        402-321-3213   2        202-231-7893  (do not add dups)   3        402-123-2356  (final pass - skip dup)   

My approach to prioritizing which phone number of the duplicates to choose is to make multiple queries based on other criteria (main store vs branch, for example), inserting the first entry found into the ANI lookup table and skipping subsequent duplicates.

How do I do this without using RBAR? I have tried the following with no luck – actually, it works OK until I get to the Store_Contacts table, where there can be multiple identical phone numbers for a given store:

INSERT INTO dbo.Store_PhoneNumbers (StoreID, PhoneNumber)     SELECT DISTINCT StoreID, dbo.GetPhoneNumber10(StorePhone)     FROM dbo.Store_Info     WHERE dbo.IsAniNumber(dbo.GetPhoneNumber10(StorePhone)) = 1         AND ParentID = 0         AND NOT EXISTS (SELECT * FROM dbo.Store_PhoneNumbers WHERE PhoneNumber = dbo.GetPhonenumber10(StorePhone)); 

… repeat for AltPhone, then StorePhone where ParentID <> 0 then AltPhone w/ ParentID <> 0

So far so good, then here’s where it falls apart:

INSERT INTO dbo.Store_PhoneNumbers (StoreID, PhoneNumber)     SELECT DISTINCT sc.StoreID, dbo.GetPhoneNumber10(sc.Phone)     FROM Store_Contacts sc             INNER JOIN         Store_Info si ON sc.StoreID = si.StoreID     WHERE (dbo.IsAniNumber(dbo.GetPhoneNumber10(sc.Phone)) = 1)         AND (si.ParentID = 0)         AND NOT EXISTS (SELECT * FROM dbo.Store_PhoneNumbers WHERE PhoneNumber = dbo.GetPhonenumber10(sc.Phone)); 

… and repeat for ParentID <> 0

That’s where I get the duplicate entries and the insert fails.

Thanks for any help you can give me, I’m about to give up and use a cursor, just to get it done…
Dave

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T14:18:31+00:00Added an answer on May 11, 2026 at 2:18 pm
    SELECT DISTINCT sc.StoreID, dbo.GetPhoneNumber10(sc.Phone) 

    DISTINCT is wrong. It will allow 2 stores to share the same number. Use GROUP BY to ensure that the second column is unique.

    INSERT INTO dbo.Store_PhoneNumbers (StoreID, PhoneNumber) SELECT MIN(StoreID), PhoneNumber FROM (   SELECT sc.StoreID as StoreID, dbo.GetPhoneNumber10(sc.Phone) as PhoneNumber   FROM Store_Contacts sc       INNER JOIN       Store_Info si ON sc.StoreID = si.StoreID   WHERE (dbo.IsAniNumber(dbo.GetPhoneNumber10(sc.Phone)) = 1)       AND (si.ParentID = 0)       AND NOT EXISTS (SELECT * FROM dbo.Store_PhoneNumbers WHERE PhoneNumber = dbo.GetPhonenumber10(sc.Phone)) ) sub GROUP BY PhoneNumber 

    The reason you could get away with distinct in the other queries, was that you were working with a single StoreID in them. This query returns multiple StoreIDs.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been trying create this simple ish to-do list web app using JavaScript &
I'm trying create a bot which automatically likes Facebook posts. Using Mechanize I can
Ok so I am trying create a login script, here I am using PHP5
Trying to create a black line in my view to separate text blocks but
Trying to create Database as follows: USE Master GO IF NOT EXISTS(SELECT [Name] FROM
hi I'm trying create chat using node.js I see example in http://chat.nodejs.org/ I have
I trying create a class derivated from System.Web.UI.Page and in override Render i set
All, I am trying create a 'to & fro' animation using jquery animate &
I'm trying create a gui using Tkinter that grabs a username and password and
I'm trying create an immutable object and initialise it from xml config file in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.