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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:01:00+00:00 2026-05-25T23:01:00+00:00

Suppose there’re 2 tables with data like below: — Table #Detail TelNO ———- 001xxxxx

  • 0

Suppose there’re 2 tables with data like below:

-- Table #Detail
TelNO      
---------- 
001xxxxx
020xxxxx
021xxxxx
800xxxxx
400xxxxx
28011111
82188888
22223333
...
...

-- Table #FeeRate
Expression Price   Description                                        
---------- ------- -------------------------------------------------- 
001%       10.00   International call
0[^0]%     5.00    National call
800%       .00     Free call
400%       .80     800 like, but caller need pay for local part
ELSE,How?  .20     Others/Local call

I want to select data from the 2 tables JOINed on dbo.FUNCTION_Match (TelNO, Expression) condition. (dbo.FUNCTION_Match is a crude function i wroted to match TelNO and Expression. In this sample, you can treat it as TelNO LIKE Expression).

So the SQL & query result is

SELECT * FROM #Detail d
    LEFT JOIN #FeeRate f ON d.TelNO LIKE f.Expression

TelNO      Expression Price   Description                                        
---------- ---------- ------- -------------------------------------------------- 
001xxxxx   001%       10.00   International call
020xxxxx   0[^0]%     5.00    National call
021xxxxx   0[^0]%     5.00    National call
800xxxxx   800%       .00     Free call
400xxxxx   400%       .80     800 like, but caller need pay for local part
28011111   NULL       NULL    NULL
82188888   NULL       NULL    NULL
22223333   NULL       NULL    NULL

The problem is clear, those local calls in #Detail can’t matched Others FeeRate.

My crude FUNCTION_Match function has handled multi-expressions like TelNO NOT LIKE '0%' AND TelNO NOT LIKE [84]00% OR TelNO LIKE '0755%' to match Others FeeRate, but it’s hard to write a correct multi-expression to express ELSE when there’re many records in #FeeRate table.

So, is there a way to implement ELSE expression from data ?


SQLs to create sample data

CREATE TABLE #Detail (TelNO VARCHAR(10) DEFAULT '')
CREATE TABLE #FeeRate (Expression VARCHAR(20) DEFAULT '', Price NUMERIC(10,2) DEFAULT 0, Description VARCHAR(50) DEFAULT '')

INSERT INTO #Detail VALUES ('001xxxxx')
INSERT INTO #Detail VALUES ('020xxxxx')
INSERT INTO #Detail VALUES ('021xxxxx')
INSERT INTO #Detail VALUES ('800xxxxx')
INSERT INTO #Detail VALUES ('400xxxxx')
INSERT INTO #Detail VALUES ('28011111')
INSERT INTO #Detail VALUES ('82188888')
INSERT INTO #Detail VALUES ('22223333')

INSERT INTO #FeeRate VALUES ('001%',  10.0, 'International call')
INSERT INTO #FeeRate VALUES ('0[^0]%', 5.0, 'National call')
INSERT INTO #FeeRate VALUES ('800%',   0.0, 'Free call')
INSERT INTO #FeeRate VALUES ('400%',   0.8,  '800 like, but caller need pay for local part')
INSERT INTO #FeeRate VALUES ('ELSE,How?',  0.2, 'Others/Local call')

SELECT * FROM #Detail
SELECT * FROM #FeeRate

SELECT
    *
FROM
    #Detail d
    LEFT JOIN #FeeRate f ON d.TelNO LIKE f.Expression

DROP TABLE #Detail
DROP TABLE #FeeRate
  • 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-25T23:01:00+00:00Added an answer on May 25, 2026 at 11:01 pm

    Add an extra column to your FeeRate table, called Priority. Assign higher priorities to the matches that should occur “earlier”. Do two joins to the fee rate table, the second being a left join, and looking for a higher priority match. If the left join works, reject the result row:

    CREATE TABLE #FeeRate (Expression VARCHAR(20) DEFAULT '', Price NUMERIC(10,2) DEFAULT 0, Description VARCHAR(50) DEFAULT '',Priority int)
    
    INSERT INTO #FeeRate VALUES ('001%',  10.0, 'International call',5)
    INSERT INTO #FeeRate VALUES ('0[^0]%', 5.0, 'National call',4)
    INSERT INTO #FeeRate VALUES ('800%',   0.0, 'Free call',3)
    INSERT INTO #FeeRate VALUES ('400%',   0.8,  '800 like, but caller need pay for local part',2)
    INSERT INTO #FeeRate VALUES ('%',  0.2, 'Others/Local call',1)
    
    SELECT * FROM #Detail d
        inner JOIN #FeeRate f ON d.TelNO LIKE f.Expression
        left join #FeeRate f_anti on d.TelNo LIKE f_anti.Expression and f_anti.Priority > f.Priority
    where
        f_anti.Price is null
    

    This probably allows you to simplify some of your other expressions also.

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

Sidebar

Related Questions

suppose there 5 column in my table info(id,loginid,pagename,pagecount,detail). I am executing the query like,
Suppose there is an xml file like below: <Instances> <Bits = 16 XCoord =
Suppose there was a SQL table like this: Label Name | Last name |
Suppose there are two tables. Table X-- Columns: id x_value Table Y-- Columns: id
Suppose there is a table like this: f1 f2 ---------- 1 3 4 8
Suppose there is a fully populated array of data String[n][3] myData. I want to
Suppose there is a table A which has a column AccessRights which is multivalued(
Suppose there are objects making subscriptions to a socket server like so: socket.on('news', obj.socketEvent)
Suppose there is a MSSQL table, UserPost , that represents something user has posted,
Suppose there are two entities EntityA and EntityB. Two tables are defined for both

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.