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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:27:05+00:00 2026-06-17T08:27:05+00:00

I have the following query that returns a comma separated list of email ids

  • 0

I have the following query that returns a comma separated list of email ids for the selected user.

CREATE TABLE TEST
(
    ID INT PRIMARY KEY,
    FIRSTNAME VARCHAR(30) NOT NULL,
    LASTNAME VARCHAR(30) NOT NULL,
    EMAIL VARCHAR(50)
)

INSERT INTO TEST VALUES (1, 'Tom', 'Lew', 'tom@hotmail.com')
INSERT INTO TEST VALUES (2, 'Tom', 'Lew', 'tom@hotmail.com')
INSERT INTO TEST VALUES (3, 'Jack', 'Stan', 'jstan@hotmail.com')
INSERT INTO TEST VALUES (4, 'Tom', 'Reed', 'tomreed@hotmail.com')
INSERT INTO TEST VALUES (5, 'Tom', 'Reed', 'tomr@hotmail.com')
INSERT INTO TEST VALUES (6, 'Tom', 'Reed', 'treed@hotmail.com')

DECLARE @result VARCHAR(MAX)

    SELECT @result = COALESCE(@result + ',', '') + [EMAIL]
    FROM [TEST]
    WHERE [FirstName] = 'Tom' AND [LastName] = 'Reed'

    SELECT @result

I need to modify the query so that it returns only one value either
– one email Id if there is only one email id associated with ‘Tom Reed’
– string ‘multiple’ if more than one email ids found for ‘Tom Reed’

I somehow was able to get the desired result but not sure about the performance. In real scenario this table will have thousands of records and will be joined to other tables.

SELECT
    DISTINCT
    CASE 
        WHEN CNT = 1 THEN EMAIL
        ELSE 'MULTIPLE'
    END
FROM (SELECT 
    ID, EMAIL,
    COUNT(*) OVER() CNT
    FROM TEST
    WHERE [FirstName] = 'Tom' AND [LastName] = 'Reed') T

Sorry, if the subject line and the content doesn’t match. Was not sure about the description.

Desired Results:

  1. If queried on ‘Tom Reed’

    Output: Multiple
    
  2. If queried on ‘Tom Lew’

    Output: Multiple
    
  3. If queried on ‘Jack Stan’

    Output: jstan@hotmail.com
    
  • 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-06-17T08:27:06+00:00Added an answer on June 17, 2026 at 8:27 am

    I think this would work (untested):

    SELECT DISTINCT
        FirstName, 
        LastName,  
        CASE 
          WHEN COUNT(Email) OVER (PARTITION BY FirstName,LastName) = 1 
          THEN Email 
          ELSE 'Multiple' END
    FROM Test
    

    And if you need just the word by the passed in params:

    SELECT DISTINCT
        CASE 
          WHEN COUNT(Email) OVER (PARTITION BY FirstName,LastName) = 1 
          THEN Email 
          ELSE 'Multiple' END
    FROM Test
    WHERE FirstName = 'Tom' 
       AND LastName = 'Reed'
    

    This should return either the Email or the word Multiple.

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

Sidebar

Related Questions

I have the following query that returns rows based on a comma seperated list
I have the following EF query that returns a list of usage data on
I have the following table: I am trying to create an SQL query that
I have the following query that returns test questions, possible answers to those questions
I have the following query that returns 1738 rows: select a.street, b.id from TableA
Possible Duplicate: mysql count into PHP variable I have the following query that returns
I have the following scenario: The user will input an interval and/or single comma-separated
I have following query which has select query that returns data in 5sec. But
I have the following LINQ query that returns two objects from my database. These
I have the following schema, and would like to do a query that returns

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.