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

  • Home
  • SEARCH
  • 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 3317734
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:37:19+00:00 2026-05-17T22:37:19+00:00

I am trying to merge 2 databases with the same schema together, and this

  • 0

I am trying to merge 2 databases with the same schema together, and this is one part of it.

I have changed the subject to keep it more understandable – I cannot change this schema, it’s just what I’m working with.

I have a table in both my source and target databases with the following columns:

Car
CarType1
CarType2
CarType3
CarType4

I am trying to write a query that will tell me in the target database, which rows have the same Cars between the 2 databases, but different CarTypes. All I need is a count on the rows that are different.

My query written in english would be:
Bring me back a count of rows where the Car is the same and the CarTypes between the two systems do not match. It doesn’t matter if the CarType is in a different CarType field between the two, just whether all of the values are contained in one of the 4 fields or not.

So if in my source database this row:

Car: Mustang
CarType1: Fast
CarType2: Convertible
CarType3: null
CarType4: null

And in my target database I have this row:

Car: Mustang
CarType1: Fast
CarType2: Convertible
CarType3: Sports
CarType4: null

This would count as a non-match, since it’s a Mustang and because the aggregate of the CarType fields is different. What order the values are in does not matter for this.

How would I write this query? I cannot get a grasp on 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-17T22:37:20+00:00Added an answer on May 17, 2026 at 10:37 pm
    ;WITH SourceT AS (
    SELECT 'Toyota' AS Car, 'A' AS CarType1, 'B' AS CarType2, 'C' CarType3, 'D' CarType4 UNION ALL
    SELECT 'BMW' AS Car, 'A' AS CarType1, 'B' AS CarType2, 'C' CarType3, 'D' CarType4 UNION ALL
    SELECT 'Mustang' AS Car, 'Fast' AS CarType1, 'Convertible' AS CarType2, 'Sports' CarType3, NULL CarType4 
    ),
    TargetT AS (
    SELECT 'Toyota' AS Car, 'D' AS CarType1, 'C' AS CarType2, 'B' CarType3, 'A' CarType4 UNION ALL
    SELECT 'BMW' AS Car, 'D' AS CarType1, 'C' AS CarType2, 'B' CarType3, 'A' CarType4  UNION ALL
    SELECT 'Mustang' AS Car, 'Fast' AS CarType1, 'Convertible' AS CarType2, NULL CarType3, NULL CarType4 )
    
    SELECT *
    FROM SourceT s
    WHERE NOT EXISTS
    (
    SELECT * 
    FROM TargetT t 
    WHERE s.Car = t.Car AND 0 =
    (SELECT COUNT(*) FROM 
        ( (
           (SELECT s.CarType1 AS t UNION ALL 
            SELECT s.CarType2 AS t UNION ALL 
            SELECT s.CarType3 AS t UNION ALL 
            SELECT s.CarType4 AS t )
        EXCEPT                
           (SELECT t.CarType1 AS t UNION ALL 
            SELECT t.CarType2 AS t UNION ALL 
            SELECT t.CarType3 AS t UNION ALL 
            SELECT t.CarType4 AS t )
            ) 
        UNION ALL
        (
           (SELECT t.CarType1 AS t UNION ALL 
            SELECT t.CarType2 AS t UNION ALL 
            SELECT t.CarType3 AS t UNION ALL 
            SELECT t.CarType4 AS t )
        EXCEPT                
           (SELECT s.CarType1 AS t UNION ALL 
            SELECT s.CarType2 AS t UNION ALL 
            SELECT s.CarType3 AS t UNION ALL 
            SELECT s.CarType4 AS t )
            )     
            ) T
        )
    )
    

    Or a slightly shorter version

    SELECT *
    FROM SourceT s
    WHERE NOT EXISTS
    (
    SELECT * 
    FROM TargetT t 
    WHERE s.Car = t.Car AND 
    (SELECT t FROM (SELECT s.CarType1 AS t UNION ALL 
                    SELECT s.CarType2 AS t UNION ALL 
                    SELECT s.CarType3 AS t UNION ALL 
                    SELECT s.CarType4 AS t ) D ORDER BY t FOR XML PATH(''))=
    (SELECT t FROM (SELECT t.CarType1 AS t UNION ALL 
                    SELECT t.CarType2 AS t UNION ALL 
                    SELECT t.CarType3 AS t UNION ALL 
                    SELECT t.CarType4 AS t ) D ORDER BY t FOR XML PATH(''))
        )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to merge two main menus together, but am having problems getting the
Trying to keep all the presentation stuff in the xhtml on this project and
I am trying to merge multiple excel files using DataTable.Merge() option For Each fileName
I'm trying to use svnmerge.py to merge some files. Under the hood it uses
What I am trying to achieve is to merge three strings. Two are provided
Trying to get my css / C# functions to look like this: body {
I'm trying to figure out the best way to move/merge a couple tables of
I am trying to merge records from an Oracle database table to my local
I'm trying to send an email merging one document (.docx) with a contacts database
Trying to keep it simple, but with almost no experience in version control, here

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.