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 6117019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:16:22+00:00 2026-05-23T15:16:22+00:00

I have table Users which contains columns Login and Phone and I have table

  • 0

I have table Users which contains columns Login and Phone and I have table GeoPhone which contains columns Mask and Region.

Phone is stored as varchar (“+380981234567”). Mask is stored as varchar (“+38098”). Masks are overlapped, for instance, +380 and +38098.

I need a View that joins Users and GeoPhone tables and assigns Region for each Login by longest matched Mask.

I wrote SQL statement below but result contains duplicated Logins and I do not know how to avoid this:

SELECT TOP (5000) 
    Users.Login, Users.Phone, GeoPhone.Region, LEN(GeoPhone.Mask) AS MaskLen
FROM   Users 
INNER JOIN GeoPhone ON LEFT(Users.Phone, LEN(GeoPhone.Mask)) = GeoPhone.Mask
ORDER BY Users.Login, MaskLen DESC

Please help to improve it.

  • 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. Editorial Team
    Editorial Team
    2026-05-23T15:16:23+00:00Added an answer on May 23, 2026 at 3:16 pm

    One way to do this is to use Cross Apply in SQL 2005 or later.

    SELECT  
        TOP (5000) Users.Login, 
        Users.Phone, 
        GeoPhone.Region, 
        LEN(GeoPhone.Mask) AS MaskLen 
    FROM    Users
        CROSS APPLY
        (
            SELECT  TOP 1 Region,Mask
            FROM    geophone
            WHERE   LEFT (users.phone, Len(geophone.mask)) = geophone.mask 
            ORDER BY
                Len(geophone.mask) DESC
            ) geophone
    

    You should note that it only returns one record from geophone. If there are two records in geophone.mask that tie for the length of mask you won’t be able to determine which record it will return

    Here’s a solution that uses MAX() and its realtively portable

    SELECT TOP (5000) users.LOGIN, 
                      users.phone, 
                      geophone.region, 
                      Len(geophone.mask) AS masklen 
    FROM   users 
           INNER JOIN geophone 
             ON LEFT(users.phone, Len(geophone.mask)) = geophone.mask 
           INNER JOIN (SELECT MAX(Len(geophone.mask)) maxlength, 
                              users.phone 
                       FROM   users 
                              INNER JOIN geophone 
                                ON LEFT(users.phone, Len(geophone.mask)) = 
                                   geophone.mask 
                       GROUP  BY users.phone) t 
             ON t.phone = users.phone 
                AND t.maxlength = Len(geophone.mask) 
    

    However if two rows in geophone Tie for longest length both results will be returned. I don’t know if this is better or worse for you.

    You can also use ROW_NUMBER in SQL 2005 or later

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

Sidebar

Related Questions

I have table Users which contains columns: id, name, points, extra_points. How can I
I have a table called order which contains columns id , user_id , price
Say I have a table called Users, which contains your typical information: Id, Name,
I have a picture table which consists for users pictures Table Name:picture Columns: picture_id
I have a user table in my database which contains two columns FirstName and
I have a table called order which contains columns id , user_id , price
I have a table which contains User IDs, Period IDs and Qty: User Period
I currently have a user's table which contains a one-to-one relationship for Youtube OAuth
I have a table users which has a primary key userid and a datetime
I have a table in which users store scores and other information about said

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.