简体   繁体   中英

Count text values across columns in SQL using CASE WHEN expressions

I have a table where I am determining whether a person's ID number exists across multiple databases. If the ID exists in only one database, then I would like to add another column that labels the person as "UNIQUE"; otherwise, it should be labeled as "NOT UNIQUE".

My query thus far is set up like this:

/* CTE that creates a long column of all distinct PersonID's across three databases */

WITH cte as
(SELECT DISTINCT t1.*
FROM 
(SELECT PersonID FROM DB_1.dbo.Persons
 UNION
 SELECT PersonID FROM DB_2.dbo.Persons
 UNION
 SELECT PersonID FROM DB_3.dbo.Persons)
t1)

/* Use CASE WHEN statements to check if Person exists in three other tables in DB_1, DB_2, and DB_3 */ 

SELECT PersonID,
    CASE WHEN PersonID IN (SELECT PersonID FROM DB_1.dbo.Table_1
                            UNION
                            SELECT PersonID FROM DB_1.dbo.Table_2
                            UNION 
                            SELECT PersonID FROM DB_1.dbo.Table_3)
    THEN 'TRUE'
    ELSE 'FALSE'
    END AS IN_DB_1,

    CASE WHEN PersonID IN (SELECT PersonID FROM DB_2.dbo.Table_1
                            UNION
                            SELECT PersonID FROM DB_2.dbo.Table_2
                            UNION 
                            SELECT PersonID FROM DB_2.dbo.Table_3)
    THEN 'TRUE'
    ELSE 'FALSE'
    END AS IN_DB_2,

    CASE WHEN PersonID IN (SELECT PersonID FROM DB_3.dbo.Table_1
                            UNION
                            SELECT PersonID FROM DB_3.dbo.Table_2
                            UNION 
                            SELECT PersonID FROM DB_3.dbo.Table_3)
    THEN 'TRUE'
    ELSE 'FALSE'
    END AS IN_DB_3

FROM cte

The results look like this:

PersonID   IN_DB_1    IN_DB_2    IN_DB_3
---------|----------|----------|----------|
001         TRUE       FALSE      FALSE
002         FALSE      TRUE       TRUE
003         TRUE       FALSE      FALSE
004         FALSE      TRUE       FALSE
005         TRUE       FALSE      TRUE

As can be seen, PersonID numbers 001, 003, and 004 appear only in one database. I would like to add a fifth column called "PID_UNIQUE" that counts the number of "TRUE" text values across the columns and specifies whether the person is unique.

It should look like this:

PersonID   IN_DB_1    IN_DB_2    IN_DB_3    PID_UNIQUE
---------|----------|----------|----------|-----------|
001         TRUE       FALSE      FALSE      UNIQUE
002         FALSE      TRUE       TRUE      NOT UNIQUE
003         TRUE       FALSE      FALSE      UNIQUE
004         FALSE      TRUE       FALSE      UNIQUE
005         TRUE       FALSE      TRUE      NOT UNIQUE

I assume this would be set up using another CASE WHEN expression. I am a little stuck as to how I could write that out to count across the three "IN_DB_no" columns.

I tried this:

CASE WHEN COUNT('TRUE') = 1
THEN 'UNIQUE'
ELSE 'NOT UNIQUE'
END AS PID_UNIQUE

However, it returned a column where all records were unique, which is not what I need.

I have a table where I am determining whether a person's ID number exists across multiple databases.

Your sample query references many more tables than this suggests. Hence, it seems much more complicated than necessary.

Let me assume that there are really three tables, one in each database. I see just an aggregation after UNION ALL :

SELECT PersonID, MAX(in_1), MAX(in_2), MAX(in_3),
       (CASE WHEN MAX(in_1) + MAX(in_2) + MAX(in_3) = 1 THEN 'UNIQUE'
             ELSE 'NOT UNIQUE'
        END) as pid_Unique
FROM ((SELECT DISTINCT PersonID, 1 as in_1, 0 as in_2, 0 as in_3
       FROM DB_1.dbo.Persons
      ) UNION ALL
      (SELECT DISTINCT PersonID, 0 as in_1, 1 as in_2, 0 as in_3
       FROM DB_2.dbo.Persons
      ) UNION ALL
      (SELECT DISTINCT PersonID, 0 as in_1, 0 as in_2, 1 as in_3
       FROM DB_3.dbo.Persons
      )
     ) p
GROUP BY PersonId;

I figured out a solution that works for me using the CROSS APPLY operator, along with a CASE / WHEN expression.

Basically, I added an additional column to the table I already made.

The query looked like this:

SELECT * FROM My_New_DB.dbo.My_New_Tbl

CROSS APPLY (
              SELECT CASE WHEN 1 = (SELECT COUNT(*)
              FROM (VALUES (IN_DB_1), (IN_DB_2), (IN_DB_3)) C (Val)
              WHERE Val = 'TRUE')
              THEN 'UNIQUE'
              ELSE 'NOT UNIQUE'
              END AS UNIQUE_ID
                               ) A

Simply put, when 1 = 1, it is unique.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM