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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T10:28:15+00:00 2026-06-02T10:28:15+00:00

I am thinking I am getting close but not sure: I have an example

  • 0

I am thinking I am getting close but not sure:

I have an example Table containing email address, Personal and Work.
I am trying to create a new table which holds a single address.

Then insert an email address, example: If a user has a work email but no personal email, the insert work email and if no work email, insert personal email.

The following code does not work but it’s just to show some of the many variations I have used:

USE EXAMPLE_DB

GO

CREATE TABLE USER_EMAIL(
 USER_NBR CHAR(1) NOT NULL
,EMAIL VARCHAR(30)NULL
)

GO

INSERT INTO USER_EMAIL(
 USER_NBR
,EMAIL
)

SELECT USER_NBR
,CASE WHEN EMAIL_ADDR IS NULL AND EMAIL_TYPE = 'Personal' THEN EMAIL_TYPE = 'Work' END EMAIL
,CASE WHEN EMAIL_ADDR IS NULL AND EMAIL_TYPE = 'Work'     THEN EMAIL_TYPE =  'Personal' END EMAIL
FROM EMAIL_DATA

Here is the table for the EMAIL_DATA:

USER_NBR   EMAIL_TYPE       EMAIL_ADDR
1          Personal 
2          Personal     user2personal@demo.com
3          Personal         user3personal@demo.com
4          Personal 
5          Personal         user5personal@demo.com
1          Work         user1work@demo.com
2          Work         user2work@demo.com
3          Work 
4          Work         user4work@demo.com
5          Work 

Thanks everyone!

  • 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-02T10:28:18+00:00Added an answer on June 2, 2026 at 10:28 am

    Here’s an example using row_number:

    insert  user_email
            (user_nbr, email)
    select  user_nbr
    ,       mail.email_addr
    from    (
            select  user_nbr
            ,       email_addr
            ,       row_number() over (partition by user_nbr
                      order by
                      case 
                      when email_type = 'personal' then 1
                      when email_type = 'work' then 2
                      end) as rn
            from    email_data
            ) usr
    where   usr.rn = 1
    

    Since there are just two email types, you could also use left join twice:

    insert  user_email
            (user_nbr, email)
    select  user_nbr
    ,       coalesce(pers.email_addr, work.email_addr)
    from    (
            select  distinct user_nbr
            from    email_data
            ) usr
    left join 
            email_data as pers 
    on      pers.user_nbr = usr.user_nbr
            and work.email_type = 'personal'
    left join 
            email_data as work
    on      work.user_nbr = usr.user_nbr
            and work.email_type = 'work'
    

    Another approach that looks up the preferred email address using cross apply:

    insert  user_email
            (user_nbr, email)
    select  user_nbr
    ,       mail.email_addr
    from    (
            select  distinct user_nbr
            from    email_data
            ) usr
    cross apply
            (
            select  top 1 *
            from    email_data mail
            where   mail.user_nbr = usr.user_nbr
                    and mail.email_addr is not null
            order by
                    case 
                    when email_type = 'personal' then 1
                    when email_type = 'work' then 2
                    end
            ) mail
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm so close to getting this effect perfect, but I have run into a
I think I'm getting pretty close to the solution but can't really figure out
I'm close to this but cross-brained on something. I have this code: var numOfLi
Thinking that the answer to this is pretty obvious but here it goes: When
I'm thinking of trying a VCS such as subversion , to manage my next
I'm trying to figure out the best way to approach this. I have a
I have gone through many links on this site and others but I could
This is a strange issue and not sure what is going on. I am
I am thinking about getting an used HTC Droid Incredible to develop Android apps,
Thinking about getting into .net technology project management I've had plenty of experience with

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.