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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:21:11+00:00 2026-05-26T21:21:11+00:00

I am trying to add a query in my current SQL INSERT INTO statement.

  • 0

I am trying to add a query in my current SQL INSERT INTO statement. Below is my table and current sql statement.

I have 3 tables:

Table1: UserID, Username.

Table2: UserID, Status.

Table3: UserID, Username, Issue

Currently I only have 3 SELECT statement which fulfills the above 3 checks and INSERT the result into Table3:

 1.
    Insert in to Table3(userid,issue)
        SELECT t1.userid,'check no.1'
        FROM table1 t1
        FULL OUTER JOIN table2 t2 ON t1.userid = t2.userid
        where t1.userid not null and t2.userid is null
2.
    Insert in to Table3(userid,issue)
        SELECT t1.userid,'check no.2'
        FROM table1 t1
        inner JOIN table2 t2 ON t1.userid = t2.userid
        where t2.status = 'DELETE'

3.
    Insert in to Table3(userid,issue)
        SELECT t2.userid,'check no.3'
        FROM table2 t2
        right outer JOIN table2 t2 ON t1.userid = t2.userid
        where t2.status <> 'DELETE' and t1.userid is null

Now I wish to add in another additional check which is to check for duplicated userid in BOTH T1 AND T2:

  1. Check for same userid with same caps (e.g. E01 and E01 should not exist)
  2. Check for same userid but different caps (e.g. E01 and e01 should not exist)

How can I code the 4th sql query which specially checks for duplicated userids?

Thank you for the help.

  • 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-26T21:21:12+00:00Added an answer on May 26, 2026 at 9:21 pm

    Edited:

    INSERT INTO Table3(userid, Username, issue)
    SELECT 
        userid, Username, 'Duplicate UserID' 
    FROM 
        (
            SELECT 
                    userid, 
                    Username, 
                    RANK() OVER (PARTITION BY userid ORDER BY userid) AS [RANK] 
            FROM 
                Table1 --table2
        ) X
    WHERE 
        [RANK]=2
    
    
    INSERT INTO Table3(userid, Username, issue)
    SELECT 
        userid, [status], 'Duplicate UserID' 
    FROM 
        (
            SELECT 
                    userid, 
                    [status], 
                    RANK() OVER (PARTITION BY userid ORDER BY userid) AS [RANK] 
            FROM 
                Table2
        ) X
    WHERE 
        [RANK]=2
    

    You can use the COLLATE as shown in the OLD section to do a case sensitive check, if you need.

    I used RANK=2 to insert only one entry for duplicate userID

    OLD:
    You can use the COLLATE to do a case sensitive check

    INSERT INTO Table3(UserId, Username, Issue)
        SELECT 
        COALESCE(t1.UserId, t2.UserId), 
        t1.UserName,
        CASE        
            WHEN (t2.userid IS NULL AND t1.userid IS NOT NULL)
                THEN 'User exists in t1 but not in t2'
            WHEN (t2.status = 'DELETE' AND t1.userid IS NOT NULL)
                THEN 'User Exists in t1, but status in t2 is DELETE'
            WHEN (t2.userid IS NOT NULL AND t2.status != 'DELETE' AND t1.userid IS NULL)
                THEN 'Non-Deleted user in t2 does not exist in t1'
            WHEN t1.userid COLLATE SQL_Latin1_General_CP1_CS_AS = t2.userid COLLATE SQL_Latin1_General_CP1_CS_AS
                THEN 'Duplicate userid with same caps'
            WHEN t1.userid = t2.userid
                THEN 'Duplicate userid but different caps'
         END
    FROM table1 t1
    FULL OUTER JOIN table2 t2 ON t1.userid = t2.userid
    WHERE 
        (t1.userid IS NOT NULL AND t2.userid IS NOT NULL AND t2.status != 'DELETE' AND t1.userid = t2.userid) OR    
        (t2.userid IS NULL AND t1.userid IS NOT NULL) OR 
        (t2.status = 'DELETE' AND t1.userid IS NOT NULL) OR 
        (t2.userid IS NOT NULL AND t2.status != 'DELETE' AND t1.userid IS NULL)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to add a query in my current SQL INSERT INTO statement.
Ok SQL and Oracle gurus I have a somewhat complicated query that I'm trying
I am trying to insert a future date into a MySQL table from PHP.
I am trying to add a query string at the end of URL for
I'm trying to add the orderby expression on the fly. But when the query
I'm trying to add a mapping to the table IdentityGroup which has ManyToMany IdentityUsers
I'm trying to add this p4sql query as a Custom Tool in P4V (Windows)
I am trying to add security of GET query to exec function. If I
I am trying to insert current date/time and a value that was redirected to
I am trying to create a query in MS SQL 2005 that will return

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.